blob: 878495b172570c2afe368f7ac91aa75f2f66b47a [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
Hanno Becker5aa4e2c2018-08-06 09:26:08 +010099static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
100 mbedtls_ssl_transform *transform );
101static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
102 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100103
104#define SSL_DONT_FORCE_FLUSH 0
105#define SSL_FORCE_FLUSH 1
106
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100108
109static uint16_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
110{
111 uint16_t mtu = ssl->conf->mtu;
112
113 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
114 return( (int) mtu );
115
116 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
117}
118
Hanno Becker67bc7c32018-08-06 11:33:50 +0100119static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
120{
121 size_t const bytes_written = ssl->out_left;
122 uint16_t const mtu = ssl_get_maximum_datagram_size( ssl );
123
124 /* Double-check that the write-index hasn't gone
125 * past what we can transmit in a single datagram. */
126 if( bytes_written > (size_t) mtu )
127 {
128 /* Should never happen... */
129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
130 }
131
132 return( (int) ( mtu - bytes_written ) );
133}
134
135static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
136{
137 int ret;
138 size_t remaining, expansion;
139 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
140
141#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
142 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
143
144 if( max_len > mfl )
145 max_len = mfl;
146#endif
147
148 ret = ssl_get_remaining_space_in_datagram( ssl );
149 if( ret < 0 )
150 return( ret );
151 remaining = (size_t) ret;
152
153 ret = mbedtls_ssl_get_record_expansion( ssl );
154 if( ret < 0 )
155 return( ret );
156 expansion = (size_t) ret;
157
158 if( remaining <= expansion )
159 return( 0 );
160
161 remaining -= expansion;
162 if( remaining >= max_len )
163 remaining = max_len;
164
165 return( (int) remaining );
166}
167
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200168/*
169 * Double the retransmit timeout value, within the allowed range,
170 * returning -1 if the maximum value has already been reached.
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200173{
174 uint32_t new_timeout;
175
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200176 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200177 return( -1 );
178
179 new_timeout = 2 * ssl->handshake->retransmit_timeout;
180
181 /* Avoid arithmetic overflow and range overflow */
182 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200183 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200184 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200185 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200186 }
187
188 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200190 ssl->handshake->retransmit_timeout ) );
191
192 return( 0 );
193}
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200196{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200197 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200199 ssl->handshake->retransmit_timeout ) );
200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200204/*
205 * Convert max_fragment_length codes to length.
206 * RFC 6066 says:
207 * enum{
208 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
209 * } MaxFragmentLength;
210 * and we add 0 -> extension unused
211 */
Angus Grattond8213d02016-05-25 20:56:48 +1000212static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213{
Angus Grattond8213d02016-05-25 20:56:48 +1000214 switch( mfl )
215 {
216 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
217 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
218 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
219 return 512;
220 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
221 return 1024;
222 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
223 return 2048;
224 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
225 return 4096;
226 default:
227 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
228 }
229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200231
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200232#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_ssl_session_free( dst );
236 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200239 if( src->peer_cert != NULL )
240 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200241 int ret;
242
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200243 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200245 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200250 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200253 dst->peer_cert = NULL;
254 return( ret );
255 }
256 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200259#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 if( src->ticket != NULL )
261 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200262 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200263 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200264 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265
266 memcpy( dst->ticket, src->ticket, src->ticket_len );
267 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200268#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269
270 return( 0 );
271}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200272#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
275int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200276 const unsigned char *key_enc, const unsigned char *key_dec,
277 size_t keylen,
278 const unsigned char *iv_enc, const unsigned char *iv_dec,
279 size_t ivlen,
280 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200281 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
283int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
284int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
285int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
286int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
287#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289/*
290 * Key material generation
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200293static int ssl3_prf( const unsigned char *secret, size_t slen,
294 const char *label,
295 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000296 unsigned char *dstbuf, size_t dlen )
297{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100298 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000299 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200300 mbedtls_md5_context md5;
301 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000302 unsigned char padding[16];
303 unsigned char sha1sum[20];
304 ((void)label);
305
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200306 mbedtls_md5_init( &md5 );
307 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200308
Paul Bakker5f70b252012-09-13 14:23:06 +0000309 /*
310 * SSLv3:
311 * block =
312 * MD5( secret + SHA1( 'A' + secret + random ) ) +
313 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
314 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
315 * ...
316 */
317 for( i = 0; i < dlen / 16; i++ )
318 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200319 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000320
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100321 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100322 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100323 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100324 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100325 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100326 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100327 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100328 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100329 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100330 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000331
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100332 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100333 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100334 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100335 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100336 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100337 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100338 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100339 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000340 }
341
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100342exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200343 mbedtls_md5_free( &md5 );
344 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500346 mbedtls_platform_zeroize( padding, sizeof( padding ) );
347 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000348
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100349 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200354static int tls1_prf( const unsigned char *secret, size_t slen,
355 const char *label,
356 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000357 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358{
Paul Bakker23986e52011-04-24 08:57:21 +0000359 size_t nb, hs;
360 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200361 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char tmp[128];
363 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 const mbedtls_md_info_t *md_info;
365 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100366 int ret;
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
373 hs = ( slen + 1 ) / 2;
374 S1 = secret;
375 S2 = secret + slen - hs;
376
377 nb = strlen( label );
378 memcpy( tmp + 20, label, nb );
379 memcpy( tmp + 20 + nb, random, rlen );
380 nb += rlen;
381
382 /*
383 * First compute P_md5(secret,label+random)[0..dlen]
384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100389 return( ret );
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
392 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
393 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395 for( i = 0; i < dlen; i += 16 )
396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_md_hmac_reset ( &md_ctx );
398 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
399 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_md_hmac_reset ( &md_ctx );
402 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
403 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
406
407 for( j = 0; j < k; j++ )
408 dstbuf[i + j] = h_i[j];
409 }
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 /*
414 * XOR out with P_sha1(secret,label+random)[0..dlen]
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100420 return( ret );
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
423 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
424 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 for( i = 0; i < dlen; i += 20 )
427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_md_hmac_reset ( &md_ctx );
429 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
430 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_md_hmac_reset ( &md_ctx );
433 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
434 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
437
438 for( j = 0; j < k; j++ )
439 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
440 }
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100443
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500444 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
445 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447 return( 0 );
448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
452static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100453 const unsigned char *secret, size_t slen,
454 const char *label,
455 const unsigned char *random, size_t rlen,
456 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457{
458 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100459 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000460 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
462 const mbedtls_md_info_t *md_info;
463 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100464 int ret;
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100472
473 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475
476 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100477 memcpy( tmp + md_len, label, nb );
478 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000479 nb += rlen;
480
481 /*
482 * Compute P_<hash>(secret, label + random)[0..dlen]
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100485 return( ret );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
488 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
489 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100490
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100491 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_md_hmac_reset ( &md_ctx );
494 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
495 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_md_hmac_reset ( &md_ctx );
498 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
499 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000500
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100501 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000502
503 for( j = 0; j < k; j++ )
504 dstbuf[i + j] = h_i[j];
505 }
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100508
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500509 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
510 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000511
512 return( 0 );
513}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100516static int tls_prf_sha256( const unsigned char *secret, size_t slen,
517 const char *label,
518 const unsigned char *random, size_t rlen,
519 unsigned char *dstbuf, size_t dlen )
520{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100522 label, random, rlen, dstbuf, dlen ) );
523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200527static int tls_prf_sha384( const unsigned char *secret, size_t slen,
528 const char *label,
529 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000530 unsigned char *dstbuf, size_t dlen )
531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100533 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#endif /* MBEDTLS_SHA512_C */
536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
541 defined(MBEDTLS_SSL_PROTO_TLS1_1)
542static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200543#endif
Paul Bakker380da532012-04-18 16:10:25 +0000544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SSL_PROTO_SSL3)
546static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
547static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200548#endif
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
551static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
552static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200553#endif
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
556#if defined(MBEDTLS_SHA256_C)
557static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
558static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
559static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200560#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_SHA512_C)
563static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
564static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
565static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200571 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 unsigned char keyblk[256];
574 unsigned char *key1;
575 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100576 unsigned char *mac_enc;
577 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000578 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200579 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 const mbedtls_cipher_info_t *cipher_info;
581 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_ssl_session *session = ssl->session_negotiate;
584 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
585 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100590 if( cipher_info == NULL )
591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100593 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100598 if( md_info == NULL )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100601 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100603 }
604
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000606 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_SSL_PROTO_SSL3)
609 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000610 {
Paul Bakker48916f92012-09-16 19:57:18 +0000611 handshake->tls_prf = ssl3_prf;
612 handshake->calc_verify = ssl_calc_verify_ssl;
613 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200615 else
616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
618 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000619 {
Paul Bakker48916f92012-09-16 19:57:18 +0000620 handshake->tls_prf = tls1_prf;
621 handshake->calc_verify = ssl_calc_verify_tls;
622 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000623 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200624 else
625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
627#if defined(MBEDTLS_SHA512_C)
628 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
629 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000630 {
Paul Bakker48916f92012-09-16 19:57:18 +0000631 handshake->tls_prf = tls_prf_sha384;
632 handshake->calc_verify = ssl_calc_verify_tls_sha384;
633 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000634 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_SHA256_C)
638 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000639 {
Paul Bakker48916f92012-09-16 19:57:18 +0000640 handshake->tls_prf = tls_prf_sha256;
641 handshake->calc_verify = ssl_calc_verify_tls_sha256;
642 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000643 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200644 else
645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200650 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000651
652 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 * SSLv3:
654 * master =
655 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
656 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
657 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200658 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200659 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 * master = PRF( premaster, "master secret", randbytes )[0..47]
661 */
Paul Bakker0a597072012-09-25 21:55:46 +0000662 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000665 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
668 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200669 {
670 unsigned char session_hash[48];
671 size_t hash_len;
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200674
675 ssl->handshake->calc_verify( ssl, session_hash );
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
678 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200681 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200683 {
684 hash_len = 48;
685 }
686 else
687#endif
688 hash_len = 32;
689 }
690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200692 hash_len = 36;
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200695
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100696 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
697 "extended master secret",
698 session_hash, hash_len,
699 session->master, 48 );
700 if( ret != 0 )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100703 return( ret );
704 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200705
706 }
707 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200708#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100709 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
710 "master secret",
711 handshake->randbytes, 64,
712 session->master, 48 );
713 if( ret != 0 )
714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100716 return( ret );
717 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200718
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500719 mbedtls_platform_zeroize( handshake->premaster,
720 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 }
722 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 /*
726 * Swap the client and server random values.
727 */
Paul Bakker48916f92012-09-16 19:57:18 +0000728 memcpy( tmp, handshake->randbytes, 64 );
729 memcpy( handshake->randbytes, tmp + 32, 32 );
730 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500731 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733 /*
734 * SSLv3:
735 * key block =
736 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
737 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
738 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
739 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
740 * ...
741 *
742 * TLSv1:
743 * key block = PRF( master, "key expansion", randbytes )
744 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100745 ret = handshake->tls_prf( session->master, 48, "key expansion",
746 handshake->randbytes, 64, keyblk, 256 );
747 if( ret != 0 )
748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100750 return( ret );
751 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
754 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
755 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
756 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
757 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500759 mbedtls_platform_zeroize( handshake->randbytes,
760 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
762 /*
763 * Determine the appropriate key, IV and MAC length.
764 */
Paul Bakker68884e32013-01-07 18:20:04 +0100765
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200766 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200769 cipher_info->mode == MBEDTLS_MODE_CCM ||
770 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200772 size_t taglen, explicit_ivlen;
773
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200774 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000775 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200776
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200777 /* All modes haves 96-bit IVs;
778 * GCM and CCM has 4 implicit and 8 explicit bytes
779 * ChachaPoly has all 12 bytes implicit
780 */
Paul Bakker68884e32013-01-07 18:20:04 +0100781 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200782 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
783 transform->fixed_ivlen = 12;
784 else
785 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200786
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200787 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
788 taglen = transform->ciphersuite_info->flags &
789 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
790
791
792 /* Minimum length of encrypted record */
793 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
794 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100795 }
796 else
797 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200798 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
800 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200803 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100804 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000805
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200806 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000807 mac_key_len = mbedtls_md_get_size( md_info );
808 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200811 /*
812 * If HMAC is to be truncated, we shall keep the leftmost bytes,
813 * (rfc 6066 page 13 or rfc 2104 section 4),
814 * so we only need to adjust the length here.
815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000819
820#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
821 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000822 * HMAC implementation which also truncates the key
823 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000824 mac_key_len = transform->maclen;
825#endif
826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200828
829 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100830 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200832 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200834 transform->minlen = transform->maclen;
835 else
Paul Bakker68884e32013-01-07 18:20:04 +0100836 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200837 /*
838 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100839 * 1. if EtM is in use: one block plus MAC
840 * otherwise: * first multiple of blocklen greater than maclen
841 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
844 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100845 {
846 transform->minlen = transform->maclen
847 + cipher_info->block_size;
848 }
849 else
850#endif
851 {
852 transform->minlen = transform->maclen
853 + cipher_info->block_size
854 - transform->maclen % cipher_info->block_size;
855 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
858 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
859 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200860 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100861 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
864 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
865 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200866 {
867 transform->minlen += transform->ivlen;
868 }
869 else
870#endif
871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874 }
Paul Bakker68884e32013-01-07 18:20:04 +0100875 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 }
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000879 transform->keylen, transform->minlen, transform->ivlen,
880 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
882 /*
883 * Finally setup the cipher contexts, IVs and MAC secrets.
884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000888 key1 = keyblk + mac_key_len * 2;
889 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
Paul Bakker68884e32013-01-07 18:20:04 +0100891 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000892 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000894 /*
895 * This is not used in TLS v1.1.
896 */
Paul Bakker48916f92012-09-16 19:57:18 +0000897 iv_copy_len = ( transform->fixed_ivlen ) ?
898 transform->fixed_ivlen : transform->ivlen;
899 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
900 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000901 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902 }
903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#endif /* MBEDTLS_SSL_CLI_C */
905#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200906 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000907 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000908 key1 = keyblk + mac_key_len * 2 + transform->keylen;
909 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Hanno Becker81c7b182017-11-09 18:39:33 +0000911 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100912 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000914 /*
915 * This is not used in TLS v1.1.
916 */
Paul Bakker48916f92012-09-16 19:57:18 +0000917 iv_copy_len = ( transform->fixed_ivlen ) ?
918 transform->fixed_ivlen : transform->ivlen;
919 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
920 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000921 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100928 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#if defined(MBEDTLS_SSL_PROTO_SSL3)
931 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100932 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000933 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100937 }
938
Hanno Becker81c7b182017-11-09 18:39:33 +0000939 memcpy( transform->mac_enc, mac_enc, mac_key_len );
940 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100941 }
942 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#endif /* MBEDTLS_SSL_PROTO_SSL3 */
944#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
945 defined(MBEDTLS_SSL_PROTO_TLS1_2)
946 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100947 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100948 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
949 For AEAD-based ciphersuites, there is nothing to do here. */
950 if( mac_key_len != 0 )
951 {
952 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
953 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
954 }
Paul Bakker68884e32013-01-07 18:20:04 +0100955 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200956 else
957#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200961 }
Paul Bakker68884e32013-01-07 18:20:04 +0100962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
964 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000965 {
966 int ret = 0;
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100971 transform->iv_enc, transform->iv_dec,
972 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100973 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000974 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
977 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000978 }
979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000981
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200982#if defined(MBEDTLS_SSL_EXPORT_KEYS)
983 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100984 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200985 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
986 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000987 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100988 iv_copy_len );
989 }
990#endif
991
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200992 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200993 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200996 return( ret );
997 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200998
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200999 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001000 cipher_info ) ) != 0 )
1001 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001003 return( ret );
1004 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001007 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001011 return( ret );
1012 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001015 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001019 return( ret );
1020 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_CIPHER_MODE_CBC)
1023 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1026 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001030 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1033 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001036 return( ret );
1037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001041 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001044 // Initialize compression
1045 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001047 {
Paul Bakker16770332013-10-11 09:59:44 +02001048 if( ssl->compress_buf == NULL )
1049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001051 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001052 if( ssl->compress_buf == NULL )
1053 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001055 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001056 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001057 }
1058 }
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001061
Paul Bakker48916f92012-09-16 19:57:18 +00001062 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1063 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001064
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001065 if( deflateInit( &transform->ctx_deflate,
1066 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001067 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1070 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001071 }
1072 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
1077 return( 0 );
1078}
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#if defined(MBEDTLS_SSL_PROTO_SSL3)
1081void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001082{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001083 mbedtls_md5_context md5;
1084 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 unsigned char pad_1[48];
1086 unsigned char pad_2[48];
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001090 mbedtls_md5_init( &md5 );
1091 mbedtls_sha1_init( &sha1 );
1092
1093 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1094 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Paul Bakker380da532012-04-18 16:10:25 +00001096 memset( pad_1, 0x36, 48 );
1097 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001099 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1100 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1101 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001103 mbedtls_md5_starts_ret( &md5 );
1104 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1105 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1106 mbedtls_md5_update_ret( &md5, hash, 16 );
1107 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001109 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1110 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1111 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001112
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001113 mbedtls_sha1_starts_ret( &sha1 );
1114 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1115 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1116 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1117 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001121
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001122 mbedtls_md5_free( &md5 );
1123 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001124
Paul Bakker380da532012-04-18 16:10:25 +00001125 return;
1126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1130void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001131{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001132 mbedtls_md5_context md5;
1133 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001136
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001137 mbedtls_md5_init( &md5 );
1138 mbedtls_sha1_init( &sha1 );
1139
1140 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1141 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001142
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001143 mbedtls_md5_finish_ret( &md5, hash );
1144 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001148
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001149 mbedtls_md5_free( &md5 );
1150 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001151
Paul Bakker380da532012-04-18 16:10:25 +00001152 return;
1153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1157#if defined(MBEDTLS_SHA256_C)
1158void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001159{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001160 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001161
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001162 mbedtls_sha256_init( &sha256 );
1163
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001165
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001166 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001167 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001171
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001172 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001173
Paul Bakker380da532012-04-18 16:10:25 +00001174 return;
1175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#if defined(MBEDTLS_SHA512_C)
1179void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001180{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001181 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001182
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001183 mbedtls_sha512_init( &sha512 );
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001186
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001187 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001188 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001193 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001194
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 return;
1196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#endif /* MBEDTLS_SHA512_C */
1198#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1201int 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 +02001202{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001203 unsigned char *p = ssl->handshake->premaster;
1204 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001205 const unsigned char *psk = ssl->conf->psk;
1206 size_t psk_len = ssl->conf->psk_len;
1207
1208 /* If the psk callback was called, use its result */
1209 if( ssl->handshake->psk != NULL )
1210 {
1211 psk = ssl->handshake->psk;
1212 psk_len = ssl->handshake->psk_len;
1213 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001214
1215 /*
1216 * PMS = struct {
1217 * opaque other_secret<0..2^16-1>;
1218 * opaque psk<0..2^16-1>;
1219 * };
1220 * with "other_secret" depending on the particular key exchange
1221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1223 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001224 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001225 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001227
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001228 *(p++) = (unsigned char)( psk_len >> 8 );
1229 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001230
1231 if( end < p || (size_t)( end - p ) < psk_len )
1232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1233
1234 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001235 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001236 }
1237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1239#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1240 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001241 {
1242 /*
1243 * other_secret already set by the ClientKeyExchange message,
1244 * and is 48 bytes long
1245 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001246 if( end - p < 2 )
1247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1248
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001249 *p++ = 0;
1250 *p++ = 48;
1251 p += 48;
1252 }
1253 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1255#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1256 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001257 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001258 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001259 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001260
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001261 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001263 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001264 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001267 return( ret );
1268 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001269 *(p++) = (unsigned char)( len >> 8 );
1270 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001271 p += len;
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001274 }
1275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1277#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1278 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001279 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001280 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001281 size_t zlen;
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001284 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001285 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001288 return( ret );
1289 }
1290
1291 *(p++) = (unsigned char)( zlen >> 8 );
1292 *(p++) = (unsigned char)( zlen );
1293 p += zlen;
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001296 }
1297 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001302 }
1303
1304 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001305 if( end - p < 2 )
1306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001307
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001308 *(p++) = (unsigned char)( psk_len >> 8 );
1309 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001310
1311 if( end < p || (size_t)( end - p ) < psk_len )
1312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1313
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001314 memcpy( p, psk, psk_len );
1315 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001316
1317 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1318
1319 return( 0 );
1320}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001324/*
1325 * SSLv3.0 MAC functions
1326 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001327#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001328static void ssl_mac( mbedtls_md_context_t *md_ctx,
1329 const unsigned char *secret,
1330 const unsigned char *buf, size_t len,
1331 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001332 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001333{
1334 unsigned char header[11];
1335 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001336 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1338 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001339
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001340 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001342 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001343 else
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
1346 memcpy( header, ctr, 8 );
1347 header[ 8] = (unsigned char) type;
1348 header[ 9] = (unsigned char)( len >> 8 );
1349 header[10] = (unsigned char)( len );
1350
Paul Bakker68884e32013-01-07 18:20:04 +01001351 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mbedtls_md_starts( md_ctx );
1353 mbedtls_md_update( md_ctx, secret, md_size );
1354 mbedtls_md_update( md_ctx, padding, padlen );
1355 mbedtls_md_update( md_ctx, header, 11 );
1356 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001357 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
Paul Bakker68884e32013-01-07 18:20:04 +01001359 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_md_starts( md_ctx );
1361 mbedtls_md_update( md_ctx, secret, md_size );
1362 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001363 mbedtls_md_update( md_ctx, out, md_size );
1364 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1369 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001370 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001371#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001372#endif
1373
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001374/* The function below is only used in the Lucky 13 counter-measure in
1375 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1376#if defined(SSL_SOME_MODES_USE_MAC) && \
1377 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1378 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1379 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1380/* This function makes sure every byte in the memory region is accessed
1381 * (in ascending addresses order) */
1382static void ssl_read_memory( unsigned char *p, size_t len )
1383{
1384 unsigned char acc = 0;
1385 volatile unsigned char force;
1386
1387 for( ; len != 0; p++, len-- )
1388 acc ^= *p;
1389
1390 force = acc;
1391 (void) force;
1392}
1393#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1394
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001395/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001401 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001405 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001409 }
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001414 ssl->out_msg, ssl->out_msglen );
1415
Angus Grattond8213d02016-05-25 20:56:48 +10001416 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001417 {
Hanno Becker184f6752017-10-04 13:47:33 +01001418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1419 (unsigned) ssl->out_msglen,
Angus Grattond8213d02016-05-25 20:56:48 +10001420 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerd33f1ca2017-09-18 10:55:31 +01001421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1422 }
1423
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001425 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001427#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 if( mode == MBEDTLS_MODE_STREAM ||
1429 ( mode == MBEDTLS_MODE_CBC
1430#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1431 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001432#endif
1433 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_SSL_PROTO_SSL3)
1436 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001437 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001438 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001439
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001440 ssl_mac( &ssl->transform_out->md_ctx_enc,
1441 ssl->transform_out->mac_enc,
1442 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001443 ssl->out_ctr, ssl->out_msgtype,
1444 mac );
1445
1446 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001447 }
1448 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1451 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1452 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001453 {
Hanno Becker992b6872017-11-09 18:57:39 +00001454 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1457 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1458 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1459 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001460 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001461 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001463
1464 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001465 }
1466 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001467#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001471 }
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001474 ssl->out_msg + ssl->out_msglen,
1475 ssl->transform_out->maclen );
1476
1477 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001478 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001479 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001480#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001482 /*
1483 * Encrypt
1484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1486 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001488 int ret;
1489 size_t olen = 0;
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 "including %d bytes of padding",
1493 ssl->out_msglen, 0 ) );
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001496 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001497 ssl->transform_out->ivlen,
1498 ssl->out_msg, ssl->out_msglen,
1499 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001502 return( ret );
1503 }
1504
1505 if( ssl->out_msglen != olen )
1506 {
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 );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 }
Paul Bakker68884e32013-01-07 18:20:04 +01001511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001513#if defined(MBEDTLS_GCM_C) || \
1514 defined(MBEDTLS_CCM_C) || \
1515 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001517 mode == MBEDTLS_MODE_CCM ||
1518 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001519 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001520 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001521 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001522 unsigned char *enc_msg;
1523 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001524 unsigned char iv[12];
1525 mbedtls_ssl_transform *transform = ssl->transform_out;
1526 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001528 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001530 /*
1531 * Prepare additional authenticated data
1532 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533 memcpy( add_data, ssl->out_ctr, 8 );
1534 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001536 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001537 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1538 add_data[12] = ssl->out_msglen & 0xFF;
1539
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001540 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541
Paul Bakker68884e32013-01-07 18:20:04 +01001542 /*
1543 * Generate IV
1544 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001545 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1546 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001547 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001548 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1549 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1550 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1551
1552 }
1553 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1554 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001555 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001556 unsigned char i;
1557
1558 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1559
1560 for( i = 0; i < 8; i++ )
1561 iv[i+4] ^= ssl->out_ctr[i];
1562 }
1563 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001564 {
1565 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001568 }
1569
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001570 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1571 iv, transform->ivlen );
1572 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1573 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001574
Paul Bakker68884e32013-01-07 18:20:04 +01001575 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001576 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001577 */
1578 enc_msg = ssl->out_msg;
1579 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001583 "including 0 bytes of padding",
1584 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585
Paul Bakker68884e32013-01-07 18:20:04 +01001586 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001587 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001588 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001589 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1590 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 add_data, 13,
1592 enc_msg, enc_msglen,
1593 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001594 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001597 return( ret );
1598 }
1599
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001600 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001604 }
1605
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001606 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001607 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1613#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001614 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001617 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001618 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001619 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001620
Paul Bakker48916f92012-09-16 19:57:18 +00001621 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1622 ssl->transform_out->ivlen;
1623 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 padlen = 0;
1625
1626 for( i = 0; i <= padlen; i++ )
1627 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1628
1629 ssl->out_msglen += padlen + 1;
1630
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001631 enc_msglen = ssl->out_msglen;
1632 enc_msg = ssl->out_msg;
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001635 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001636 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1637 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640 {
1641 /*
1642 * Generate IV
1643 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001644 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001645 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001646 if( ret != 0 )
1647 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648
Paul Bakker92be97b2013-01-02 17:30:03 +01001649 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001650 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651
1652 /*
1653 * Fix pointer positions and message length with added IV
1654 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001655 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001657 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001662 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001663 ssl->out_msglen, ssl->transform_out->ivlen,
1664 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001667 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001668 ssl->transform_out->ivlen,
1669 enc_msg, enc_msglen,
1670 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001673 return( ret );
1674 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001675
Paul Bakkercca5b812013-08-31 17:40:26 +02001676 if( enc_msglen != olen )
1677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1679 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001680 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1683 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001684 {
1685 /*
1686 * Save IV in SSL3 and TLS1
1687 */
1688 memcpy( ssl->transform_out->iv_enc,
1689 ssl->transform_out->cipher_ctx_enc.iv,
1690 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001692#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001695 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001696 {
1697 /*
1698 * MAC(MAC_write_key, seq_num +
1699 * TLSCipherText.type +
1700 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001701 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001702 * IV + // except for TLS 1.0
1703 * ENC(content + padding + padding_length));
1704 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001705 unsigned char pseudo_hdr[13];
1706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001708
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001709 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1710 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001711 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1712 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1717 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001718 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001720 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001722
1723 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001724 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001728 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001730 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001734 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001736 /* Make extra sure authentication was performed, exactly once */
1737 if( auth_done != 1 )
1738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001741 }
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 return( 0 );
1746}
1747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001752#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001753 size_t padlen = 0, correct = 1;
1754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762 }
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765
Paul Bakker48916f92012-09-16 19:57:18 +00001766 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001769 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1774 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001775 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001776 int ret;
1777 size_t olen = 0;
1778
Paul Bakker68884e32013-01-07 18:20:04 +01001779 padlen = 0;
1780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001782 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001783 ssl->transform_in->ivlen,
1784 ssl->in_msg, ssl->in_msglen,
1785 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001788 return( ret );
1789 }
1790
1791 if( ssl->in_msglen != olen )
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 }
Paul Bakker68884e32013-01-07 18:20:04 +01001797 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001799#if defined(MBEDTLS_GCM_C) || \
1800 defined(MBEDTLS_CCM_C) || \
1801 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803 mode == MBEDTLS_MODE_CCM ||
1804 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001805 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001806 int ret;
1807 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001808 unsigned char *dec_msg;
1809 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001810 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001811 unsigned char iv[12];
1812 mbedtls_ssl_transform *transform = ssl->transform_in;
1813 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001815 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001816
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001817 /*
1818 * Compute and update sizes
1819 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001820 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001823 "+ taglen (%d)", ssl->in_msglen,
1824 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001826 }
1827 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1828
Paul Bakker68884e32013-01-07 18:20:04 +01001829 dec_msg = ssl->in_msg;
1830 dec_msg_result = ssl->in_msg;
1831 ssl->in_msglen = dec_msglen;
1832
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001833 /*
1834 * Prepare additional authenticated data
1835 */
Paul Bakker68884e32013-01-07 18:20:04 +01001836 memcpy( add_data, ssl->in_ctr, 8 );
1837 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001839 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001840 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1841 add_data[12] = ssl->in_msglen & 0xFF;
1842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001843 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001844
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001845 /*
1846 * Prepare IV
1847 */
1848 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1849 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001850 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1852 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001853
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001854 }
1855 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1856 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001857 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001858 unsigned char i;
1859
1860 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1861
1862 for( i = 0; i < 8; i++ )
1863 iv[i+4] ^= ssl->in_ctr[i];
1864 }
1865 else
1866 {
1867 /* Reminder if we ever add an AEAD mode with a different size */
1868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1869 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1870 }
1871
1872 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001874
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001875 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001876 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001879 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001880 add_data, 13,
1881 dec_msg, dec_msglen,
1882 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001883 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1888 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001889
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001890 return( ret );
1891 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001892 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001893
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001894 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001898 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1902#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001903 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 {
Paul Bakker45829992013-01-03 14:52:21 +01001906 /*
1907 * Decrypt and check the padding
1908 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001909 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001910 unsigned char *dec_msg;
1911 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001912 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001913 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001914 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001915
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 /*
Paul Bakker45829992013-01-03 14:52:21 +01001917 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1920 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001921 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001922#endif
Paul Bakker45829992013-01-03 14:52:21 +01001923
1924 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1925 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001928 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1929 ssl->transform_in->ivlen,
1930 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001932 }
1933
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001934 dec_msglen = ssl->in_msglen;
1935 dec_msg = ssl->in_msg;
1936 dec_msg_result = ssl->in_msg;
1937
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001938 /*
1939 * Authenticate before decrypt if enabled
1940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1942 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001943 {
Hanno Becker992b6872017-11-09 18:57:39 +00001944 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001945 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001948
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001949 dec_msglen -= ssl->transform_in->maclen;
1950 ssl->in_msglen -= ssl->transform_in->maclen;
1951
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001952 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1953 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1954 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1955 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1960 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001961 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001962 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001967 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968 ssl->transform_in->maclen );
1969
Hanno Becker992b6872017-11-09 18:57:39 +00001970 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1971 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001977 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980
1981 /*
1982 * Check length sanity
1983 */
1984 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001987 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001989 }
1990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001992 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001993 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001997 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001998 dec_msglen -= ssl->transform_in->ivlen;
1999 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000
Paul Bakker48916f92012-09-16 19:57:18 +00002001 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002002 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002007 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002008 ssl->transform_in->ivlen,
2009 dec_msg, dec_msglen,
2010 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002013 return( ret );
2014 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002015
Paul Bakkercca5b812013-08-31 17:40:26 +02002016 if( dec_msglen != olen )
2017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002020 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2023 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 {
2025 /*
2026 * Save IV in SSL3 and TLS1
2027 */
2028 memcpy( ssl->transform_in->iv_dec,
2029 ssl->transform_in->cipher_ctx_dec.iv,
2030 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
2034 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002035
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002036 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002037 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_SSL_DEBUG_ALL)
2040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002041 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002042#endif
Paul Bakker45829992013-01-03 14:52:21 +01002043 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002044 correct = 0;
2045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#if defined(MBEDTLS_SSL_PROTO_SSL3)
2048 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 {
Paul Bakker48916f92012-09-16 19:57:18 +00002050 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_SSL_DEBUG_ALL)
2053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002055 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002056#endif
Paul Bakker45829992013-01-03 14:52:21 +01002057 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
2059 }
2060 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2063 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2064 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 {
2066 /*
Paul Bakker45829992013-01-03 14:52:21 +01002067 * TLSv1+: always check the padding up to the first failure
2068 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002070 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002071 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002072 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002073
Paul Bakker956c9e02013-12-19 14:42:28 +01002074 /*
2075 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002076 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002077 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002078 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002079 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002080 *
2081 * In both cases we reset padding_idx to a safe value (0) to
2082 * prevent out-of-buffer reads.
2083 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002084 correct &= ( padlen <= ssl->in_msglen );
2085 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002086 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002087
2088 padding_idx *= correct;
2089
Angus Grattonb512bc12018-06-19 15:57:50 +10002090 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002091 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002092 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002093 pad_count += real_count *
2094 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2095 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002096
2097 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002100 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002102#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002103 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2107 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002111 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002112
2113 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002117 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002123#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002127
2128 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002129 * Authenticate if not done yet.
2130 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002132#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002134 {
Hanno Becker992b6872017-11-09 18:57:39 +00002135 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002136
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002137 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002139 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2140 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142#if defined(MBEDTLS_SSL_PROTO_SSL3)
2143 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002144 {
2145 ssl_mac( &ssl->transform_in->md_ctx_dec,
2146 ssl->transform_in->mac_dec,
2147 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002148 ssl->in_ctr, ssl->in_msgtype,
2149 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002150 }
2151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2153#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2154 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2155 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002156 {
2157 /*
2158 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002159 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 *
2161 * Known timing attacks:
2162 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2163 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002164 * To compensate for different timings for the MAC calculation
2165 * depending on how much padding was removed (which is determined
2166 * by padlen), process extra_run more blocks through the hash
2167 * function.
2168 *
2169 * The formula in the paper is
2170 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2171 * where L1 is the size of the header plus the decrypted message
2172 * plus CBC padding and L2 is the size of the header plus the
2173 * decrypted message. This is for an underlying hash function
2174 * with 64-byte blocks.
2175 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2176 * correctly. We round down instead of up, so -56 is the correct
2177 * value for our calculations instead of -55.
2178 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002179 * Repeat the formula rather than defining a block_size variable.
2180 * This avoids requiring division by a variable at runtime
2181 * (which would be marginally less efficient and would require
2182 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002183 */
2184 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002185
2186 /*
2187 * The next two sizes are the minimum and maximum values of
2188 * in_msglen over all padlen values.
2189 *
2190 * They're independent of padlen, since we previously did
2191 * in_msglen -= padlen.
2192 *
2193 * Note that max_len + maclen is never more than the buffer
2194 * length, as we previously did in_msglen -= maclen too.
2195 */
2196 const size_t max_len = ssl->in_msglen + padlen;
2197 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2198
Gilles Peskine20b44082018-05-29 14:06:49 +02002199 switch( ssl->transform_in->ciphersuite_info->mac )
2200 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002201#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2202 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002203 case MBEDTLS_MD_MD5:
2204 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002205 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002206 /* 8 bytes of message size, 64-byte compression blocks */
2207 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2208 ( 13 + ssl->in_msglen + 8 ) / 64;
2209 break;
2210#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002211#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002212 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002213 /* 16 bytes of message size, 128-byte compression blocks */
2214 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2215 ( 13 + ssl->in_msglen + 16 ) / 128;
2216 break;
2217#endif
2218 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2221 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002222
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002223 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2226 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2227 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2228 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002229 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002230 /* Make sure we access everything even when padlen > 0. This
2231 * makes the synchronisation requirements for just-in-time
2232 * Prime+Probe attacks much tighter and hopefully impractical. */
2233 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002234 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002235
2236 /* Call mbedtls_md_process at least once due to cache attacks
2237 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002238 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002242
2243 /* Make sure we access all the memory that could contain the MAC,
2244 * before we check it in the next code block. This makes the
2245 * synchronisation requirements for just-in-time Prime+Probe
2246 * attacks much tighter and hopefully impractical. */
2247 ssl_read_memory( ssl->in_msg + min_len,
2248 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002249 }
2250 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2252 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002258#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002259 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2260 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2261 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Hanno Becker992b6872017-11-09 18:57:39 +00002264 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2265 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267#if defined(MBEDTLS_SSL_DEBUG_ALL)
2268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002269#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002270 correct = 0;
2271 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002272 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002274 /*
2275 * Finally check the correct flag
2276 */
2277 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002279 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002280#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281
2282 /* Make extra sure authentication was performed, exactly once */
2283 if( auth_done != 1 )
2284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
2289 if( ssl->in_msglen == 0 )
2290 {
Angus Gratton34817922018-06-19 15:58:22 +10002291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2292 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2293 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2294 {
2295 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2297 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2298 }
2299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2300
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 ssl->nb_zero++;
2302
2303 /*
2304 * Three or more empty messages may be a DoS attack
2305 * (excessive CPU consumption).
2306 */
2307 if( ssl->nb_zero > 3 )
2308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 }
2313 }
2314 else
2315 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002319 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002320 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002321 }
2322 else
2323#endif
2324 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002326 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2327 if( ++ssl->in_ctr[i - 1] != 0 )
2328 break;
2329
2330 /* The loop goes to its end iff the counter is wrapping */
2331 if( i == ssl_ep_len( ssl ) )
2332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2334 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002335 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002336 }
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
2340 return( 0 );
2341}
2342
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002343#undef MAC_NONE
2344#undef MAC_PLAINTEXT
2345#undef MAC_CIPHERTEXT
2346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348/*
2349 * Compression/decompression functions
2350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352{
2353 int ret;
2354 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002355 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002357 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002361 if( len_pre == 0 )
2362 return( 0 );
2363
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364 memcpy( msg_pre, ssl->out_msg, len_pre );
2365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002367 ssl->out_msglen ) );
2368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370 ssl->out_msg, ssl->out_msglen );
2371
Paul Bakker48916f92012-09-16 19:57:18 +00002372 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2373 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2374 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002375 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002376
Paul Bakker48916f92012-09-16 19:57:18 +00002377 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378 if( ret != Z_OK )
2379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2381 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 }
2383
Angus Grattond8213d02016-05-25 20:56:48 +10002384 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002385 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 ssl->out_msglen ) );
2389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391 ssl->out_msg, ssl->out_msglen );
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394
2395 return( 0 );
2396}
2397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399{
2400 int ret;
2401 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002402 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002404 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002407
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002408 if( len_pre == 0 )
2409 return( 0 );
2410
Paul Bakker2770fbd2012-07-03 13:30:23 +00002411 memcpy( msg_pre, ssl->in_msg, len_pre );
2412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002414 ssl->in_msglen ) );
2415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417 ssl->in_msg, ssl->in_msglen );
2418
Paul Bakker48916f92012-09-16 19:57:18 +00002419 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2420 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2421 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002422 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002423 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424
Paul Bakker48916f92012-09-16 19:57:18 +00002425 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002426 if( ret != Z_OK )
2427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2429 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002430 }
2431
Angus Grattond8213d02016-05-25 20:56:48 +10002432 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002433 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436 ssl->in_msglen ) );
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002439 ssl->in_msg, ssl->in_msglen );
2440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002442
2443 return( 0 );
2444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2448static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#if defined(MBEDTLS_SSL_PROTO_DTLS)
2451static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002452{
2453 /* If renegotiation is not enforced, retransmit until we would reach max
2454 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002455 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002456 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002457 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002458 unsigned char doublings = 1;
2459
2460 while( ratio != 0 )
2461 {
2462 ++doublings;
2463 ratio >>= 1;
2464 }
2465
2466 if( ++ssl->renego_records_seen > doublings )
2467 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002469 return( 0 );
2470 }
2471 }
2472
2473 return( ssl_write_hello_request( ssl ) );
2474}
2475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002477
Paul Bakker5121ce52009-01-03 21:22:43 +00002478/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002479 * Fill the input message buffer by appending data to it.
2480 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002481 *
2482 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2483 * available (from this read and/or a previous one). Otherwise, an error code
2484 * is returned (possibly EOF or WANT_READ).
2485 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002486 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2487 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2488 * since we always read a whole datagram at once.
2489 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002490 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002491 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002494{
Paul Bakker23986e52011-04-24 08:57:21 +00002495 int ret;
2496 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002500 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002503 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002505 }
2506
Angus Grattond8213d02016-05-25 20:56:48 +10002507 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002511 }
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002516 uint32_t timeout;
2517
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002518 /* Just to be sure */
2519 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2520 {
2521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2522 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2524 }
2525
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002526 /*
2527 * The point is, we need to always read a full datagram at once, so we
2528 * sometimes read more then requested, and handle the additional data.
2529 * It could be the rest of the current record (while fetching the
2530 * header) and/or some other records in the same datagram.
2531 */
2532
2533 /*
2534 * Move to the next record in the already read datagram if applicable
2535 */
2536 if( ssl->next_record_offset != 0 )
2537 {
2538 if( ssl->in_left < ssl->next_record_offset )
2539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002542 }
2543
2544 ssl->in_left -= ssl->next_record_offset;
2545
2546 if( ssl->in_left != 0 )
2547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002549 ssl->next_record_offset ) );
2550 memmove( ssl->in_hdr,
2551 ssl->in_hdr + ssl->next_record_offset,
2552 ssl->in_left );
2553 }
2554
2555 ssl->next_record_offset = 0;
2556 }
2557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002560
2561 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002562 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002563 */
2564 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002567 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002568 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002569
2570 /*
2571 * A record can't be split accross datagrams. If we need to read but
2572 * are not at the beginning of a new record, the caller did something
2573 * wrong.
2574 */
2575 if( ssl->in_left != 0 )
2576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002579 }
2580
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002581 /*
2582 * Don't even try to read if time's out already.
2583 * This avoids by-passing the timer when repeatedly receiving messages
2584 * that will end up being dropped.
2585 */
2586 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002587 {
2588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002589 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002590 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002591 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002592 {
Angus Grattond8213d02016-05-25 20:56:48 +10002593 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002596 timeout = ssl->handshake->retransmit_timeout;
2597 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002598 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002601
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002602 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002603 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2604 timeout );
2605 else
2606 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609
2610 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002612 }
2613
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002614 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002617 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002620 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002621 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002624 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002625 }
2626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002630 return( ret );
2631 }
2632
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002633 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002636 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002638 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002639 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002642 return( ret );
2643 }
2644
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002645 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002648 }
2649
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 if( ret < 0 )
2651 return( ret );
2652
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002653 ssl->in_left = ret;
2654 }
2655 else
2656#endif
2657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002659 ssl->in_left, nb_want ) );
2660
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002661 while( ssl->in_left < nb_want )
2662 {
2663 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002664
2665 if( ssl_check_timer( ssl ) != 0 )
2666 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2667 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002668 {
2669 if( ssl->f_recv_timeout != NULL )
2670 {
2671 ret = ssl->f_recv_timeout( ssl->p_bio,
2672 ssl->in_hdr + ssl->in_left, len,
2673 ssl->conf->read_timeout );
2674 }
2675 else
2676 {
2677 ret = ssl->f_recv( ssl->p_bio,
2678 ssl->in_hdr + ssl->in_left, len );
2679 }
2680 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002683 ssl->in_left, nb_want ) );
2684 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002685
2686 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002688
2689 if( ret < 0 )
2690 return( ret );
2691
mohammad160352aecb92018-03-28 23:41:40 -07002692 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002693 {
Darryl Green11999bb2018-03-13 15:22:58 +00002694 MBEDTLS_SSL_DEBUG_MSG( 1,
2695 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002696 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002697 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2698 }
2699
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002700 ssl->in_left += ret;
2701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
2706 return( 0 );
2707}
2708
2709/*
2710 * Flush any data not yet written
2711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002714 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002715 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002719 if( ssl->f_send == NULL )
2720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002722 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002724 }
2725
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002726 /* Avoid incrementing counter if data is flushed */
2727 if( ssl->out_left == 0 )
2728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002730 return( 0 );
2731 }
2732
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 while( ssl->out_left > 0 )
2734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2736 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Hanno Becker2b1e3542018-08-06 11:19:13 +01002738 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002739 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
2743 if( ret <= 0 )
2744 return( ret );
2745
mohammad160352aecb92018-03-28 23:41:40 -07002746 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002747 {
Darryl Green11999bb2018-03-13 15:22:58 +00002748 MBEDTLS_SSL_DEBUG_MSG( 1,
2749 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002750 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2752 }
2753
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 ssl->out_left -= ret;
2755 }
2756
Hanno Becker2b1e3542018-08-06 11:19:13 +01002757#if defined(MBEDTLS_SSL_PROTO_DTLS)
2758 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2759 {
2760 ssl->out_hdr = ssl->out_buf;
2761 }
2762 else
2763#endif
2764 {
2765 ssl->out_hdr = ssl->out_buf + 8;
2766 }
2767 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
2771 return( 0 );
2772}
2773
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002774/*
2775 * Functions to handle the DTLS retransmission state machine
2776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002778/*
2779 * Append current handshake message to current outgoing flight
2780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002782{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2785 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2786 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002787
2788 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002789 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002790 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002793 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794 }
2795
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002796 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002797 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002800 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002801 }
2802
2803 /* Copy current handshake message with headers */
2804 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2805 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002806 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002807 msg->next = NULL;
2808
2809 /* Append to the current flight */
2810 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002811 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002812 else
2813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002815 while( cur->next != NULL )
2816 cur = cur->next;
2817 cur->next = msg;
2818 }
2819
Hanno Becker3b235902018-08-06 09:54:53 +01002820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821 return( 0 );
2822}
2823
2824/*
2825 * Free the current flight of handshake messages
2826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 mbedtls_ssl_flight_item *cur = flight;
2830 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831
2832 while( cur != NULL )
2833 {
2834 next = cur->next;
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 mbedtls_free( cur->p );
2837 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838
2839 cur = next;
2840 }
2841}
2842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2844static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002845#endif
2846
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002847/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002848 * Swap transform_out and out_ctr with the alternative ones
2849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002853 unsigned char tmp_out_ctr[8];
2854
2855 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002858 return;
2859 }
2860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002862
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002863 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002864 tmp_transform = ssl->transform_out;
2865 ssl->transform_out = ssl->handshake->alt_transform_out;
2866 ssl->handshake->alt_transform_out = tmp_transform;
2867
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002868 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002869 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2870 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002871 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872
2873 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002874 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2877 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002883 }
2884 }
2885#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002886}
2887
2888/*
2889 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002890 */
2891int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2892{
2893 int ret = 0;
2894
2895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2896
2897 ret = mbedtls_ssl_flight_transmit( ssl );
2898
2899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2900
2901 return( ret );
2902}
2903
2904/*
2905 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002906 *
2907 * Need to remember the current message in case flush_output returns
2908 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002909 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002910 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002911int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002912{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002913 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002917 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002919
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002920 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002921 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002922 ssl_swap_epochs( ssl );
2923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002925 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002926
2927 while( ssl->handshake->cur_msg != NULL )
2928 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002929 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002930 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002931
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002932 /* Swap epochs before sending Finished: we can't do it after
2933 * sending ChangeCipherSpec, in case write returns WANT_READ.
2934 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker67bc7c32018-08-06 11:33:50 +01002936 cur->p[0] == MBEDTLS_SSL_HS_FINISHED &&
2937 ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002938 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002940 ssl_swap_epochs( ssl );
2941 }
2942
Hanno Becker67bc7c32018-08-06 11:33:50 +01002943 ret = ssl_get_remaining_payload_in_datagram( ssl );
2944 if( ret < 0 )
2945 return( ret );
2946 max_frag_len = (size_t) ret;
2947
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002948 /* CCS is copied as is, while HS messages may need fragmentation */
2949 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2950 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002951 if( max_frag_len == 0 )
2952 {
2953 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2954 return( ret );
2955
2956 continue;
2957 }
2958
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002959 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002960 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002961 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002962
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002963 /* Update position inside current message */
2964 ssl->handshake->cur_msg_p += cur->len;
2965 }
2966 else
2967 {
2968 const unsigned char * const p = ssl->handshake->cur_msg_p;
2969 const size_t hs_len = cur->len - 12;
2970 const size_t frag_off = p - ( cur->p + 12 );
2971 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002972 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002973
Hanno Becker67bc7c32018-08-06 11:33:50 +01002974 if( max_frag_len < 12 )
2975 {
2976 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2977 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
2978 {
2979 ssl_swap_epochs( ssl );
2980 }
2981
2982 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2983 return( ret );
2984
2985 continue;
2986 }
2987 max_hs_frag_len = max_frag_len - 12;
2988
2989 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2990 max_hs_frag_len : rem_len;
2991
2992 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002993 {
2994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002995 (unsigned) cur_hs_frag_len,
2996 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002997 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002998
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002999 /* Messages are stored with handshake headers as if not fragmented,
3000 * copy beginning of headers then fill fragmentation fields.
3001 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3002 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003003
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003004 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3005 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3006 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3007
Hanno Becker67bc7c32018-08-06 11:33:50 +01003008 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3009 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3010 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003011
3012 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3013
Hanno Becker67bc7c32018-08-06 11:33:50 +01003014 /* Copy the handshame message content and set records fields */
3015 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3016 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003017 ssl->out_msgtype = cur->type;
3018
3019 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003020 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003021 }
3022
3023 /* If done with the current message move to the next one if any */
3024 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3025 {
3026 if( cur->next != NULL )
3027 {
3028 ssl->handshake->cur_msg = cur->next;
3029 ssl->handshake->cur_msg_p = cur->next->p + 12;
3030 }
3031 else
3032 {
3033 ssl->handshake->cur_msg = NULL;
3034 ssl->handshake->cur_msg_p = NULL;
3035 }
3036 }
3037
3038 /* Actually send the message out */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003039 if( ( ret = mbedtls_ssl_write_record( ssl,
3040 SSL_DONT_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003043 return( ret );
3044 }
3045 }
3046
Hanno Becker67bc7c32018-08-06 11:33:50 +01003047 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3048 return( ret );
3049
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003050 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3052 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003053 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003056 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3057 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003058
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003060
3061 return( 0 );
3062}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003063
3064/*
3065 * To be called when the last message of an incoming flight is received.
3066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003068{
3069 /* We won't need to resend that one any more */
3070 ssl_flight_free( ssl->handshake->flight );
3071 ssl->handshake->flight = NULL;
3072 ssl->handshake->cur_msg = NULL;
3073
3074 /* The next incoming flight will start with this msg_seq */
3075 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3076
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003077 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003078 ssl_set_timer( ssl, 0 );
3079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3081 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003084 }
3085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003087}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003088
3089/*
3090 * To be called when the last message of an outgoing flight is send.
3091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003093{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003094 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003095 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3098 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003101 }
3102 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003106
Paul Bakker5121ce52009-01-03 21:22:43 +00003107/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003108 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003110
3111/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003112 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003113 *
3114 * - fill in handshake headers
3115 * - update handshake checksum
3116 * - DTLS: save message for resending
3117 * - then pass to the record layer
3118 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003119 * DTLS: except for HelloRequest, messages are only queued, and will only be
3120 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003121 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003122 * Inputs:
3123 * - ssl->out_msglen: 4 + actual handshake message len
3124 * (4 is the size of handshake headers for TLS)
3125 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3126 * - ssl->out_msg + 4: the handshake message body
3127 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003128 * Ouputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003129 * - ssl->out_msglen: the length of the record contents
3130 * (including handshake headers but excluding record headers)
3131 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003132 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003133int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003134{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003135 int ret;
3136 const size_t hs_len = ssl->out_msglen - 4;
3137 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3140
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003141 /*
3142 * Sanity checks
3143 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003144 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3145 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3146 {
3147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003151 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3152 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3153 ssl->handshake == NULL )
3154 {
3155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3157 }
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003161 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003166 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003167#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003168
3169 /*
3170 * Fill handshake headers
3171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003174 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3175 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3176 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003178 /*
3179 * DTLS has additional fields in the Handshake layer,
3180 * between the length field and the actual payload:
3181 * uint16 message_seq;
3182 * uint24 fragment_offset;
3183 * uint24 fragment_length;
3184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003187 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003188 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003189 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003190 {
3191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3192 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003193 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003194 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3196 }
3197
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003198 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003199 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003200
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003201 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003202 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003203 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003204 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3205 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3206 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003207 }
3208 else
3209 {
3210 ssl->out_msg[4] = 0;
3211 ssl->out_msg[5] = 0;
3212 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003213
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003214 /* Handshake hashes are computed without fragmentation,
3215 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003216 memset( ssl->out_msg + 6, 0x00, 3 );
3217 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003220
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003221 /* Update running hashes of hanshake messages seen */
3222 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3223 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 }
3225
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003226 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003228 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003229 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003230 {
3231 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003234 return( ret );
3235 }
3236 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003237 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003238#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003239 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003240 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003241 {
3242 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3243 return( ret );
3244 }
3245 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003246
3247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3248
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003249 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003250}
3251
3252/*
3253 * Record layer functions
3254 */
3255
3256/*
3257 * Write current record.
3258 *
3259 * Uses:
3260 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3261 * - ssl->out_msglen: length of the record content (excl headers)
3262 * - ssl->out_msg: record content
3263 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003264int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003265{
3266 int ret, done = 0;
3267 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003268 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003269
3270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003273 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003275 {
3276 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003279 return( ret );
3280 }
3281
3282 len = ssl->out_msglen;
3283 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3287 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 ret = mbedtls_ssl_hw_record_write( ssl );
3292 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3295 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003296 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003297
3298 if( ret == 0 )
3299 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003302 if( !done )
3303 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003304 unsigned i;
3305 size_t protected_record_size;
3306
Paul Bakker05ef8352012-05-08 09:17:57 +00003307 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003309 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003310
Hanno Becker19859472018-08-06 09:40:20 +01003311 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003312 ssl->out_len[0] = (unsigned char)( len >> 8 );
3313 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003314
Paul Bakker48916f92012-09-16 19:57:18 +00003315 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003316 {
3317 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003320 return( ret );
3321 }
3322
3323 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003324 ssl->out_len[0] = (unsigned char)( len >> 8 );
3325 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003326 }
3327
Hanno Becker2b1e3542018-08-06 11:19:13 +01003328 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3329
3330#if defined(MBEDTLS_SSL_PROTO_DTLS)
3331 /* In case of DTLS, double-check that we don't exceed
3332 * the remaining space in the datagram. */
3333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3334 {
3335 ret = ssl_get_maximum_datagram_size( ssl );
3336 if( ret < 0 )
3337 return( ret );
3338
3339 if( protected_record_size > (size_t) ret )
3340 {
3341 /* Should never happen */
3342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3343 }
3344 }
3345#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003348 "version = [%d:%d], msglen = %d",
3349 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3350
Paul Bakker05ef8352012-05-08 09:17:57 +00003351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003353 ssl->out_hdr, protected_record_size );
3354
3355 ssl->out_left += protected_record_size;
3356 ssl->out_hdr += protected_record_size;
3357 ssl_update_out_pointers( ssl, ssl->transform_out );
3358
Hanno Becker04484622018-08-06 09:49:38 +01003359 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3360 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3361 break;
3362
3363 /* The loop goes to its end iff the counter is wrapping */
3364 if( i == ssl_ep_len( ssl ) )
3365 {
3366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3367 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 }
3370
Hanno Becker67bc7c32018-08-06 11:33:50 +01003371#if defined(MBEDTLS_SSL_PROTO_DTLS)
3372 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3373 {
3374 size_t remaining = ssl_get_remaining_payload_in_datagram( ssl );
3375 if( remaining == 0 )
3376 flush = SSL_FORCE_FLUSH;
3377 else
3378 {
3379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Stil %u bytes available in current datagram", (unsigned) remaining ) );
3380 }
3381 }
3382#endif /* MBEDTLS_SSL_PROTO_DTLS */
3383
3384 if( ( flush == SSL_FORCE_FLUSH ) &&
3385 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 return( ret );
3389 }
3390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
3393 return( 0 );
3394}
3395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003397/*
3398 * Mark bits in bitmask (used for DTLS HS reassembly)
3399 */
3400static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3401{
3402 unsigned int start_bits, end_bits;
3403
3404 start_bits = 8 - ( offset % 8 );
3405 if( start_bits != 8 )
3406 {
3407 size_t first_byte_idx = offset / 8;
3408
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003409 /* Special case */
3410 if( len <= start_bits )
3411 {
3412 for( ; len != 0; len-- )
3413 mask[first_byte_idx] |= 1 << ( start_bits - len );
3414
3415 /* Avoid potential issues with offset or len becoming invalid */
3416 return;
3417 }
3418
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003419 offset += start_bits; /* Now offset % 8 == 0 */
3420 len -= start_bits;
3421
3422 for( ; start_bits != 0; start_bits-- )
3423 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3424 }
3425
3426 end_bits = len % 8;
3427 if( end_bits != 0 )
3428 {
3429 size_t last_byte_idx = ( offset + len ) / 8;
3430
3431 len -= end_bits; /* Now len % 8 == 0 */
3432
3433 for( ; end_bits != 0; end_bits-- )
3434 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3435 }
3436
3437 memset( mask + offset / 8, 0xFF, len / 8 );
3438}
3439
3440/*
3441 * Check that bitmask is full
3442 */
3443static int ssl_bitmask_check( unsigned char *mask, size_t len )
3444{
3445 size_t i;
3446
3447 for( i = 0; i < len / 8; i++ )
3448 if( mask[i] != 0xFF )
3449 return( -1 );
3450
3451 for( i = 0; i < len % 8; i++ )
3452 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3453 return( -1 );
3454
3455 return( 0 );
3456}
3457
3458/*
3459 * Reassemble fragmented DTLS handshake messages.
3460 *
3461 * Use a temporary buffer for reassembly, divided in two parts:
3462 * - the first holds the reassembled message (including handshake header),
3463 * - the second holds a bitmask indicating which parts of the message
3464 * (excluding headers) have been received so far.
3465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003467{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003468 unsigned char *msg, *bitmask;
3469 size_t frag_len, frag_off;
3470 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3471
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003472 if( ssl->handshake == NULL )
3473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3475 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003476 }
3477
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003478 /*
3479 * For first fragment, check size and allocate buffer
3480 */
3481 if( ssl->handshake->hs_msg == NULL )
3482 {
3483 size_t alloc_len;
3484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003486 msg_len ) );
3487
Angus Grattond8213d02016-05-25 20:56:48 +10003488 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3491 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003492 }
3493
3494 /* The bitmask needs one bit per byte of message excluding header */
3495 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3496
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003497 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003498 if( ssl->handshake->hs_msg == NULL )
3499 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003501 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003502 }
3503
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003504 /* Prepare final header: copy msg_type, length and message_seq,
3505 * then add standardised fragment_offset and fragment_length */
3506 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3507 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3508 memcpy( ssl->handshake->hs_msg + 9,
3509 ssl->handshake->hs_msg + 1, 3 );
3510 }
3511 else
3512 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003513 /* Make sure msg_type and length are consistent */
3514 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3517 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003518 }
3519 }
3520
3521 msg = ssl->handshake->hs_msg + 12;
3522 bitmask = msg + msg_len;
3523
3524 /*
3525 * Check and copy current fragment
3526 */
3527 frag_off = ( ssl->in_msg[6] << 16 ) |
3528 ( ssl->in_msg[7] << 8 ) |
3529 ssl->in_msg[8];
3530 frag_len = ( ssl->in_msg[9] << 16 ) |
3531 ( ssl->in_msg[10] << 8 ) |
3532 ssl->in_msg[11];
3533
3534 if( frag_off + frag_len > msg_len )
3535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003537 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003539 }
3540
3541 if( frag_len + 12 > ssl->in_msglen )
3542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003544 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003546 }
3547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003549 frag_off, frag_len ) );
3550
3551 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3552 ssl_bitmask_set( bitmask, frag_off, frag_len );
3553
3554 /*
3555 * Do we have the complete message by now?
3556 * If yes, finalize it, else ask to read the next record.
3557 */
3558 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003561 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003562 }
3563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003565
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003566 if( frag_len + 12 < ssl->in_msglen )
3567 {
3568 /*
3569 * We'got more handshake messages in the same record.
3570 * This case is not handled now because no know implementation does
3571 * that and it's hard to test, so we prefer to fail cleanly for now.
3572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3574 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003575 }
3576
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003577 if( ssl->in_left > ssl->next_record_offset )
3578 {
3579 /*
3580 * We've got more data in the buffer after the current record,
3581 * that we don't want to overwrite. Move it before writing the
3582 * reassembled message, and adjust in_left and next_record_offset.
3583 */
3584 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3585 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3586 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3587
3588 /* First compute and check new lengths */
3589 ssl->next_record_offset = new_remain - ssl->in_hdr;
3590 ssl->in_left = ssl->next_record_offset + remain_len;
3591
Angus Grattond8213d02016-05-25 20:56:48 +10003592 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003593 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3596 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003597 }
3598
3599 memmove( new_remain, cur_remain, remain_len );
3600 }
3601
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003602 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003605 ssl->handshake->hs_msg = NULL;
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003608 ssl->in_msg, ssl->in_hslen );
3609
3610 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003613
Simon Butcher99000142016-10-13 17:21:01 +01003614int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003619 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003621 }
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003624 ( ssl->in_msg[1] << 16 ) |
3625 ( ssl->in_msg[2] << 8 ) |
3626 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003629 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003630 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003633 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003634 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003635 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003636 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003637
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003638 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003639 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3640 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3641 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3642 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003643 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003644 /* Retransmit only on last message from previous flight, to avoid
3645 * too many retransmissions.
3646 * Besides, No sane server ever retransmits HelloVerifyRequest */
3647 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003651 "message_seq = %d, start_of_flight = %d",
3652 recv_msg_seq,
3653 ssl->handshake->in_flight_start_seq ) );
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003658 return( ret );
3659 }
3660 }
3661 else
3662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003664 "message_seq = %d, expected = %d",
3665 recv_msg_seq,
3666 ssl->handshake->in_msg_seq ) );
3667 }
3668
Hanno Becker90333da2017-10-10 11:27:13 +01003669 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003670 }
3671 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003672
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003673 /* Reassemble if current message is fragmented or reassembly is
3674 * already in progress */
3675 if( ssl->in_msglen < ssl->in_hslen ||
3676 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3677 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3678 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003681
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003682 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003685 return( ret );
3686 }
3687 }
3688 }
3689 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003691 /* With TLS we don't handle fragmentation (for now) */
3692 if( ssl->in_msglen < ssl->in_hslen )
3693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3695 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003696 }
3697
Simon Butcher99000142016-10-13 17:21:01 +01003698 return( 0 );
3699}
3700
3701void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3702{
3703
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003704 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3705 ssl->handshake != NULL )
3706 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003707 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003708 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003709
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003710 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003712 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003713 ssl->handshake != NULL )
3714 {
3715 ssl->handshake->in_msg_seq++;
3716 }
3717#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003718}
3719
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003720/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003721 * DTLS anti-replay: RFC 6347 4.1.2.6
3722 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003723 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3724 * Bit n is set iff record number in_window_top - n has been seen.
3725 *
3726 * Usually, in_window_top is the last record number seen and the lsb of
3727 * in_window is set. The only exception is the initial state (record number 0
3728 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3731static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003732{
3733 ssl->in_window_top = 0;
3734 ssl->in_window = 0;
3735}
3736
3737static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3738{
3739 return( ( (uint64_t) buf[0] << 40 ) |
3740 ( (uint64_t) buf[1] << 32 ) |
3741 ( (uint64_t) buf[2] << 24 ) |
3742 ( (uint64_t) buf[3] << 16 ) |
3743 ( (uint64_t) buf[4] << 8 ) |
3744 ( (uint64_t) buf[5] ) );
3745}
3746
3747/*
3748 * Return 0 if sequence number is acceptable, -1 otherwise
3749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003751{
3752 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3753 uint64_t bit;
3754
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003755 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003756 return( 0 );
3757
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003758 if( rec_seqnum > ssl->in_window_top )
3759 return( 0 );
3760
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003761 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003762
3763 if( bit >= 64 )
3764 return( -1 );
3765
3766 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3767 return( -1 );
3768
3769 return( 0 );
3770}
3771
3772/*
3773 * Update replay window on new validated record
3774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003776{
3777 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3778
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003779 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003780 return;
3781
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003782 if( rec_seqnum > ssl->in_window_top )
3783 {
3784 /* Update window_top and the contents of the window */
3785 uint64_t shift = rec_seqnum - ssl->in_window_top;
3786
3787 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003788 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003789 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003790 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003791 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003792 ssl->in_window |= 1;
3793 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003794
3795 ssl->in_window_top = rec_seqnum;
3796 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003797 else
3798 {
3799 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003800 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003801
3802 if( bit < 64 ) /* Always true, but be extra sure */
3803 ssl->in_window |= (uint64_t) 1 << bit;
3804 }
3805}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003807
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003808#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003809/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003810static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3811
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003812/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003813 * Without any SSL context, check if a datagram looks like a ClientHello with
3814 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003815 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003816 *
3817 * - if cookie is valid, return 0
3818 * - if ClientHello looks superficially valid but cookie is not,
3819 * fill obuf and set olen, then
3820 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3821 * - otherwise return a specific error code
3822 */
3823static int ssl_check_dtls_clihlo_cookie(
3824 mbedtls_ssl_cookie_write_t *f_cookie_write,
3825 mbedtls_ssl_cookie_check_t *f_cookie_check,
3826 void *p_cookie,
3827 const unsigned char *cli_id, size_t cli_id_len,
3828 const unsigned char *in, size_t in_len,
3829 unsigned char *obuf, size_t buf_len, size_t *olen )
3830{
3831 size_t sid_len, cookie_len;
3832 unsigned char *p;
3833
3834 if( f_cookie_write == NULL || f_cookie_check == NULL )
3835 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3836
3837 /*
3838 * Structure of ClientHello with record and handshake headers,
3839 * and expected values. We don't need to check a lot, more checks will be
3840 * done when actually parsing the ClientHello - skipping those checks
3841 * avoids code duplication and does not make cookie forging any easier.
3842 *
3843 * 0-0 ContentType type; copied, must be handshake
3844 * 1-2 ProtocolVersion version; copied
3845 * 3-4 uint16 epoch; copied, must be 0
3846 * 5-10 uint48 sequence_number; copied
3847 * 11-12 uint16 length; (ignored)
3848 *
3849 * 13-13 HandshakeType msg_type; (ignored)
3850 * 14-16 uint24 length; (ignored)
3851 * 17-18 uint16 message_seq; copied
3852 * 19-21 uint24 fragment_offset; copied, must be 0
3853 * 22-24 uint24 fragment_length; (ignored)
3854 *
3855 * 25-26 ProtocolVersion client_version; (ignored)
3856 * 27-58 Random random; (ignored)
3857 * 59-xx SessionID session_id; 1 byte len + sid_len content
3858 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3859 * ...
3860 *
3861 * Minimum length is 61 bytes.
3862 */
3863 if( in_len < 61 ||
3864 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3865 in[3] != 0 || in[4] != 0 ||
3866 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3867 {
3868 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3869 }
3870
3871 sid_len = in[59];
3872 if( sid_len > in_len - 61 )
3873 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3874
3875 cookie_len = in[60 + sid_len];
3876 if( cookie_len > in_len - 60 )
3877 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3878
3879 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3880 cli_id, cli_id_len ) == 0 )
3881 {
3882 /* Valid cookie */
3883 return( 0 );
3884 }
3885
3886 /*
3887 * If we get here, we've got an invalid cookie, let's prepare HVR.
3888 *
3889 * 0-0 ContentType type; copied
3890 * 1-2 ProtocolVersion version; copied
3891 * 3-4 uint16 epoch; copied
3892 * 5-10 uint48 sequence_number; copied
3893 * 11-12 uint16 length; olen - 13
3894 *
3895 * 13-13 HandshakeType msg_type; hello_verify_request
3896 * 14-16 uint24 length; olen - 25
3897 * 17-18 uint16 message_seq; copied
3898 * 19-21 uint24 fragment_offset; copied
3899 * 22-24 uint24 fragment_length; olen - 25
3900 *
3901 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3902 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3903 *
3904 * Minimum length is 28.
3905 */
3906 if( buf_len < 28 )
3907 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3908
3909 /* Copy most fields and adapt others */
3910 memcpy( obuf, in, 25 );
3911 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3912 obuf[25] = 0xfe;
3913 obuf[26] = 0xff;
3914
3915 /* Generate and write actual cookie */
3916 p = obuf + 28;
3917 if( f_cookie_write( p_cookie,
3918 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3919 {
3920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3921 }
3922
3923 *olen = p - obuf;
3924
3925 /* Go back and fill length fields */
3926 obuf[27] = (unsigned char)( *olen - 28 );
3927
3928 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3929 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3930 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3931
3932 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3933 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3934
3935 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3936}
3937
3938/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003939 * Handle possible client reconnect with the same UDP quadruplet
3940 * (RFC 6347 Section 4.2.8).
3941 *
3942 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3943 * that looks like a ClientHello.
3944 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003945 * - if the input looks like a ClientHello without cookies,
3946 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003947 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003948 * - if the input looks like a ClientHello with a valid cookie,
3949 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003950 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003951 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003952 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003953 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003954 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3955 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003956 */
3957static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3958{
3959 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003960 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003961
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003962 ret = ssl_check_dtls_clihlo_cookie(
3963 ssl->conf->f_cookie_write,
3964 ssl->conf->f_cookie_check,
3965 ssl->conf->p_cookie,
3966 ssl->cli_id, ssl->cli_id_len,
3967 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003968 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003969
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003970 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3971
3972 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003973 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003974 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003975 * If the error is permanent we'll catch it later,
3976 * if it's not, then hopefully it'll work next time. */
3977 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3978
3979 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003980 }
3981
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003982 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003983 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003984 /* Got a valid cookie, partially reset context */
3985 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3986 {
3987 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3988 return( ret );
3989 }
3990
3991 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003992 }
3993
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003994 return( ret );
3995}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003996#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003997
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003998/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003999 * ContentType type;
4000 * ProtocolVersion version;
4001 * uint16 epoch; // DTLS only
4002 * uint48 sequence_number; // DTLS only
4003 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004004 *
4005 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004006 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004007 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4008 *
4009 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004010 * 1. proceed with the record if this function returns 0
4011 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4012 * 3. return CLIENT_RECONNECT if this function return that value
4013 * 4. drop the whole datagram if this function returns anything else.
4014 * Point 2 is needed when the peer is resending, and we have already received
4015 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004019 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 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 +02004022
Paul Bakker5121ce52009-01-03 21:22:43 +00004023 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004024 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004025 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004029 ssl->in_msgtype,
4030 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004032 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4034 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4035 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4036 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004039
4040#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004041 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4042 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004043 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4044#endif /* MBEDTLS_SSL_PROTO_DTLS */
4045 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4046 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004049 }
4050
4051 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004052 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4055 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 }
4057
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004058 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4061 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 }
4063
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004064 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004065 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004066 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4069 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004070 }
4071
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004072 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004073 * DTLS-related tests.
4074 * Check epoch before checking length constraint because
4075 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4076 * message gets duplicated before the corresponding Finished message,
4077 * the second ChangeCipherSpec should be discarded because it belongs
4078 * to an old epoch, but not because its length is shorter than
4079 * the minimum record length for packets using the new record transform.
4080 * Note that these two kinds of failures are handled differently,
4081 * as an unexpected record is silently skipped but an invalid
4082 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004083 */
4084#if defined(MBEDTLS_SSL_PROTO_DTLS)
4085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4086 {
4087 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4088
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004089 /* Check epoch (and sequence number) with DTLS */
4090 if( rec_epoch != ssl->in_epoch )
4091 {
4092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4093 "expected %d, received %d",
4094 ssl->in_epoch, rec_epoch ) );
4095
4096#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4097 /*
4098 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4099 * access the first byte of record content (handshake type), as we
4100 * have an active transform (possibly iv_len != 0), so use the
4101 * fact that the record header len is 13 instead.
4102 */
4103 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4104 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4105 rec_epoch == 0 &&
4106 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4107 ssl->in_left > 13 &&
4108 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4109 {
4110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4111 "from the same port" ) );
4112 return( ssl_handle_possible_reconnect( ssl ) );
4113 }
4114 else
4115#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4116 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4117 }
4118
4119#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4120 /* Replay detection only works for the current epoch */
4121 if( rec_epoch == ssl->in_epoch &&
4122 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4123 {
4124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4125 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4126 }
4127#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004128
4129 /* Drop unexpected ChangeCipherSpec messages */
4130 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4131 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4132 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4133 {
4134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4135 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4136 }
4137
4138 /* Drop unexpected ApplicationData records,
4139 * except at the beginning of renegotiations */
4140 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4141 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4142#if defined(MBEDTLS_SSL_RENEGOTIATION)
4143 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4144 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4145#endif
4146 )
4147 {
4148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4149 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4150 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004151 }
4152#endif /* MBEDTLS_SSL_PROTO_DTLS */
4153
Hanno Becker52c6dc62017-05-26 16:07:36 +01004154
4155 /* Check length against bounds of the current transform and version */
4156 if( ssl->transform_in == NULL )
4157 {
4158 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004159 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004160 {
4161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4162 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4163 }
4164 }
4165 else
4166 {
4167 if( ssl->in_msglen < ssl->transform_in->minlen )
4168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4170 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4171 }
4172
4173#if defined(MBEDTLS_SSL_PROTO_SSL3)
4174 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004175 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004176 {
4177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4178 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4179 }
4180#endif
4181#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4182 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4183 /*
4184 * TLS encrypted messages can have up to 256 bytes of padding
4185 */
4186 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4187 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004188 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004189 {
4190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4191 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4192 }
4193#endif
4194 }
4195
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004196 return( 0 );
4197}
Paul Bakker5121ce52009-01-03 21:22:43 +00004198
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004199/*
4200 * If applicable, decrypt (and decompress) record content
4201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004203{
4204 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4207 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4210 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 ret = mbedtls_ssl_hw_record_read( ssl );
4215 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4218 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004219 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004220
4221 if( ret == 0 )
4222 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004225 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004226 {
4227 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004230 return( ret );
4231 }
4232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 ssl->in_msg, ssl->in_msglen );
4235
Angus Grattond8213d02016-05-25 20:56:48 +10004236 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4239 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004240 }
4241 }
4242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004244 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004246 {
4247 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004250 return( ret );
4251 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004259 }
4260#endif
4261
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004262 return( 0 );
4263}
4264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004266
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004267/*
4268 * Read a record.
4269 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004270 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4271 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4272 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004275{
4276 int ret;
4277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004279
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004280 if( ssl->keep_current_message == 0 )
4281 {
4282 do {
Simon Butcher99000142016-10-13 17:21:01 +01004283
Hanno Becker90333da2017-10-10 11:27:13 +01004284 do ret = mbedtls_ssl_read_record_layer( ssl );
4285 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4286
4287 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004288 {
4289 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4290 return( ret );
4291 }
4292
4293 ret = mbedtls_ssl_handle_message_type( ssl );
4294
Hanno Becker90333da2017-10-10 11:27:13 +01004295 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4296 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004297
4298 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004299 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004300 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004301 return( ret );
4302 }
4303
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004304 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4305 {
4306 mbedtls_ssl_update_handshake_status( ssl );
4307 }
Simon Butcher99000142016-10-13 17:21:01 +01004308 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004309 else
Simon Butcher99000142016-10-13 17:21:01 +01004310 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4312 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004313 }
4314
4315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4316
4317 return( 0 );
4318}
4319
4320int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4321{
4322 int ret;
4323
Hanno Becker4a810fb2017-05-24 16:27:30 +01004324 /*
4325 * Step A
4326 *
4327 * Consume last content-layer message and potentially
4328 * update in_msglen which keeps track of the contents'
4329 * consumption state.
4330 *
4331 * (1) Handshake messages:
4332 * Remove last handshake message, move content
4333 * and adapt in_msglen.
4334 *
4335 * (2) Alert messages:
4336 * Consume whole record content, in_msglen = 0.
4337 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004338 * (3) Change cipher spec:
4339 * Consume whole record content, in_msglen = 0.
4340 *
4341 * (4) Application data:
4342 * Don't do anything - the record layer provides
4343 * the application data as a stream transport
4344 * and consumes through mbedtls_ssl_read only.
4345 *
4346 */
4347
4348 /* Case (1): Handshake messages */
4349 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004350 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004351 /* Hard assertion to be sure that no application data
4352 * is in flight, as corrupting ssl->in_msglen during
4353 * ssl->in_offt != NULL is fatal. */
4354 if( ssl->in_offt != NULL )
4355 {
4356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4357 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4358 }
4359
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004360 /*
4361 * Get next Handshake message in the current record
4362 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004363
Hanno Becker4a810fb2017-05-24 16:27:30 +01004364 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004365 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004366 * current handshake content: If DTLS handshake
4367 * fragmentation is used, that's the fragment
4368 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004369 * size here is faulty and should be changed at
4370 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004371 * (2) While it doesn't seem to cause problems, one
4372 * has to be very careful not to assume that in_hslen
4373 * is always <= in_msglen in a sensible communication.
4374 * Again, it's wrong for DTLS handshake fragmentation.
4375 * The following check is therefore mandatory, and
4376 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004377 * Additionally, ssl->in_hslen might be arbitrarily out of
4378 * bounds after handling a DTLS message with an unexpected
4379 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004380 */
4381 if( ssl->in_hslen < ssl->in_msglen )
4382 {
4383 ssl->in_msglen -= ssl->in_hslen;
4384 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4385 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004386
Hanno Becker4a810fb2017-05-24 16:27:30 +01004387 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4388 ssl->in_msg, ssl->in_msglen );
4389 }
4390 else
4391 {
4392 ssl->in_msglen = 0;
4393 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004394
Hanno Becker4a810fb2017-05-24 16:27:30 +01004395 ssl->in_hslen = 0;
4396 }
4397 /* Case (4): Application data */
4398 else if( ssl->in_offt != NULL )
4399 {
4400 return( 0 );
4401 }
4402 /* Everything else (CCS & Alerts) */
4403 else
4404 {
4405 ssl->in_msglen = 0;
4406 }
4407
4408 /*
4409 * Step B
4410 *
4411 * Fetch and decode new record if current one is fully consumed.
4412 *
4413 */
4414
4415 if( ssl->in_msglen > 0 )
4416 {
4417 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004418 return( 0 );
4419 }
4420
Hanno Becker4a810fb2017-05-24 16:27:30 +01004421 /* Current record either fully processed or to be discarded. */
4422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004426 return( ret );
4427 }
4428
4429 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004432 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4433 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004434 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004435 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4436 {
4437 /* Skip unexpected record (but not whole datagram) */
4438 ssl->next_record_offset = ssl->in_msglen
4439 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004440
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4442 "(header)" ) );
4443 }
4444 else
4445 {
4446 /* Skip invalid record and the rest of the datagram */
4447 ssl->next_record_offset = 0;
4448 ssl->in_left = 0;
4449
4450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4451 "(header)" ) );
4452 }
4453
4454 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004455 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004456 }
4457#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004458 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004459 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004460
4461 /*
4462 * Read and optionally decrypt the message contents
4463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4465 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004468 return( ret );
4469 }
4470
4471 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004473 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004476 if( ssl->next_record_offset < ssl->in_left )
4477 {
4478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4479 }
4480 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004481 else
4482#endif
4483 ssl->in_left = 0;
4484
4485 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004489 {
4490 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4492 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004493 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004494 /* Except when waiting for Finished as a bad mac here
4495 * probably means something went wrong in the handshake
4496 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4497 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4498 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4499 {
4500#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4501 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4502 {
4503 mbedtls_ssl_send_alert_message( ssl,
4504 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4505 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4506 }
4507#endif
4508 return( ret );
4509 }
4510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004512 if( ssl->conf->badmac_limit != 0 &&
4513 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4516 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004517 }
4518#endif
4519
Hanno Becker4a810fb2017-05-24 16:27:30 +01004520 /* As above, invalid records cause
4521 * dismissal of the whole datagram. */
4522
4523 ssl->next_record_offset = 0;
4524 ssl->in_left = 0;
4525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004527 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004528 }
4529
4530 return( ret );
4531 }
4532 else
4533#endif
4534 {
4535 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4537 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 mbedtls_ssl_send_alert_message( ssl,
4540 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4541 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004542 }
4543#endif
4544 return( ret );
4545 }
4546 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004547
Simon Butcher99000142016-10-13 17:21:01 +01004548 return( 0 );
4549}
4550
4551int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4552{
4553 int ret;
4554
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004555 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004556 * Handle particular types of records
4557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004559 {
Simon Butcher99000142016-10-13 17:21:01 +01004560 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4561 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004562 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 }
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004568 if( ssl->in_msglen != 2 )
4569 {
4570 /* Note: Standard allows for more than one 2 byte alert
4571 to be packed in a single message, but Mbed TLS doesn't
4572 currently support this. */
4573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4574 ssl->in_msglen ) );
4575 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4576 }
4577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 ssl->in_msg[0], ssl->in_msg[1] ) );
4580
4581 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004582 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004587 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004589 }
4590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4592 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4595 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004596 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004597
4598#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4599 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4600 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4601 {
Hanno Becker90333da2017-10-10 11:27:13 +01004602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004603 /* Will be handled when trying to parse ServerHello */
4604 return( 0 );
4605 }
4606#endif
4607
4608#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4609 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4610 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4611 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4612 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4613 {
4614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4615 /* Will be handled in mbedtls_ssl_parse_certificate() */
4616 return( 0 );
4617 }
4618#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4619
4620 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004621 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 }
4623
Hanno Beckerc76c6192017-06-06 10:03:17 +01004624#if defined(MBEDTLS_SSL_PROTO_DTLS)
4625 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4626 ssl->handshake != NULL &&
4627 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4628 {
4629 ssl_handshake_wrapup_free_hs_transform( ssl );
4630 }
4631#endif
4632
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 return( 0 );
4634}
4635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004637{
4638 int ret;
4639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4641 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4642 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004643 {
4644 return( ret );
4645 }
4646
4647 return( 0 );
4648}
4649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004651 unsigned char level,
4652 unsigned char message )
4653{
4654 int ret;
4655
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004656 if( ssl == NULL || ssl->conf == NULL )
4657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004663 ssl->out_msglen = 2;
4664 ssl->out_msg[0] = level;
4665 ssl->out_msg[1] = message;
4666
Hanno Becker67bc7c32018-08-06 11:33:50 +01004667 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004670 return( ret );
4671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004673
4674 return( 0 );
4675}
4676
Paul Bakker5121ce52009-01-03 21:22:43 +00004677/*
4678 * Handshake functions
4679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4681 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4682 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4683 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4684 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4685 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4686 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004687/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4695 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004696 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4697 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004700 ssl->state++;
4701 return( 0 );
4702 }
4703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004706}
4707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004709{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4715 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004716 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4717 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004720 ssl->state++;
4721 return( 0 );
4722 }
4723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004726}
Gilles Peskinef9828522017-05-03 12:28:43 +02004727
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004728#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004729/* Some certificate support -> implement write and parse */
4730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004734 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 const mbedtls_x509_crt *crt;
4736 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4741 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004742 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4743 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004746 ssl->state++;
4747 return( 0 );
4748 }
4749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004751 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004752 {
4753 if( ssl->client_auth == 0 )
4754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 ssl->state++;
4757 return( 0 );
4758 }
4759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 /*
4762 * If using SSLv3 and got no cert, send an Alert message
4763 * (otherwise an empty Certificate message will be sent).
4764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4766 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 {
4768 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4770 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4771 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004774 goto write_msg;
4775 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778#endif /* MBEDTLS_SSL_CLI_C */
4779#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004780 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4785 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004786 }
4787 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004788#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004791
4792 /*
4793 * 0 . 0 handshake type
4794 * 1 . 3 handshake length
4795 * 4 . 6 length of all certs
4796 * 7 . 9 length of cert. 1
4797 * 10 . n-1 peer certificate
4798 * n . n+2 length of cert. 2
4799 * n+3 . ... upper level cert, etc.
4800 */
4801 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004803
Paul Bakker29087132010-03-21 21:03:34 +00004804 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004805 {
4806 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004807 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004810 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 }
4813
4814 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4815 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4816 ssl->out_msg[i + 2] = (unsigned char)( n );
4817
4818 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4819 i += n; crt = crt->next;
4820 }
4821
4822 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4823 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4824 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4825
4826 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4828 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004829
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004830#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004831write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004832#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004833
4834 ssl->state++;
4835
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004836 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004837 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 return( ret );
4840 }
4841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004843
Paul Bakkered27a042013-04-18 22:46:23 +02004844 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004845}
4846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004850 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004852 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004853 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4858 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004859 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4860 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004863 ssl->state++;
4864 return( 0 );
4865 }
4866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004869 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4870 {
4871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4872 ssl->state++;
4873 return( 0 );
4874 }
4875
4876#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4877 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4878 authmode = ssl->handshake->sni_authmode;
4879#endif
4880
4881 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4882 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004884 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004886 ssl->state++;
4887 return( 0 );
4888 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004889#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004893 /* mbedtls_ssl_read_record may have sent an alert already. We
4894 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 return( ret );
4897 }
4898
4899 ssl->state++;
4900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901#if defined(MBEDTLS_SSL_SRV_C)
4902#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 /*
4904 * Check if the client sent an empty certificate
4905 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004906 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004908 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004909 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4911 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4912 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004915
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004916 /* The client was asked for a certificate but didn't send
4917 one. The client should know what's going on, so we
4918 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004919 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004920 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004921 return( 0 );
4922 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004924 }
4925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4929 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004930 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4934 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4935 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4936 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004939
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004940 /* The client was asked for a certificate but didn't send
4941 one. The client should know what's going on, so we
4942 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004943 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004944 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004945 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004946 else
4947 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004948 }
4949 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4951 MBEDTLS_SSL_PROTO_TLS1_2 */
4952#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004957 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4958 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004960 }
4961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4963 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004966 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4967 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004969 }
4970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004972
Paul Bakker5121ce52009-01-03 21:22:43 +00004973 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004975 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004976 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004977
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004978 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004982 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4983 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004985 }
4986
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004987 /* In case we tried to reuse a session but it failed */
4988 if( ssl->session_negotiate->peer_cert != NULL )
4989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4991 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004992 }
4993
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004994 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004996 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004999 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5000 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005001 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005002 }
5003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005005
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005006 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005007
5008 while( i < ssl->in_hslen )
5009 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005010 if ( i + 3 > ssl->in_hslen ) {
5011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5012 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5013 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5014 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005016 if( ssl->in_msg[i] != 0 )
5017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005019 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5020 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005022 }
5023
5024 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5025 | (unsigned int) ssl->in_msg[i + 2];
5026 i += 3;
5027
5028 if( n < 128 || i + n > ssl->in_hslen )
5029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005031 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5032 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005034 }
5035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005037 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005038 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005039 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005040 case 0: /*ok*/
5041 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5042 /* Ignore certificate with an unknown algorithm: maybe a
5043 prior certificate was already trusted. */
5044 break;
5045
5046 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5047 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5048 goto crt_parse_der_failed;
5049
5050 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5051 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5052 goto crt_parse_der_failed;
5053
5054 default:
5055 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5056 crt_parse_der_failed:
5057 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005059 return( ret );
5060 }
5061
5062 i += n;
5063 }
5064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005066
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005067 /*
5068 * On client, make sure the server cert doesn't change during renego to
5069 * avoid "triple handshake" attack: https://secure-resumption.com/
5070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005072 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005074 {
5075 if( ssl->session->peer_cert == NULL )
5076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005078 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5079 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005081 }
5082
5083 if( ssl->session->peer_cert->raw.len !=
5084 ssl->session_negotiate->peer_cert->raw.len ||
5085 memcmp( ssl->session->peer_cert->raw.p,
5086 ssl->session_negotiate->peer_cert->raw.p,
5087 ssl->session->peer_cert->raw.len ) != 0 )
5088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005090 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5091 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005093 }
5094 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005096
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005097 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005099 mbedtls_x509_crt *ca_chain;
5100 mbedtls_x509_crl *ca_crl;
5101
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005102#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005103 if( ssl->handshake->sni_ca_chain != NULL )
5104 {
5105 ca_chain = ssl->handshake->sni_ca_chain;
5106 ca_crl = ssl->handshake->sni_ca_crl;
5107 }
5108 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005109#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005110 {
5111 ca_chain = ssl->conf->ca_chain;
5112 ca_crl = ssl->conf->ca_crl;
5113 }
5114
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005115 /*
5116 * Main check: verify certificate
5117 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005118 ret = mbedtls_x509_crt_verify_with_profile(
5119 ssl->session_negotiate->peer_cert,
5120 ca_chain, ca_crl,
5121 ssl->conf->cert_profile,
5122 ssl->hostname,
5123 &ssl->session_negotiate->verify_result,
5124 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005125
5126 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005129 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005130
5131 /*
5132 * Secondary checks: always done, but change 'ret' only if it was 0
5133 */
5134
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005135#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005138
5139 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005141 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005142 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005143 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005146 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005148 }
5149 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005150#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005153 ciphersuite_info,
5154 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005155 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005158 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005160 }
5161
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005162 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5163 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5164 * with details encoded in the verification flags. All other kinds
5165 * of error codes, including those from the user provided f_vrfy
5166 * functions, are treated as fatal and lead to a failure of
5167 * ssl_parse_certificate even if verification was optional. */
5168 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5169 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5170 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5171 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005172 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005173 }
5174
5175 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5176 {
5177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5178 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5179 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005180
5181 if( ret != 0 )
5182 {
5183 /* The certificate may have been rejected for several reasons.
5184 Pick one and send the corresponding alert. Which alert to send
5185 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005186 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5187 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5188 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5189 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5190 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5191 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5192 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5193 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5194 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5195 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5196 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5197 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5198 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5199 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5200 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5201 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5202 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5203 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5204 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5205 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005206 else
5207 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005208 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5209 alert );
5210 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005211
Hanno Beckere6706e62017-05-15 16:05:15 +01005212#if defined(MBEDTLS_DEBUG_C)
5213 if( ssl->session_negotiate->verify_result != 0 )
5214 {
5215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5216 ssl->session_negotiate->verify_result ) );
5217 }
5218 else
5219 {
5220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5221 }
5222#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005223 }
5224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005226
5227 return( ret );
5228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5230 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5231 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5232 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5233 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5234 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5235 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005238{
5239 int ret;
5240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 ssl->out_msglen = 1;
5245 ssl->out_msg[0] = 1;
5246
Paul Bakker5121ce52009-01-03 21:22:43 +00005247 ssl->state++;
5248
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005249 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005250 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005252 return( ret );
5253 }
5254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005256
5257 return( 0 );
5258}
5259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005261{
5262 int ret;
5263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 return( ret );
5270 }
5271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005275 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5276 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005278 }
5279
5280 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005283 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5284 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005286 }
5287
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005288 /*
5289 * Switch to our negotiated transform and session parameters for inbound
5290 * data.
5291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005293 ssl->transform_in = ssl->transform_negotiate;
5294 ssl->session_in = ssl->session_negotiate;
5295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005300 ssl_dtls_replay_reset( ssl );
5301#endif
5302
5303 /* Increment epoch */
5304 if( ++ssl->in_epoch == 0 )
5305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005307 /* This is highly unlikely to happen for legitimate reasons, so
5308 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005310 }
5311 }
5312 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005314 memset( ssl->in_ctr, 0, 8 );
5315
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005316 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5319 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005324 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5325 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005327 }
5328 }
5329#endif
5330
Paul Bakker5121ce52009-01-03 21:22:43 +00005331 ssl->state++;
5332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005334
5335 return( 0 );
5336}
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5339 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005340{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005341 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5344 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5345 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005346 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005347 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005348#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5350#if defined(MBEDTLS_SHA512_C)
5351 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005352 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5353 else
5354#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355#if defined(MBEDTLS_SHA256_C)
5356 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005357 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005358 else
5359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005363 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005364 }
Paul Bakker380da532012-04-18 16:10:25 +00005365}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5370 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005371 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5372 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5375#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005376 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005377#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005379 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005382}
5383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005385 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5388 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005389 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5390 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5393#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005394 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005397 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005400}
5401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5403 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5404static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005405 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005406{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005407 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5408 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005409}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005410#endif
Paul Bakker380da532012-04-18 16:10:25 +00005411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5413#if defined(MBEDTLS_SHA256_C)
5414static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005415 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005416{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005417 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005418}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005419#endif
Paul Bakker380da532012-04-18 16:10:25 +00005420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421#if defined(MBEDTLS_SHA512_C)
5422static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005423 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005424{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005425 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005426}
Paul Bakker769075d2012-11-24 11:26:46 +01005427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005431static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005433{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005434 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005435 mbedtls_md5_context md5;
5436 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005437
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 unsigned char padbuf[48];
5439 unsigned char md5sum[16];
5440 unsigned char sha1sum[20];
5441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005443 if( !session )
5444 session = ssl->session;
5445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005447
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005448 mbedtls_md5_init( &md5 );
5449 mbedtls_sha1_init( &sha1 );
5450
5451 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5452 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005453
5454 /*
5455 * SSLv3:
5456 * hash =
5457 * MD5( master + pad2 +
5458 * MD5( handshake + sender + master + pad1 ) )
5459 * + SHA1( master + pad2 +
5460 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 */
5462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005464 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5465 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005466#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005469 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5470 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005471#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005474 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005475
Paul Bakker1ef83d62012-04-11 12:09:53 +00005476 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005477
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005478 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5479 mbedtls_md5_update_ret( &md5, session->master, 48 );
5480 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5481 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005482
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005483 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5484 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5485 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5486 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005487
Paul Bakker1ef83d62012-04-11 12:09:53 +00005488 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005490 mbedtls_md5_starts_ret( &md5 );
5491 mbedtls_md5_update_ret( &md5, session->master, 48 );
5492 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5493 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5494 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005495
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005496 mbedtls_sha1_starts_ret( &sha1 );
5497 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5498 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5499 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5500 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005503
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005504 mbedtls_md5_free( &md5 );
5505 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005506
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005507 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5508 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5509 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005516static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005518{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005519 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005520 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005521 mbedtls_md5_context md5;
5522 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005523 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005526 if( !session )
5527 session = ssl->session;
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005530
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005531 mbedtls_md5_init( &md5 );
5532 mbedtls_sha1_init( &sha1 );
5533
5534 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5535 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005536
Paul Bakker1ef83d62012-04-11 12:09:53 +00005537 /*
5538 * TLSv1:
5539 * hash = PRF( master, finished_label,
5540 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5541 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005544 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5545 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005546#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005549 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5550 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005551#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005554 ? "client finished"
5555 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005556
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005557 mbedtls_md5_finish_ret( &md5, padbuf );
5558 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005559
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005560 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005561 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005564
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005565 mbedtls_md5_free( &md5 );
5566 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005567
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005568 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5575#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005576static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005578{
5579 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005580 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005581 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005582 unsigned char padbuf[32];
5583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005585 if( !session )
5586 session = ssl->session;
5587
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005588 mbedtls_sha256_init( &sha256 );
5589
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005591
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005592 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005593
5594 /*
5595 * TLSv1.2:
5596 * hash = PRF( master, finished_label,
5597 * Hash( handshake ) )[0.11]
5598 */
5599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600#if !defined(MBEDTLS_SHA256_ALT)
5601 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005602 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005603#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005606 ? "client finished"
5607 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005608
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005609 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005610
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005611 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005612 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005615
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005616 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005617
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005618 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005625static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005627{
5628 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005629 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005630 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005631 unsigned char padbuf[48];
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005634 if( !session )
5635 session = ssl->session;
5636
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005637 mbedtls_sha512_init( &sha512 );
5638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005640
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005641 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005642
5643 /*
5644 * TLSv1.2:
5645 * hash = PRF( master, finished_label,
5646 * Hash( handshake ) )[0.11]
5647 */
5648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005650 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5651 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005652#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005655 ? "client finished"
5656 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005658 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005659
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005660 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005661 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005664
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005665 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005666
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005667 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671#endif /* MBEDTLS_SHA512_C */
5672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005677
5678 /*
5679 * Free our handshake params
5680 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005681 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005683 ssl->handshake = NULL;
5684
5685 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005686 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005687 */
5688 if( ssl->transform )
5689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 mbedtls_ssl_transform_free( ssl->transform );
5691 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005692 }
5693 ssl->transform = ssl->transform_negotiate;
5694 ssl->transform_negotiate = NULL;
5695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005697}
5698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005700{
5701 int resume = ssl->handshake->resume;
5702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705#if defined(MBEDTLS_SSL_RENEGOTIATION)
5706 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005709 ssl->renego_records_seen = 0;
5710 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005711#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005712
5713 /*
5714 * Free the previous session and switch in the current one
5715 */
Paul Bakker0a597072012-09-25 21:55:46 +00005716 if( ssl->session )
5717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005719 /* RFC 7366 3.1: keep the EtM state */
5720 ssl->session_negotiate->encrypt_then_mac =
5721 ssl->session->encrypt_then_mac;
5722#endif
5723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 mbedtls_ssl_session_free( ssl->session );
5725 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005726 }
5727 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005728 ssl->session_negotiate = NULL;
5729
Paul Bakker0a597072012-09-25 21:55:46 +00005730 /*
5731 * Add cache entry
5732 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005733 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005734 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005735 resume == 0 )
5736 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005737 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005739 }
Paul Bakker0a597072012-09-25 21:55:46 +00005740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005742 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005743 ssl->handshake->flight != NULL )
5744 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005745 /* Cancel handshake timer */
5746 ssl_set_timer( ssl, 0 );
5747
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005748 /* Keep last flight around in case we need to resend it:
5749 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005751 }
5752 else
5753#endif
5754 ssl_handshake_wrapup_free_hs_transform( ssl );
5755
Paul Bakker48916f92012-09-16 19:57:18 +00005756 ssl->state++;
5757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005759}
5760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005762{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005763 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005766
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005767 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01005768
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005769 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005770
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005771 /*
5772 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5773 * may define some other value. Currently (early 2016), no defined
5774 * ciphersuite does this (and this is unlikely to change as activity has
5775 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005780 ssl->verify_data_len = hash_len;
5781 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005782#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005783
Paul Bakker5121ce52009-01-03 21:22:43 +00005784 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5786 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005787
5788 /*
5789 * In case of session resuming, invert the client and server
5790 * ChangeCipherSpec messages order.
5791 */
Paul Bakker0a597072012-09-25 21:55:46 +00005792 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005795 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005799 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005801#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 }
5803 else
5804 ssl->state++;
5805
Paul Bakker48916f92012-09-16 19:57:18 +00005806 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005807 * Switch to our negotiated transform and session parameters for outbound
5808 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005813 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005814 {
5815 unsigned char i;
5816
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005817 /* Remember current epoch settings for resending */
5818 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01005819 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005820
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005821 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01005822 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005823
5824 /* Increment epoch */
5825 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01005826 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005827 break;
5828
5829 /* The loop goes to its end iff the counter is wrapping */
5830 if( i == 0 )
5831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5833 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005834 }
5835 }
5836 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01005838 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005839
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005840 ssl->transform_out = ssl->transform_negotiate;
5841 ssl->session_out = ssl->session_negotiate;
5842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5844 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5849 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005850 }
5851 }
5852#endif
5853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005857#endif
5858
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005859 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005862 return( ret );
5863 }
5864
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005865#if defined(MBEDTLS_SSL_PROTO_DTLS)
5866 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5867 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5868 {
5869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5870 return( ret );
5871 }
5872#endif
5873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005875
5876 return( 0 );
5877}
5878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005880#define SSL_MAX_HASH_LEN 36
5881#else
5882#define SSL_MAX_HASH_LEN 12
5883#endif
5884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886{
Paul Bakker23986e52011-04-24 08:57:21 +00005887 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005888 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005889 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005892
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005893 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005898 return( ret );
5899 }
5900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005904 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5905 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005907 }
5908
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005909 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910#if defined(MBEDTLS_SSL_PROTO_SSL3)
5911 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005912 hash_len = 36;
5913 else
5914#endif
5915 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5918 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005921 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5922 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005924 }
5925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005927 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005930 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5931 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 }
5934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005936 ssl->verify_data_len = hash_len;
5937 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005938#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005939
Paul Bakker0a597072012-09-25 21:55:46 +00005940 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005943 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005949#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 }
5951 else
5952 ssl->state++;
5953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005957#endif
5958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005960
5961 return( 0 );
5962}
5963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5969 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5970 mbedtls_md5_init( &handshake->fin_md5 );
5971 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005972 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5973 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5976#if defined(MBEDTLS_SHA256_C)
5977 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005978 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980#if defined(MBEDTLS_SHA512_C)
5981 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005982 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005985
5986 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005987
5988#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5989 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5990 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5991#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993#if defined(MBEDTLS_DHM_C)
5994 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005995#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996#if defined(MBEDTLS_ECDH_C)
5997 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005998#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02005999#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006000 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006001#if defined(MBEDTLS_SSL_CLI_C)
6002 handshake->ecjpake_cache = NULL;
6003 handshake->ecjpake_cache_len = 0;
6004#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006005#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006006
6007#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6008 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6009#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006010}
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6017 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 mbedtls_md_init( &transform->md_ctx_enc );
6020 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006021}
6022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006026}
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006029{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006030 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006031 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006033 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006035 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006036 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006037
6038 /*
6039 * Either the pointers are now NULL or cleared properly and can be freed.
6040 * Now allocate missing structures.
6041 */
6042 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006043 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006044 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006045 }
Paul Bakker48916f92012-09-16 19:57:18 +00006046
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006047 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006048 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006049 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006050 }
Paul Bakker48916f92012-09-16 19:57:18 +00006051
Paul Bakker82788fb2014-10-20 13:59:19 +02006052 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006053 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006054 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006055 }
Paul Bakker48916f92012-09-16 19:57:18 +00006056
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006057 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006058 if( ssl->handshake == NULL ||
6059 ssl->transform_negotiate == NULL ||
6060 ssl->session_negotiate == NULL )
6061 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 mbedtls_free( ssl->handshake );
6065 mbedtls_free( ssl->transform_negotiate );
6066 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006067
6068 ssl->handshake = NULL;
6069 ssl->transform_negotiate = NULL;
6070 ssl->session_negotiate = NULL;
6071
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006072 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006073 }
6074
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006075 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006077 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006078 ssl_handshake_params_init( ssl->handshake );
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006081 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6082 {
6083 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006084
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006085 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6086 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6087 else
6088 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006089
6090 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006091 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006092#endif
6093
Paul Bakker48916f92012-09-16 19:57:18 +00006094 return( 0 );
6095}
6096
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006097#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006098/* Dummy cookie callbacks for defaults */
6099static int ssl_cookie_write_dummy( void *ctx,
6100 unsigned char **p, unsigned char *end,
6101 const unsigned char *cli_id, size_t cli_id_len )
6102{
6103 ((void) ctx);
6104 ((void) p);
6105 ((void) end);
6106 ((void) cli_id);
6107 ((void) cli_id_len);
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006110}
6111
6112static int ssl_cookie_check_dummy( void *ctx,
6113 const unsigned char *cookie, size_t cookie_len,
6114 const unsigned char *cli_id, size_t cli_id_len )
6115{
6116 ((void) ctx);
6117 ((void) cookie);
6118 ((void) cookie_len);
6119 ((void) cli_id);
6120 ((void) cli_id_len);
6121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006123}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006124#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006125
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006126/* Once ssl->out_hdr as the address of the beginning of the
6127 * next outgoing record is set, deduce the other pointers.
6128 *
6129 * Note: For TLS, we save the implicit record sequence number
6130 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6131 * and the caller has to make sure there's space for this.
6132 */
6133
6134static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6135 mbedtls_ssl_transform *transform )
6136{
6137#if defined(MBEDTLS_SSL_PROTO_DTLS)
6138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6139 {
6140 ssl->out_ctr = ssl->out_hdr + 3;
6141 ssl->out_len = ssl->out_hdr + 11;
6142 ssl->out_iv = ssl->out_hdr + 13;
6143 }
6144 else
6145#endif
6146 {
6147 ssl->out_ctr = ssl->out_hdr - 8;
6148 ssl->out_len = ssl->out_hdr + 3;
6149 ssl->out_iv = ssl->out_hdr + 5;
6150 }
6151
6152 /* Adjust out_msg to make space for explicit IV, if used. */
6153 if( transform != NULL &&
6154 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6155 {
6156 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6157 }
6158 else
6159 ssl->out_msg = ssl->out_iv;
6160}
6161
6162/* Once ssl->in_hdr as the address of the beginning of the
6163 * next incoming record is set, deduce the other pointers.
6164 *
6165 * Note: For TLS, we save the implicit record sequence number
6166 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6167 * and the caller has to make sure there's space for this.
6168 */
6169
6170static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6171 mbedtls_ssl_transform *transform )
6172{
6173#if defined(MBEDTLS_SSL_PROTO_DTLS)
6174 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6175 {
6176 ssl->in_ctr = ssl->in_hdr + 3;
6177 ssl->in_len = ssl->in_hdr + 11;
6178 ssl->in_iv = ssl->in_hdr + 13;
6179 }
6180 else
6181#endif
6182 {
6183 ssl->in_ctr = ssl->in_hdr - 8;
6184 ssl->in_len = ssl->in_hdr + 3;
6185 ssl->in_iv = ssl->in_hdr + 5;
6186 }
6187
6188 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6189 if( transform != NULL &&
6190 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6191 {
6192 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6193 }
6194 else
6195 ssl->in_msg = ssl->in_iv;
6196}
6197
Paul Bakker5121ce52009-01-03 21:22:43 +00006198/*
6199 * Initialize an SSL context
6200 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006201void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6202{
6203 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6204}
6205
6206/*
6207 * Setup an SSL context
6208 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006209int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006210 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006211{
Paul Bakker48916f92012-09-16 19:57:18 +00006212 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006213
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006214 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006215
6216 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006217 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006218 */
Angus Grattond8213d02016-05-25 20:56:48 +10006219 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6220 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 {
Angus Grattond8213d02016-05-25 20:56:48 +10006222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6223 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6224 }
6225
6226 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6227 if( ssl->out_buf == NULL )
6228 {
6229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006231 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006232 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233 }
6234
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006235 /* Set the incoming and outgoing record pointers. */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006236#if defined(MBEDTLS_SSL_PROTO_DTLS)
6237 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6238 {
6239 ssl->out_hdr = ssl->out_buf;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006240 ssl->in_hdr = ssl->in_buf;
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006241 }
6242 else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006244 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006245 ssl->out_hdr = ssl->out_buf + 8;
6246 ssl->in_hdr = ssl->in_buf + 8;
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006247 }
6248
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006249 /* Derive other internal pointers. */
6250 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6251 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6252
Paul Bakker48916f92012-09-16 19:57:18 +00006253 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6254 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006255
6256 return( 0 );
6257}
6258
6259/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006260 * Reset an initialized and used SSL context for re-use while retaining
6261 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006262 *
6263 * If partial is non-zero, keep data in the input buffer and client ID.
6264 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006265 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006266static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006267{
Paul Bakker48916f92012-09-16 19:57:18 +00006268 int ret;
6269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006271
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006272 /* Cancel any possibly running timer */
6273 ssl_set_timer( ssl, 0 );
6274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275#if defined(MBEDTLS_SSL_RENEGOTIATION)
6276 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006277 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006278
6279 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6281 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006284
Paul Bakker7eb013f2011-10-06 12:37:39 +00006285 ssl->in_offt = NULL;
6286
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006287 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006288 ssl->in_msgtype = 0;
6289 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006290 if( partial == 0 )
6291 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006293 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006294 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006295#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006297 ssl_dtls_replay_reset( ssl );
6298#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006299
6300 ssl->in_hslen = 0;
6301 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006302
6303 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006304
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006305 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006306 ssl->out_msgtype = 0;
6307 ssl->out_msglen = 0;
6308 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6310 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006311 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006312#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006313
Hanno Becker19859472018-08-06 09:40:20 +01006314 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6315
Paul Bakker48916f92012-09-16 19:57:18 +00006316 ssl->transform_in = NULL;
6317 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006318
Hanno Becker78640902018-08-13 16:35:15 +01006319 ssl->session_in = NULL;
6320 ssl->session_out = NULL;
6321
Angus Grattond8213d02016-05-25 20:56:48 +10006322 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006323 if( partial == 0 )
Angus Grattond8213d02016-05-25 20:56:48 +10006324 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6327 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6330 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6333 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006334 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006335 }
6336#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006337
Paul Bakker48916f92012-09-16 19:57:18 +00006338 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 mbedtls_ssl_transform_free( ssl->transform );
6341 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006342 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006343 }
Paul Bakker48916f92012-09-16 19:57:18 +00006344
Paul Bakkerc0463502013-02-14 11:19:38 +01006345 if( ssl->session )
6346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 mbedtls_ssl_session_free( ssl->session );
6348 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006349 ssl->session = NULL;
6350 }
6351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006353 ssl->alpn_chosen = NULL;
6354#endif
6355
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006356#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006357 if( partial == 0 )
6358 {
6359 mbedtls_free( ssl->cli_id );
6360 ssl->cli_id = NULL;
6361 ssl->cli_id_len = 0;
6362 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006363#endif
6364
Paul Bakker48916f92012-09-16 19:57:18 +00006365 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6366 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006367
6368 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006369}
6370
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006371/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006372 * Reset an initialized and used SSL context for re-use while retaining
6373 * all application-set variables, function pointers and data.
6374 */
6375int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6376{
6377 return( ssl_session_reset_int( ssl, 0 ) );
6378}
6379
6380/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006381 * SSL set accessors
6382 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006383void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006384{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006385 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006386}
6387
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006388void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006389{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006390 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006391}
6392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006394void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006396 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006397}
6398#endif
6399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006401void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006403 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006404}
6405#endif
6406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006408void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006410 conf->hs_timeout_min = min;
6411 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006412}
6413#endif
6414
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006415void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006416{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006417 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006418}
6419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006421void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006422 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006423 void *p_vrfy )
6424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006425 conf->f_vrfy = f_vrfy;
6426 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006429
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006430void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006431 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 void *p_rng )
6433{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006434 conf->f_rng = f_rng;
6435 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006436}
6437
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006438void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006439 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006440 void *p_dbg )
6441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006442 conf->f_dbg = f_dbg;
6443 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006444}
6445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006447 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006448 mbedtls_ssl_send_t *f_send,
6449 mbedtls_ssl_recv_t *f_recv,
6450 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006451{
6452 ssl->p_bio = p_bio;
6453 ssl->f_send = f_send;
6454 ssl->f_recv = f_recv;
6455 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006456}
6457
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006458void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006459{
6460 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006461}
6462
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006463void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6464 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006465 mbedtls_ssl_set_timer_t *f_set_timer,
6466 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006467{
6468 ssl->p_timer = p_timer;
6469 ssl->f_set_timer = f_set_timer;
6470 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006471
6472 /* Make sure we start with no timer running */
6473 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006474}
6475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006477void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006478 void *p_cache,
6479 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6480 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006481{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006482 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006483 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006484 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488#if defined(MBEDTLS_SSL_CLI_C)
6489int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006490{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006491 int ret;
6492
6493 if( ssl == NULL ||
6494 session == NULL ||
6495 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006496 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006499 }
6500
6501 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6502 return( ret );
6503
Paul Bakker0a597072012-09-25 21:55:46 +00006504 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006505
6506 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006509
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006510void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006511 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006512{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006513 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6514 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6515 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6516 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006517}
6518
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006519void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006520 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006521 int major, int minor )
6522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006524 return;
6525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006527 return;
6528
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006529 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006530}
6531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006533void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006534 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006535{
6536 conf->cert_profile = profile;
6537}
6538
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006539/* Append a new keycert entry to a (possibly empty) list */
6540static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6541 mbedtls_x509_crt *cert,
6542 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006543{
niisato8ee24222018-06-25 19:05:48 +09006544 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006545
niisato8ee24222018-06-25 19:05:48 +09006546 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6547 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006549
niisato8ee24222018-06-25 19:05:48 +09006550 new_cert->cert = cert;
6551 new_cert->key = key;
6552 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006553
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006554 /* Update head is the list was null, else add to the end */
6555 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006556 {
niisato8ee24222018-06-25 19:05:48 +09006557 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006558 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006559 else
6560 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006561 mbedtls_ssl_key_cert *cur = *head;
6562 while( cur->next != NULL )
6563 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006564 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006565 }
6566
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006567 return( 0 );
6568}
6569
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006570int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006571 mbedtls_x509_crt *own_cert,
6572 mbedtls_pk_context *pk_key )
6573{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006574 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006575}
6576
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006577void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006578 mbedtls_x509_crt *ca_chain,
6579 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006580{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006581 conf->ca_chain = ca_chain;
6582 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006585
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006586#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6587int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6588 mbedtls_x509_crt *own_cert,
6589 mbedtls_pk_context *pk_key )
6590{
6591 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6592 own_cert, pk_key ) );
6593}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006594
6595void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6596 mbedtls_x509_crt *ca_chain,
6597 mbedtls_x509_crl *ca_crl )
6598{
6599 ssl->handshake->sni_ca_chain = ca_chain;
6600 ssl->handshake->sni_ca_crl = ca_crl;
6601}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006602
6603void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6604 int authmode )
6605{
6606 ssl->handshake->sni_authmode = authmode;
6607}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006608#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6609
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006610#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006611/*
6612 * Set EC J-PAKE password for current handshake
6613 */
6614int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6615 const unsigned char *pw,
6616 size_t pw_len )
6617{
6618 mbedtls_ecjpake_role role;
6619
Janos Follath8eb64132016-06-03 15:40:57 +01006620 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6622
6623 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6624 role = MBEDTLS_ECJPAKE_SERVER;
6625 else
6626 role = MBEDTLS_ECJPAKE_CLIENT;
6627
6628 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6629 role,
6630 MBEDTLS_MD_SHA256,
6631 MBEDTLS_ECP_DP_SECP256R1,
6632 pw, pw_len ) );
6633}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006634#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006637int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006638 const unsigned char *psk, size_t psk_len,
6639 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006640{
Paul Bakker6db455e2013-09-18 17:29:31 +02006641 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006646
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006647 /* Identity len will be encoded on two bytes */
6648 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006649 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006650 {
6651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6652 }
6653
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006654 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006655 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006656 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006657
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006658 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006659 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006660 conf->psk_len = 0;
6661 }
6662 if( conf->psk_identity != NULL )
6663 {
6664 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006665 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006666 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006667 }
6668
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006669 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6670 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006671 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006672 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006673 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006674 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006675 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006676 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006677 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006678
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006679 conf->psk_len = psk_len;
6680 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006681
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006682 memcpy( conf->psk, psk, conf->psk_len );
6683 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006684
6685 return( 0 );
6686}
6687
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006688int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6689 const unsigned char *psk, size_t psk_len )
6690{
6691 if( psk == NULL || ssl->handshake == NULL )
6692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6693
6694 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6696
6697 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006698 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006699 mbedtls_platform_zeroize( ssl->handshake->psk,
6700 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006701 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006702 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006703 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006704
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006705 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006706 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006707
6708 ssl->handshake->psk_len = psk_len;
6709 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6710
6711 return( 0 );
6712}
6713
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006714void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006716 size_t),
6717 void *p_psk )
6718{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006719 conf->f_psk = f_psk;
6720 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006723
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006724#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006725
6726#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006727int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006728{
6729 int ret;
6730
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006731 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6732 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6733 {
6734 mbedtls_mpi_free( &conf->dhm_P );
6735 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006736 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006737 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006738
6739 return( 0 );
6740}
Hanno Becker470a8c42017-10-04 15:28:46 +01006741#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006742
Hanno Beckera90658f2017-10-04 15:29:08 +01006743int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6744 const unsigned char *dhm_P, size_t P_len,
6745 const unsigned char *dhm_G, size_t G_len )
6746{
6747 int ret;
6748
6749 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6750 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6751 {
6752 mbedtls_mpi_free( &conf->dhm_P );
6753 mbedtls_mpi_free( &conf->dhm_G );
6754 return( ret );
6755 }
6756
6757 return( 0 );
6758}
Paul Bakker5121ce52009-01-03 21:22:43 +00006759
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006760int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006761{
6762 int ret;
6763
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006764 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6765 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6766 {
6767 mbedtls_mpi_free( &conf->dhm_P );
6768 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006769 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006770 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006771
6772 return( 0 );
6773}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006774#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006775
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006776#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6777/*
6778 * Set the minimum length for Diffie-Hellman parameters
6779 */
6780void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6781 unsigned int bitlen )
6782{
6783 conf->dhm_min_bitlen = bitlen;
6784}
6785#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6786
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006787#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006788/*
6789 * Set allowed/preferred hashes for handshake signatures
6790 */
6791void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6792 const int *hashes )
6793{
6794 conf->sig_hashes = hashes;
6795}
Hanno Becker947194e2017-04-07 13:25:49 +01006796#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006797
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006798#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006799/*
6800 * Set the allowed elliptic curves
6801 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006802void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006803 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006804{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006805 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006806}
Hanno Becker947194e2017-04-07 13:25:49 +01006807#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006808
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006809#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006811{
Hanno Becker947194e2017-04-07 13:25:49 +01006812 /* Initialize to suppress unnecessary compiler warning */
6813 size_t hostname_len = 0;
6814
6815 /* Check if new hostname is valid before
6816 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006817 if( hostname != NULL )
6818 {
6819 hostname_len = strlen( hostname );
6820
6821 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6823 }
6824
6825 /* Now it's clear that we will overwrite the old hostname,
6826 * so we can free it safely */
6827
6828 if( ssl->hostname != NULL )
6829 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006830 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006831 mbedtls_free( ssl->hostname );
6832 }
6833
6834 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006835
Paul Bakker5121ce52009-01-03 21:22:43 +00006836 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006837 {
6838 ssl->hostname = NULL;
6839 }
6840 else
6841 {
6842 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006843 if( ssl->hostname == NULL )
6844 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006845
Hanno Becker947194e2017-04-07 13:25:49 +01006846 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006847
Hanno Becker947194e2017-04-07 13:25:49 +01006848 ssl->hostname[hostname_len] = '\0';
6849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006850
6851 return( 0 );
6852}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006853#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006854
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006855#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006856void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006858 const unsigned char *, size_t),
6859 void *p_sni )
6860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006861 conf->f_sni = f_sni;
6862 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006867int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006868{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006869 size_t cur_len, tot_len;
6870 const char **p;
6871
6872 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006873 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6874 * MUST NOT be truncated."
6875 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006876 */
6877 tot_len = 0;
6878 for( p = protos; *p != NULL; p++ )
6879 {
6880 cur_len = strlen( *p );
6881 tot_len += cur_len;
6882
6883 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006885 }
6886
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006887 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006888
6889 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006890}
6891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006893{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006894 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006897
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006898void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006899{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006900 conf->max_major_ver = major;
6901 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006902}
6903
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006904void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006905{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006906 conf->min_major_ver = major;
6907 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006908}
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006911void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006912{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006913 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006914}
6915#endif
6916
Janos Follath088ce432017-04-10 12:42:31 +01006917#if defined(MBEDTLS_SSL_SRV_C)
6918void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6919 char cert_req_ca_list )
6920{
6921 conf->cert_req_ca_list = cert_req_ca_list;
6922}
6923#endif
6924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006926void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006928 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006929}
6930#endif
6931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006933void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006934{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006935 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006936}
6937#endif
6938
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006939#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006940void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006942 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006943}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006944#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006945
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02006946#if defined(MBEDTLS_SSL_PROTO_DTLS)
6947void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
6948{
6949 conf->mtu = mtu;
6950}
6951#endif
6952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006954int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10006957 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006960 }
6961
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006962 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006963
6964 return( 0 );
6965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006969void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006971 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006976void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006977{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006978 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006979}
6980#endif
6981
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006982void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006984 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006985}
6986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006988void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006989{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006990 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006991}
6992
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006993void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006995 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006996}
6997
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006998void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006999 const unsigned char period[8] )
7000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007001 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007006#if defined(MBEDTLS_SSL_CLI_C)
7007void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007008{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007009 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007010}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007011#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007012
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007013#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007014void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7015 mbedtls_ssl_ticket_write_t *f_ticket_write,
7016 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7017 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007018{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007019 conf->f_ticket_write = f_ticket_write;
7020 conf->f_ticket_parse = f_ticket_parse;
7021 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007022}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007025
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007026#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7027void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7028 mbedtls_ssl_export_keys_t *f_export_keys,
7029 void *p_export_keys )
7030{
7031 conf->f_export_keys = f_export_keys;
7032 conf->p_export_keys = p_export_keys;
7033}
7034#endif
7035
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007036#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007037void mbedtls_ssl_conf_async_private_cb(
7038 mbedtls_ssl_config *conf,
7039 mbedtls_ssl_async_sign_t *f_async_sign,
7040 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7041 mbedtls_ssl_async_resume_t *f_async_resume,
7042 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007043 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007044{
7045 conf->f_async_sign_start = f_async_sign;
7046 conf->f_async_decrypt_start = f_async_decrypt;
7047 conf->f_async_resume = f_async_resume;
7048 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007049 conf->p_async_config_data = async_config_data;
7050}
7051
Gilles Peskine8f97af72018-04-26 11:46:10 +02007052void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7053{
7054 return( conf->p_async_config_data );
7055}
7056
Gilles Peskine1febfef2018-04-30 11:54:39 +02007057void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007058{
7059 if( ssl->handshake == NULL )
7060 return( NULL );
7061 else
7062 return( ssl->handshake->user_async_ctx );
7063}
7064
Gilles Peskine1febfef2018-04-30 11:54:39 +02007065void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007066 void *ctx )
7067{
7068 if( ssl->handshake != NULL )
7069 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007070}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007071#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007072
Paul Bakker5121ce52009-01-03 21:22:43 +00007073/*
7074 * SSL get accessors
7075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007077{
7078 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7079}
7080
Hanno Becker8b170a02017-10-10 11:51:19 +01007081int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7082{
7083 /*
7084 * Case A: We're currently holding back
7085 * a message for further processing.
7086 */
7087
7088 if( ssl->keep_current_message == 1 )
7089 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007091 return( 1 );
7092 }
7093
7094 /*
7095 * Case B: Further records are pending in the current datagram.
7096 */
7097
7098#if defined(MBEDTLS_SSL_PROTO_DTLS)
7099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7100 ssl->in_left > ssl->next_record_offset )
7101 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007103 return( 1 );
7104 }
7105#endif /* MBEDTLS_SSL_PROTO_DTLS */
7106
7107 /*
7108 * Case C: A handshake message is being processed.
7109 */
7110
Hanno Becker8b170a02017-10-10 11:51:19 +01007111 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7112 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007114 return( 1 );
7115 }
7116
7117 /*
7118 * Case D: An application data message is being processed
7119 */
7120 if( ssl->in_offt != NULL )
7121 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007123 return( 1 );
7124 }
7125
7126 /*
7127 * In all other cases, the rest of the message can be dropped.
7128 * As in ssl_read_record_layer, this needs to be adapted if
7129 * we implement support for multiple alerts in single records.
7130 */
7131
7132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7133 return( 0 );
7134}
7135
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007136uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007137{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007138 if( ssl->session != NULL )
7139 return( ssl->session->verify_result );
7140
7141 if( ssl->session_negotiate != NULL )
7142 return( ssl->session_negotiate->verify_result );
7143
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007144 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007145}
7146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007148{
Paul Bakker926c8e42013-03-06 10:23:34 +01007149 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007150 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007153}
7154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007159 {
7160 switch( ssl->minor_ver )
7161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007163 return( "DTLSv1.0" );
7164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007166 return( "DTLSv1.2" );
7167
7168 default:
7169 return( "unknown (DTLS)" );
7170 }
7171 }
7172#endif
7173
Paul Bakker43ca69c2011-01-15 17:35:19 +00007174 switch( ssl->minor_ver )
7175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007177 return( "SSLv3.0" );
7178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007180 return( "TLSv1.0" );
7181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007183 return( "TLSv1.1" );
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007186 return( "TLSv1.2" );
7187
Paul Bakker43ca69c2011-01-15 17:35:19 +00007188 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007189 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007190 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007191}
7192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007194{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007195 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007197 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007198
Hanno Becker78640902018-08-13 16:35:15 +01007199 if( transform == NULL )
7200 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_ZLIB_SUPPORT)
7203 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7204 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007205 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007206#endif
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 case MBEDTLS_MODE_GCM:
7211 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007212 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007214 transform_expansion = transform->minlen;
7215 break;
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007218
7219 block_size = mbedtls_cipher_get_block_size(
7220 &transform->cipher_ctx_enc );
7221
7222#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7223 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7224 {
7225 /* Expansion due to addition of
7226 * - MAC
7227 * - CBC padding (theoretically up to 256 bytes, but
7228 * we never use more than block_size)
7229 * - explicit IV
7230 */
7231 transform_expansion = transform->maclen + 2 * block_size;
7232 }
7233 else
7234#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7235 {
7236 /* No explicit IV prior to TLS 1.1. */
7237 transform_expansion = transform->maclen + block_size;
7238 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007239 break;
7240
7241 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007244 }
7245
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007246 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007247}
7248
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007249#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7250size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7251{
7252 size_t max_len;
7253
7254 /*
7255 * Assume mfl_code is correct since it was checked when set
7256 */
Angus Grattond8213d02016-05-25 20:56:48 +10007257 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007258
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007259 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007260 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007261 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007262 {
Angus Grattond8213d02016-05-25 20:56:48 +10007263 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007264 }
7265
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007266 /* During a handshake, use the value being negotiated */
7267 if( ssl->session_negotiate != NULL &&
7268 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7269 {
7270 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7271 }
7272
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007273 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007274}
7275#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7276
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007277int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7278{
7279 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7280
7281#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7282 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7283
7284 if( max_len > mfl )
7285 max_len = mfl;
7286#endif
7287
7288#if defined(MBEDTLS_SSL_PROTO_DTLS)
7289 if( ssl->conf->mtu != 0 )
7290 {
7291 const size_t mtu = ssl->conf->mtu;
7292 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7293 const size_t overhead = (size_t) ret;
7294
7295 if( ret < 0 )
7296 return( ret );
7297
7298 if( mtu <= overhead )
7299 {
7300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7301 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7302 }
7303
7304 if( max_len > mtu - overhead )
7305 max_len = mtu - overhead;
7306 }
7307#endif
7308
7309 return( (int) max_len );
7310}
7311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#if defined(MBEDTLS_X509_CRT_PARSE_C)
7313const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007314{
7315 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007316 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007317
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007318 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_CLI_C)
7323int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007324{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007325 if( ssl == NULL ||
7326 dst == NULL ||
7327 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007328 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007331 }
7332
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007333 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007336
Paul Bakker5121ce52009-01-03 21:22:43 +00007337/*
Paul Bakker1961b702013-01-25 14:49:24 +01007338 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007341{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007343
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007344 if( ssl == NULL || ssl->conf == NULL )
7345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007348 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007350#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007354#endif
7355
Paul Bakker1961b702013-01-25 14:49:24 +01007356 return( ret );
7357}
7358
7359/*
7360 * Perform the SSL handshake
7361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007363{
7364 int ret = 0;
7365
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007366 if( ssl == NULL || ssl->conf == NULL )
7367 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007374
7375 if( ret != 0 )
7376 break;
7377 }
7378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007380
7381 return( ret );
7382}
7383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_RENEGOTIATION)
7385#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007386/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007387 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007390{
7391 int ret;
7392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007394
7395 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7397 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007398
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007399 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007400 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007402 return( ret );
7403 }
7404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007406
7407 return( 0 );
7408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007410
7411/*
7412 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 * - any side: calling mbedtls_ssl_renegotiate(),
7414 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7415 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007416 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007417 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007421{
7422 int ret;
7423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007425
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007426 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7427 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007428
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007429 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7430 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007432 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007434 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007435 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007436 ssl->handshake->out_msg_seq = 1;
7437 else
7438 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007439 }
7440#endif
7441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7443 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007448 return( ret );
7449 }
7450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007452
7453 return( 0 );
7454}
7455
7456/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007457 * Renegotiate current connection on client,
7458 * or request renegotiation on server
7459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007463
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007464 if( ssl == NULL || ssl->conf == NULL )
7465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007468 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007469 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007475
7476 /* Did we already try/start sending HelloRequest? */
7477 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007479
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007480 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007485 /*
7486 * On client, either start the renegotiation process or,
7487 * if already in progress, continue the handshake
7488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007493
7494 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007497 return( ret );
7498 }
7499 }
7500 else
7501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007505 return( ret );
7506 }
7507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007509
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007510 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007511}
7512
7513/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007514 * Check record counters and renegotiate if they're above the limit.
7515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007517{
Andres AG2196c7f2016-12-15 17:01:16 +00007518 size_t ep_len = ssl_ep_len( ssl );
7519 int in_ctr_cmp;
7520 int out_ctr_cmp;
7521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7523 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007524 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007525 {
7526 return( 0 );
7527 }
7528
Andres AG2196c7f2016-12-15 17:01:16 +00007529 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7530 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007531 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007532 ssl->conf->renego_period + ep_len, 8 - ep_len );
7533
7534 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007535 {
7536 return( 0 );
7537 }
7538
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007543
7544/*
7545 * Receive application data decrypted from the SSL layer
7546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007548{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007549 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007550 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007551
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007552 if( ssl == NULL || ssl->conf == NULL )
7553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007558 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007561 return( ret );
7562
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007563 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007565 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007566 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007567 return( ret );
7568 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007569 }
7570#endif
7571
Hanno Becker4a810fb2017-05-24 16:27:30 +01007572 /*
7573 * Check if renegotiation is necessary and/or handshake is
7574 * in process. If yes, perform/continue, and fall through
7575 * if an unexpected packet is received while the client
7576 * is waiting for the ServerHello.
7577 *
7578 * (There is no equivalent to the last condition on
7579 * the server-side as it is not treated as within
7580 * a handshake while waiting for the ClientHello
7581 * after a renegotiation request.)
7582 */
7583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007585 ret = ssl_check_ctr_renegotiate( ssl );
7586 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7587 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007590 return( ret );
7591 }
7592#endif
7593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007597 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7598 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007601 return( ret );
7602 }
7603 }
7604
Hanno Beckere41158b2017-10-23 13:30:32 +01007605 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007606 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007607 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007608 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007609 if( ssl->f_get_timer != NULL &&
7610 ssl->f_get_timer( ssl->p_timer ) == -1 )
7611 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007612 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007613 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007614
Hanno Becker4a810fb2017-05-24 16:27:30 +01007615 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007616 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007617 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7618 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007619
Hanno Becker4a810fb2017-05-24 16:27:30 +01007620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7621 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007622 }
7623
7624 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007626 {
7627 /*
7628 * OpenSSL sends empty messages to randomize the IV
7629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007633 return( 0 );
7634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007636 return( ret );
7637 }
7638 }
7639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007643
Hanno Becker4a810fb2017-05-24 16:27:30 +01007644 /*
7645 * - For client-side, expect SERVER_HELLO_REQUEST.
7646 * - For server-side, expect CLIENT_HELLO.
7647 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7648 */
7649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007651 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007653 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007656
7657 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007659 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007660 {
7661 continue;
7662 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007665 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007666#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007667
Hanno Becker4a810fb2017-05-24 16:27:30 +01007668#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007669 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007673
7674 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007676 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007677 {
7678 continue;
7679 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007682 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007683#endif /* MBEDTLS_SSL_SRV_C */
7684
Hanno Becker21df7f92017-10-17 11:03:26 +01007685#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007686 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007687 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7688 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7689 ssl->conf->allow_legacy_renegotiation ==
7690 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7691 {
7692 /*
7693 * Accept renegotiation request
7694 */
Paul Bakker48916f92012-09-16 19:57:18 +00007695
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007696 /* DTLS clients need to know renego is server-initiated */
7697#if defined(MBEDTLS_SSL_PROTO_DTLS)
7698 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7699 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7700 {
7701 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7702 }
7703#endif
7704 ret = ssl_start_renegotiation( ssl );
7705 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7706 ret != 0 )
7707 {
7708 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7709 return( ret );
7710 }
7711 }
7712 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007713#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007714 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007715 /*
7716 * Refuse renegotiation
7717 */
7718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721#if defined(MBEDTLS_SSL_PROTO_SSL3)
7722 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007723 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007724 /* SSLv3 does not have a "no_renegotiation" warning, so
7725 we send a fatal alert and abort the connection. */
7726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7727 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7728 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007729 }
7730 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7732#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7733 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7734 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7737 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7738 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007739 {
7740 return( ret );
7741 }
Paul Bakker48916f92012-09-16 19:57:18 +00007742 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007743 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7745 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7748 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007749 }
Paul Bakker48916f92012-09-16 19:57:18 +00007750 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007751
Hanno Becker90333da2017-10-10 11:27:13 +01007752 /* At this point, we don't know whether the renegotiation has been
7753 * completed or not. The cases to consider are the following:
7754 * 1) The renegotiation is complete. In this case, no new record
7755 * has been read yet.
7756 * 2) The renegotiation is incomplete because the client received
7757 * an application data record while awaiting the ServerHello.
7758 * 3) The renegotiation is incomplete because the client received
7759 * a non-handshake, non-application data message while awaiting
7760 * the ServerHello.
7761 * In each of these case, looping will be the proper action:
7762 * - For 1), the next iteration will read a new record and check
7763 * if it's application data.
7764 * - For 2), the loop condition isn't satisfied as application data
7765 * is present, hence continue is the same as break
7766 * - For 3), the loop condition is satisfied and read_record
7767 * will re-deliver the message that was held back by the client
7768 * when expecting the ServerHello.
7769 */
7770 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007771 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007772#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007774 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007775 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007776 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007777 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007780 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007782 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007783 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7788 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007791 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007792 }
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7797 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007798 }
7799
7800 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007801
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007802 /* We're going to return something now, cancel timer,
7803 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007805 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007806
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007808 /* If we requested renego but received AppData, resend HelloRequest.
7809 * Do it now, after setting in_offt, to avoid taking this branch
7810 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007812 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007814 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007815 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007818 return( ret );
7819 }
7820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007822#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007823 }
7824
7825 n = ( len < ssl->in_msglen )
7826 ? len : ssl->in_msglen;
7827
7828 memcpy( buf, ssl->in_offt, n );
7829 ssl->in_msglen -= n;
7830
7831 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007832 {
7833 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007834 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007835 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007837 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007838 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007839 /* more data available */
7840 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007841 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007844
Paul Bakker23986e52011-04-24 08:57:21 +00007845 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007846}
7847
7848/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007849 * Send application data to be encrypted by the SSL layer, taking care of max
7850 * fragment length and buffer size.
7851 *
7852 * According to RFC 5246 Section 6.2.1:
7853 *
7854 * Zero-length fragments of Application data MAY be sent as they are
7855 * potentially useful as a traffic analysis countermeasure.
7856 *
7857 * Therefore, it is possible that the input message length is 0 and the
7858 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007860static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007861 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007863 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7864 const size_t max_len = (size_t) ret;
7865
7866 if( ret < 0 )
7867 {
7868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7869 return( ret );
7870 }
7871
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007872 if( len > max_len )
7873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007875 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007878 "maximum fragment length: %d > %d",
7879 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007881 }
7882 else
7883#endif
7884 len = max_len;
7885 }
Paul Bakker887bd502011-06-08 13:10:54 +00007886
Paul Bakker5121ce52009-01-03 21:22:43 +00007887 if( ssl->out_left != 0 )
7888 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007889 /*
7890 * The user has previously tried to send the data and
7891 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7892 * written. In this case, we expect the high-level write function
7893 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007898 return( ret );
7899 }
7900 }
Paul Bakker887bd502011-06-08 13:10:54 +00007901 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007902 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007903 /*
7904 * The user is trying to send a message the first time, so we need to
7905 * copy the data into the internal buffers and setup the data structure
7906 * to keep track of partial writes
7907 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007908 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007910 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007911
Hanno Becker67bc7c32018-08-06 11:33:50 +01007912 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007915 return( ret );
7916 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007917 }
7918
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007919 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007920}
7921
7922/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007923 * Write application data, doing 1/n-1 splitting if necessary.
7924 *
7925 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007926 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007927 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007930static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007931 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007932{
7933 int ret;
7934
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007935 if( ssl->conf->cbc_record_splitting ==
7936 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007937 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7939 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7940 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007941 {
7942 return( ssl_write_real( ssl, buf, len ) );
7943 }
7944
7945 if( ssl->split_done == 0 )
7946 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007947 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007948 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007949 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007950 }
7951
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007952 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7953 return( ret );
7954 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007955
7956 return( ret + 1 );
7957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007959
7960/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007961 * Write application data (public-facing wrapper)
7962 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007963int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007964{
7965 int ret;
7966
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007968
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007969 if( ssl == NULL || ssl->conf == NULL )
7970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7971
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007972#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007973 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7974 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007975 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007976 return( ret );
7977 }
7978#endif
7979
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007980 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007981 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007982 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007983 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007985 return( ret );
7986 }
7987 }
7988
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007989#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007990 ret = ssl_write_split( ssl, buf, len );
7991#else
7992 ret = ssl_write_real( ssl, buf, len );
7993#endif
7994
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007996
7997 return( ret );
7998}
7999
8000/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008001 * Notify the peer that the connection is being closed
8002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008004{
8005 int ret;
8006
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008007 if( ssl == NULL || ssl->conf == NULL )
8008 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008011
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008012 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8018 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8019 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008022 return( ret );
8023 }
8024 }
8025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008027
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008028 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008029}
8030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008032{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008033 if( transform == NULL )
8034 return;
8035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008037 deflateEnd( &transform->ctx_deflate );
8038 inflateEnd( &transform->ctx_inflate );
8039#endif
8040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8042 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 mbedtls_md_free( &transform->md_ctx_enc );
8045 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008046
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008047 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008048}
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050#if defined(MBEDTLS_X509_CRT_PARSE_C)
8051static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008054
8055 while( cur != NULL )
8056 {
8057 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008059 cur = next;
8060 }
8061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008063
Gilles Peskine9b562d52018-04-25 20:32:43 +02008064void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008065{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008066 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8067
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008068 if( handshake == NULL )
8069 return;
8070
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008071#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8072 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8073 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008074 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008075 handshake->async_in_progress = 0;
8076 }
8077#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8078
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008079#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8080 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8081 mbedtls_md5_free( &handshake->fin_md5 );
8082 mbedtls_sha1_free( &handshake->fin_sha1 );
8083#endif
8084#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8085#if defined(MBEDTLS_SHA256_C)
8086 mbedtls_sha256_free( &handshake->fin_sha256 );
8087#endif
8088#if defined(MBEDTLS_SHA512_C)
8089 mbedtls_sha512_free( &handshake->fin_sha512 );
8090#endif
8091#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093#if defined(MBEDTLS_DHM_C)
8094 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#if defined(MBEDTLS_ECDH_C)
8097 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008098#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008099#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008100 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008101#if defined(MBEDTLS_SSL_CLI_C)
8102 mbedtls_free( handshake->ecjpake_cache );
8103 handshake->ecjpake_cache = NULL;
8104 handshake->ecjpake_cache_len = 0;
8105#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008106#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008107
Janos Follath4ae5c292016-02-10 11:27:43 +00008108#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8109 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008110 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008112#endif
8113
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008114#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8115 if( handshake->psk != NULL )
8116 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008117 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008118 mbedtls_free( handshake->psk );
8119 }
8120#endif
8121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8123 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008124 /*
8125 * Free only the linked list wrapper, not the keys themselves
8126 * since the belong to the SNI callback
8127 */
8128 if( handshake->sni_key_cert != NULL )
8129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008131
8132 while( cur != NULL )
8133 {
8134 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008136 cur = next;
8137 }
8138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#if defined(MBEDTLS_SSL_PROTO_DTLS)
8142 mbedtls_free( handshake->verify_cookie );
8143 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008144 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008145#endif
8146
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008147 mbedtls_platform_zeroize( handshake,
8148 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008149}
8150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008152{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008153 if( session == NULL )
8154 return;
8155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008157 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 mbedtls_x509_crt_free( session->peer_cert );
8160 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008161 }
Paul Bakkered27a042013-04-18 22:46:23 +02008162#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008163
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008164#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008166#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008167
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008168 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008169}
8170
Paul Bakker5121ce52009-01-03 21:22:43 +00008171/*
8172 * Free an SSL context
8173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008175{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008176 if( ssl == NULL )
8177 return;
8178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008180
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008181 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008182 {
Angus Grattond8213d02016-05-25 20:56:48 +10008183 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008185 }
8186
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008187 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008188 {
Angus Grattond8213d02016-05-25 20:56:48 +10008189 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008191 }
8192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008194 if( ssl->compress_buf != NULL )
8195 {
Angus Grattond8213d02016-05-25 20:56:48 +10008196 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008198 }
8199#endif
8200
Paul Bakker48916f92012-09-16 19:57:18 +00008201 if( ssl->transform )
8202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 mbedtls_ssl_transform_free( ssl->transform );
8204 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008205 }
8206
8207 if( ssl->handshake )
8208 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008209 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8211 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 mbedtls_free( ssl->handshake );
8214 mbedtls_free( ssl->transform_negotiate );
8215 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008216 }
8217
Paul Bakkerc0463502013-02-14 11:19:38 +01008218 if( ssl->session )
8219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 mbedtls_ssl_session_free( ssl->session );
8221 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008222 }
8223
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008224#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008225 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008226 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008227 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008229 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008230#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8233 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8236 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008237 }
8238#endif
8239
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008240#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008242#endif
8243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008245
Paul Bakker86f04f42013-02-14 11:20:09 +01008246 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008247 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008248}
8249
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008250/*
8251 * Initialze mbedtls_ssl_config
8252 */
8253void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8254{
8255 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8256}
8257
Simon Butcherc97b6972015-12-27 23:48:17 +00008258#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008259static int ssl_preset_default_hashes[] = {
8260#if defined(MBEDTLS_SHA512_C)
8261 MBEDTLS_MD_SHA512,
8262 MBEDTLS_MD_SHA384,
8263#endif
8264#if defined(MBEDTLS_SHA256_C)
8265 MBEDTLS_MD_SHA256,
8266 MBEDTLS_MD_SHA224,
8267#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008268#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008269 MBEDTLS_MD_SHA1,
8270#endif
8271 MBEDTLS_MD_NONE
8272};
Simon Butcherc97b6972015-12-27 23:48:17 +00008273#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008274
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008275static int ssl_preset_suiteb_ciphersuites[] = {
8276 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8277 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8278 0
8279};
8280
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008281#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008282static int ssl_preset_suiteb_hashes[] = {
8283 MBEDTLS_MD_SHA256,
8284 MBEDTLS_MD_SHA384,
8285 MBEDTLS_MD_NONE
8286};
8287#endif
8288
8289#if defined(MBEDTLS_ECP_C)
8290static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8291 MBEDTLS_ECP_DP_SECP256R1,
8292 MBEDTLS_ECP_DP_SECP384R1,
8293 MBEDTLS_ECP_DP_NONE
8294};
8295#endif
8296
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008297/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008298 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008299 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008300int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008301 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008302{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008303#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008304 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008305#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008306
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008307 /* Use the functions here so that they are covered in tests,
8308 * but otherwise access member directly for efficiency */
8309 mbedtls_ssl_conf_endpoint( conf, endpoint );
8310 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008311
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008312 /*
8313 * Things that are common to all presets
8314 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008315#if defined(MBEDTLS_SSL_CLI_C)
8316 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8317 {
8318 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8319#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8320 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8321#endif
8322 }
8323#endif
8324
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008325#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008326 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008327#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008328
8329#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8330 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8331#endif
8332
8333#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8334 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8335#endif
8336
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008337#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8338 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8339#endif
8340
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008341#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008342 conf->f_cookie_write = ssl_cookie_write_dummy;
8343 conf->f_cookie_check = ssl_cookie_check_dummy;
8344#endif
8345
8346#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8347 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8348#endif
8349
Janos Follath088ce432017-04-10 12:42:31 +01008350#if defined(MBEDTLS_SSL_SRV_C)
8351 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8352#endif
8353
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008354#if defined(MBEDTLS_SSL_PROTO_DTLS)
8355 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8356 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8357#endif
8358
8359#if defined(MBEDTLS_SSL_RENEGOTIATION)
8360 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008361 memset( conf->renego_period, 0x00, 2 );
8362 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008363#endif
8364
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008365#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8366 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8367 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008368 const unsigned char dhm_p[] =
8369 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8370 const unsigned char dhm_g[] =
8371 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8372
Hanno Beckera90658f2017-10-04 15:29:08 +01008373 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8374 dhm_p, sizeof( dhm_p ),
8375 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008376 {
8377 return( ret );
8378 }
8379 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008380#endif
8381
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008382 /*
8383 * Preset-specific defaults
8384 */
8385 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008386 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008387 /*
8388 * NSA Suite B
8389 */
8390 case MBEDTLS_SSL_PRESET_SUITEB:
8391 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8392 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8393 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8394 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8395
8396 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8397 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8398 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8399 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8400 ssl_preset_suiteb_ciphersuites;
8401
8402#if defined(MBEDTLS_X509_CRT_PARSE_C)
8403 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008404#endif
8405
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008406#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008407 conf->sig_hashes = ssl_preset_suiteb_hashes;
8408#endif
8409
8410#if defined(MBEDTLS_ECP_C)
8411 conf->curve_list = ssl_preset_suiteb_curves;
8412#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008413 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008414
8415 /*
8416 * Default
8417 */
8418 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008419 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8420 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8421 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8422 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8423 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8424 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8425 MBEDTLS_SSL_MIN_MINOR_VERSION :
8426 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008427 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8428 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8429
8430#if defined(MBEDTLS_SSL_PROTO_DTLS)
8431 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8432 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8433#endif
8434
8435 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8436 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8437 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8438 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8439 mbedtls_ssl_list_ciphersuites();
8440
8441#if defined(MBEDTLS_X509_CRT_PARSE_C)
8442 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8443#endif
8444
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008445#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008446 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008447#endif
8448
8449#if defined(MBEDTLS_ECP_C)
8450 conf->curve_list = mbedtls_ecp_grp_id_list();
8451#endif
8452
8453#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8454 conf->dhm_min_bitlen = 1024;
8455#endif
8456 }
8457
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008458 return( 0 );
8459}
8460
8461/*
8462 * Free mbedtls_ssl_config
8463 */
8464void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8465{
8466#if defined(MBEDTLS_DHM_C)
8467 mbedtls_mpi_free( &conf->dhm_P );
8468 mbedtls_mpi_free( &conf->dhm_G );
8469#endif
8470
8471#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8472 if( conf->psk != NULL )
8473 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008474 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008475 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008476 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008477 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008478 }
8479
8480 if( conf->psk_identity != NULL )
8481 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008482 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008483 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008484 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008485 conf->psk_identity_len = 0;
8486 }
8487#endif
8488
8489#if defined(MBEDTLS_X509_CRT_PARSE_C)
8490 ssl_key_cert_free( conf->key_cert );
8491#endif
8492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008493 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008494}
8495
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008496#if defined(MBEDTLS_PK_C) && \
8497 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008498/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503#if defined(MBEDTLS_RSA_C)
8504 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8505 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507#if defined(MBEDTLS_ECDSA_C)
8508 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8509 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008511 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008512}
8513
Hanno Becker7e5437a2017-04-28 17:15:26 +01008514unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8515{
8516 switch( type ) {
8517 case MBEDTLS_PK_RSA:
8518 return( MBEDTLS_SSL_SIG_RSA );
8519 case MBEDTLS_PK_ECDSA:
8520 case MBEDTLS_PK_ECKEY:
8521 return( MBEDTLS_SSL_SIG_ECDSA );
8522 default:
8523 return( MBEDTLS_SSL_SIG_ANON );
8524 }
8525}
8526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008528{
8529 switch( sig )
8530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531#if defined(MBEDTLS_RSA_C)
8532 case MBEDTLS_SSL_SIG_RSA:
8533 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535#if defined(MBEDTLS_ECDSA_C)
8536 case MBEDTLS_SSL_SIG_ECDSA:
8537 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008538#endif
8539 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008541 }
8542}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008543#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008544
Hanno Becker7e5437a2017-04-28 17:15:26 +01008545#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8546 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8547
8548/* Find an entry in a signature-hash set matching a given hash algorithm. */
8549mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8550 mbedtls_pk_type_t sig_alg )
8551{
8552 switch( sig_alg )
8553 {
8554 case MBEDTLS_PK_RSA:
8555 return( set->rsa );
8556 case MBEDTLS_PK_ECDSA:
8557 return( set->ecdsa );
8558 default:
8559 return( MBEDTLS_MD_NONE );
8560 }
8561}
8562
8563/* Add a signature-hash-pair to a signature-hash set */
8564void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8565 mbedtls_pk_type_t sig_alg,
8566 mbedtls_md_type_t md_alg )
8567{
8568 switch( sig_alg )
8569 {
8570 case MBEDTLS_PK_RSA:
8571 if( set->rsa == MBEDTLS_MD_NONE )
8572 set->rsa = md_alg;
8573 break;
8574
8575 case MBEDTLS_PK_ECDSA:
8576 if( set->ecdsa == MBEDTLS_MD_NONE )
8577 set->ecdsa = md_alg;
8578 break;
8579
8580 default:
8581 break;
8582 }
8583}
8584
8585/* Allow exactly one hash algorithm for each signature. */
8586void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8587 mbedtls_md_type_t md_alg )
8588{
8589 set->rsa = md_alg;
8590 set->ecdsa = md_alg;
8591}
8592
8593#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8594 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8595
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008596/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008597 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008600{
8601 switch( hash )
8602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603#if defined(MBEDTLS_MD5_C)
8604 case MBEDTLS_SSL_HASH_MD5:
8605 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607#if defined(MBEDTLS_SHA1_C)
8608 case MBEDTLS_SSL_HASH_SHA1:
8609 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611#if defined(MBEDTLS_SHA256_C)
8612 case MBEDTLS_SSL_HASH_SHA224:
8613 return( MBEDTLS_MD_SHA224 );
8614 case MBEDTLS_SSL_HASH_SHA256:
8615 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617#if defined(MBEDTLS_SHA512_C)
8618 case MBEDTLS_SSL_HASH_SHA384:
8619 return( MBEDTLS_MD_SHA384 );
8620 case MBEDTLS_SSL_HASH_SHA512:
8621 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008622#endif
8623 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008625 }
8626}
8627
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008628/*
8629 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8630 */
8631unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8632{
8633 switch( md )
8634 {
8635#if defined(MBEDTLS_MD5_C)
8636 case MBEDTLS_MD_MD5:
8637 return( MBEDTLS_SSL_HASH_MD5 );
8638#endif
8639#if defined(MBEDTLS_SHA1_C)
8640 case MBEDTLS_MD_SHA1:
8641 return( MBEDTLS_SSL_HASH_SHA1 );
8642#endif
8643#if defined(MBEDTLS_SHA256_C)
8644 case MBEDTLS_MD_SHA224:
8645 return( MBEDTLS_SSL_HASH_SHA224 );
8646 case MBEDTLS_MD_SHA256:
8647 return( MBEDTLS_SSL_HASH_SHA256 );
8648#endif
8649#if defined(MBEDTLS_SHA512_C)
8650 case MBEDTLS_MD_SHA384:
8651 return( MBEDTLS_SSL_HASH_SHA384 );
8652 case MBEDTLS_MD_SHA512:
8653 return( MBEDTLS_SSL_HASH_SHA512 );
8654#endif
8655 default:
8656 return( MBEDTLS_SSL_HASH_NONE );
8657 }
8658}
8659
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008660#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008661/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008662 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008663 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008664 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008665int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008668
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008669 if( ssl->conf->curve_list == NULL )
8670 return( -1 );
8671
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008672 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008673 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008674 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008675
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008676 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008677}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008678#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008679
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008680#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008681/*
8682 * Check if a hash proposed by the peer is in our list.
8683 * Return 0 if we're willing to use it, -1 otherwise.
8684 */
8685int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8686 mbedtls_md_type_t md )
8687{
8688 const int *cur;
8689
8690 if( ssl->conf->sig_hashes == NULL )
8691 return( -1 );
8692
8693 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8694 if( *cur == (int) md )
8695 return( 0 );
8696
8697 return( -1 );
8698}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008699#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701#if defined(MBEDTLS_X509_CRT_PARSE_C)
8702int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8703 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008704 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008705 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008706{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008707 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008709 int usage = 0;
8710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008712 const char *ext_oid;
8713 size_t ext_len;
8714#endif
8715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8717 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008718 ((void) cert);
8719 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008720 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008721#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8724 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008725 {
8726 /* Server part of the key exchange */
8727 switch( ciphersuite->key_exchange )
8728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 case MBEDTLS_KEY_EXCHANGE_RSA:
8730 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008731 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008732 break;
8733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8735 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8736 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8737 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008738 break;
8739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8741 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008742 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008743 break;
8744
8745 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 case MBEDTLS_KEY_EXCHANGE_NONE:
8747 case MBEDTLS_KEY_EXCHANGE_PSK:
8748 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8749 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008750 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008751 usage = 0;
8752 }
8753 }
8754 else
8755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8757 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008758 }
8759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008761 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008762 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008763 ret = -1;
8764 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008765#else
8766 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8770 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8773 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008774 }
8775 else
8776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8778 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008779 }
8780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008782 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008783 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008784 ret = -1;
8785 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008787
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008788 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008791
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008792/*
8793 * Convert version numbers to/from wire format
8794 * and, for DTLS, to/from TLS equivalent.
8795 *
8796 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008797 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008798 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8799 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008802 unsigned char ver[2] )
8803{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804#if defined(MBEDTLS_SSL_PROTO_DTLS)
8805 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008808 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8809
8810 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8811 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8812 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008813 else
8814#else
8815 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008816#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008817 {
8818 ver[0] = (unsigned char) major;
8819 ver[1] = (unsigned char) minor;
8820 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008821}
8822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008823void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008824 const unsigned char ver[2] )
8825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826#if defined(MBEDTLS_SSL_PROTO_DTLS)
8827 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008828 {
8829 *major = 255 - ver[0] + 2;
8830 *minor = 255 - ver[1] + 1;
8831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008833 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8834 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008835 else
8836#else
8837 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008838#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008839 {
8840 *major = ver[0];
8841 *minor = ver[1];
8842 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008843}
8844
Simon Butcher99000142016-10-13 17:21:01 +01008845int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8846{
8847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8848 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8849 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8850
8851 switch( md )
8852 {
8853#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8854#if defined(MBEDTLS_MD5_C)
8855 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008856 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008857#endif
8858#if defined(MBEDTLS_SHA1_C)
8859 case MBEDTLS_SSL_HASH_SHA1:
8860 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8861 break;
8862#endif
8863#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8864#if defined(MBEDTLS_SHA512_C)
8865 case MBEDTLS_SSL_HASH_SHA384:
8866 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8867 break;
8868#endif
8869#if defined(MBEDTLS_SHA256_C)
8870 case MBEDTLS_SSL_HASH_SHA256:
8871 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8872 break;
8873#endif
8874 default:
8875 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8876 }
8877
8878 return 0;
8879#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8880 (void) ssl;
8881 (void) md;
8882
8883 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8884#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8885}
8886
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008887#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8888 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8889int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8890 unsigned char *output,
8891 unsigned char *data, size_t data_len )
8892{
8893 int ret = 0;
8894 mbedtls_md5_context mbedtls_md5;
8895 mbedtls_sha1_context mbedtls_sha1;
8896
8897 mbedtls_md5_init( &mbedtls_md5 );
8898 mbedtls_sha1_init( &mbedtls_sha1 );
8899
8900 /*
8901 * digitally-signed struct {
8902 * opaque md5_hash[16];
8903 * opaque sha_hash[20];
8904 * };
8905 *
8906 * md5_hash
8907 * MD5(ClientHello.random + ServerHello.random
8908 * + ServerParams);
8909 * sha_hash
8910 * SHA(ClientHello.random + ServerHello.random
8911 * + ServerParams);
8912 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008913 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008914 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008916 goto exit;
8917 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008918 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008919 ssl->handshake->randbytes, 64 ) ) != 0 )
8920 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008922 goto exit;
8923 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008924 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008925 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008927 goto exit;
8928 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008929 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008930 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008932 goto exit;
8933 }
8934
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008935 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008936 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008938 goto exit;
8939 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008940 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008941 ssl->handshake->randbytes, 64 ) ) != 0 )
8942 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008944 goto exit;
8945 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008946 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008947 data_len ) ) != 0 )
8948 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008950 goto exit;
8951 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008952 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008953 output + 16 ) ) != 0 )
8954 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008956 goto exit;
8957 }
8958
8959exit:
8960 mbedtls_md5_free( &mbedtls_md5 );
8961 mbedtls_sha1_free( &mbedtls_sha1 );
8962
8963 if( ret != 0 )
8964 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8965 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8966
8967 return( ret );
8968
8969}
8970#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8971 MBEDTLS_SSL_PROTO_TLS1_1 */
8972
8973#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8974 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8975int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02008976 unsigned char *hash, size_t *hashlen,
8977 unsigned char *data, size_t data_len,
8978 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008979{
8980 int ret = 0;
8981 mbedtls_md_context_t ctx;
8982 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02008983 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008984
8985 mbedtls_md_init( &ctx );
8986
8987 /*
8988 * digitally-signed struct {
8989 * opaque client_random[32];
8990 * opaque server_random[32];
8991 * ServerDHParams params;
8992 * };
8993 */
8994 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
8995 {
8996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
8997 goto exit;
8998 }
8999 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9000 {
9001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9002 goto exit;
9003 }
9004 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9005 {
9006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9007 goto exit;
9008 }
9009 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9010 {
9011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9012 goto exit;
9013 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009014 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009015 {
9016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9017 goto exit;
9018 }
9019
9020exit:
9021 mbedtls_md_free( &ctx );
9022
9023 if( ret != 0 )
9024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9025 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9026
9027 return( ret );
9028}
9029#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9030 MBEDTLS_SSL_PROTO_TLS1_2 */
9031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032#endif /* MBEDTLS_SSL_TLS_C */