blob: cceb96fd054ade3e8dfc6081ef0ef870724757fb [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
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
58
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010059/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020063 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065#else
66 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067#endif
68 return( 0 );
69}
70
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020071/*
72 * Start a timer.
73 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020077 if( ssl->f_set_timer == NULL )
78 return;
79
80 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
81 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082}
83
84/*
85 * Return -1 is timer is expired, 0 if it isn't.
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020089 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020090 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020091
92 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020093 {
94 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020096 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097
98 return( 0 );
99}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100101static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
102 mbedtls_ssl_transform *transform );
103static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
104 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100105
106#define SSL_DONT_FORCE_FLUSH 0
107#define SSL_FORCE_FLUSH 1
108
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100110
Hanno Beckera67dee22018-08-22 10:05:20 +0100111static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100112static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100113{
Hanno Becker11682cc2018-08-22 14:41:02 +0100114 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100115
116 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100117 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100118
119 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
120}
121
Hanno Becker67bc7c32018-08-06 11:33:50 +0100122static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
123{
Hanno Becker11682cc2018-08-22 14:41:02 +0100124 size_t const bytes_written = ssl->out_left;
125 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100126
127 /* Double-check that the write-index hasn't gone
128 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100129 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100130 {
131 /* Should never happen... */
132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
133 }
134
135 return( (int) ( mtu - bytes_written ) );
136}
137
138static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
139{
140 int ret;
141 size_t remaining, expansion;
142 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
143
144#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
145 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
146
147 if( max_len > mfl )
148 max_len = mfl;
149#endif
150
151 ret = ssl_get_remaining_space_in_datagram( ssl );
152 if( ret < 0 )
153 return( ret );
154 remaining = (size_t) ret;
155
156 ret = mbedtls_ssl_get_record_expansion( ssl );
157 if( ret < 0 )
158 return( ret );
159 expansion = (size_t) ret;
160
161 if( remaining <= expansion )
162 return( 0 );
163
164 remaining -= expansion;
165 if( remaining >= max_len )
166 remaining = max_len;
167
168 return( (int) remaining );
169}
170
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200171/*
172 * Double the retransmit timeout value, within the allowed range,
173 * returning -1 if the maximum value has already been reached.
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200176{
177 uint32_t new_timeout;
178
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200179 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200180 return( -1 );
181
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200182 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
183 * in the following way: after the initial transmission and a first
184 * retransmission, back off to a temporary estimated MTU of 508 bytes.
185 * This value is guaranteed to be deliverable (if not guaranteed to be
186 * delivered) of any compliant IPv4 (and IPv6) network, and should work
187 * on most non-IP stacks too. */
188 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
189 ssl->handshake->mtu = 508;
190
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200191 new_timeout = 2 * ssl->handshake->retransmit_timeout;
192
193 /* Avoid arithmetic overflow and range overflow */
194 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200195 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200196 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200197 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200198 }
199
200 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202 ssl->handshake->retransmit_timeout ) );
203
204 return( 0 );
205}
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200208{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200209 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211 ssl->handshake->retransmit_timeout ) );
212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200216/*
217 * Convert max_fragment_length codes to length.
218 * RFC 6066 says:
219 * enum{
220 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
221 * } MaxFragmentLength;
222 * and we add 0 -> extension unused
223 */
Angus Grattond8213d02016-05-25 20:56:48 +1000224static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200225{
Angus Grattond8213d02016-05-25 20:56:48 +1000226 switch( mfl )
227 {
228 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
229 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
230 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
231 return 512;
232 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
233 return 1024;
234 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
235 return 2048;
236 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
237 return 4096;
238 default:
239 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
240 }
241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200243
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200244#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_ssl_session_free( dst );
248 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200251 if( src->peer_cert != NULL )
252 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200253 int ret;
254
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200255 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200256 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200257 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200262 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265 dst->peer_cert = NULL;
266 return( ret );
267 }
268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200270
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200271#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200272 if( src->ticket != NULL )
273 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200274 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200275 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200276 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200277
278 memcpy( dst->ticket, src->ticket, src->ticket_len );
279 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200280#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200281
282 return( 0 );
283}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200284#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
287int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200288 const unsigned char *key_enc, const unsigned char *key_dec,
289 size_t keylen,
290 const unsigned char *iv_enc, const unsigned char *iv_dec,
291 size_t ivlen,
292 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200293 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
295int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
296int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
297int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
298int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
299#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000300
Paul Bakker5121ce52009-01-03 21:22:43 +0000301/*
302 * Key material generation
303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200305static int ssl3_prf( const unsigned char *secret, size_t slen,
306 const char *label,
307 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000308 unsigned char *dstbuf, size_t dlen )
309{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100310 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000311 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200312 mbedtls_md5_context md5;
313 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000314 unsigned char padding[16];
315 unsigned char sha1sum[20];
316 ((void)label);
317
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200318 mbedtls_md5_init( &md5 );
319 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200320
Paul Bakker5f70b252012-09-13 14:23:06 +0000321 /*
322 * SSLv3:
323 * block =
324 * MD5( secret + SHA1( 'A' + secret + random ) ) +
325 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
326 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
327 * ...
328 */
329 for( i = 0; i < dlen / 16; i++ )
330 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200331 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000332
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100333 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100334 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100335 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100336 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100337 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100338 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100339 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100340 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100342 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000343
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100344 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100345 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100346 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100347 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100348 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100349 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100350 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100351 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000352 }
353
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100354exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200355 mbedtls_md5_free( &md5 );
356 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000357
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500358 mbedtls_platform_zeroize( padding, sizeof( padding ) );
359 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000360
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100361 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200366static int tls1_prf( const unsigned char *secret, size_t slen,
367 const char *label,
368 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000369 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000370{
Paul Bakker23986e52011-04-24 08:57:21 +0000371 size_t nb, hs;
372 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200373 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 unsigned char tmp[128];
375 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 const mbedtls_md_info_t *md_info;
377 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100378 int ret;
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 hs = ( slen + 1 ) / 2;
386 S1 = secret;
387 S2 = secret + slen - hs;
388
389 nb = strlen( label );
390 memcpy( tmp + 20, label, nb );
391 memcpy( tmp + 20 + nb, random, rlen );
392 nb += rlen;
393
394 /*
395 * First compute P_md5(secret,label+random)[0..dlen]
396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100401 return( ret );
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
404 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
405 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407 for( i = 0; i < dlen; i += 16 )
408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_reset ( &md_ctx );
410 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
411 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_md_hmac_reset ( &md_ctx );
414 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
415 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
418
419 for( j = 0; j < k; j++ )
420 dstbuf[i + j] = h_i[j];
421 }
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100424
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 /*
426 * XOR out with P_sha1(secret,label+random)[0..dlen]
427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 return( ret );
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
435 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
436 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 for( i = 0; i < dlen; i += 20 )
439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_reset ( &md_ctx );
441 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
442 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_md_hmac_reset ( &md_ctx );
445 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
446 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
449
450 for( j = 0; j < k; j++ )
451 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
452 }
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500456 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
457 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 return( 0 );
460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
464static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100465 const unsigned char *secret, size_t slen,
466 const char *label,
467 const unsigned char *random, size_t rlen,
468 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000469{
470 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100471 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000472 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
474 const mbedtls_md_info_t *md_info;
475 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100476 int ret;
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100484
485 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487
488 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100489 memcpy( tmp + md_len, label, nb );
490 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000491 nb += rlen;
492
493 /*
494 * Compute P_<hash>(secret, label + random)[0..dlen]
495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497 return( ret );
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
500 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
501 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100502
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100503 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_md_hmac_reset ( &md_ctx );
506 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
507 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_md_hmac_reset ( &md_ctx );
510 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
511 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000512
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100513 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000514
515 for( j = 0; j < k; j++ )
516 dstbuf[i + j] = h_i[j];
517 }
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100520
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500521 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
522 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523
524 return( 0 );
525}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100528static int tls_prf_sha256( const unsigned char *secret, size_t slen,
529 const char *label,
530 const unsigned char *random, size_t rlen,
531 unsigned char *dstbuf, size_t dlen )
532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100534 label, random, rlen, dstbuf, dlen ) );
535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200539static int tls_prf_sha384( const unsigned char *secret, size_t slen,
540 const char *label,
541 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000542 unsigned char *dstbuf, size_t dlen )
543{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100545 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#endif /* MBEDTLS_SHA512_C */
548#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
553 defined(MBEDTLS_SSL_PROTO_TLS1_1)
554static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200555#endif
Paul Bakker380da532012-04-18 16:10:25 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#if defined(MBEDTLS_SSL_PROTO_SSL3)
558static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
559static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200560#endif
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
563static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
564static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200565#endif
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
568#if defined(MBEDTLS_SHA256_C)
569static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
570static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
571static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200572#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_SHA512_C)
575static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
576static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
577static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200583 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 unsigned char keyblk[256];
586 unsigned char *key1;
587 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100588 unsigned char *mac_enc;
589 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000590 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200591 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 const mbedtls_cipher_info_t *cipher_info;
593 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_ssl_session *session = ssl->session_negotiate;
596 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
597 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100602 if( cipher_info == NULL )
603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100605 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100607 }
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100610 if( md_info == NULL )
611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100613 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100615 }
616
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000618 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SSL_PROTO_SSL3)
621 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000622 {
Paul Bakker48916f92012-09-16 19:57:18 +0000623 handshake->tls_prf = ssl3_prf;
624 handshake->calc_verify = ssl_calc_verify_ssl;
625 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000626 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200627 else
628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
630 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 {
Paul Bakker48916f92012-09-16 19:57:18 +0000632 handshake->tls_prf = tls1_prf;
633 handshake->calc_verify = ssl_calc_verify_tls;
634 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000635 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200636 else
637#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
639#if defined(MBEDTLS_SHA512_C)
640 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
641 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000642 {
Paul Bakker48916f92012-09-16 19:57:18 +0000643 handshake->tls_prf = tls_prf_sha384;
644 handshake->calc_verify = ssl_calc_verify_tls_sha384;
645 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200648#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SHA256_C)
650 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651 {
Paul Bakker48916f92012-09-16 19:57:18 +0000652 handshake->tls_prf = tls_prf_sha256;
653 handshake->calc_verify = ssl_calc_verify_tls_sha256;
654 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000655 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200656 else
657#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200662 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000663
664 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 * SSLv3:
666 * master =
667 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
668 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
669 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200670 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200671 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 * master = PRF( premaster, "master secret", randbytes )[0..47]
673 */
Paul Bakker0a597072012-09-25 21:55:46 +0000674 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000677 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
680 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200681 {
682 unsigned char session_hash[48];
683 size_t hash_len;
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200686
687 ssl->handshake->calc_verify( ssl, session_hash );
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
690 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200693 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200695 {
696 hash_len = 48;
697 }
698 else
699#endif
700 hash_len = 32;
701 }
702 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200704 hash_len = 36;
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200707
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100708 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
709 "extended master secret",
710 session_hash, hash_len,
711 session->master, 48 );
712 if( ret != 0 )
713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100715 return( ret );
716 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200717
718 }
719 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200720#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100721 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
722 "master secret",
723 handshake->randbytes, 64,
724 session->master, 48 );
725 if( ret != 0 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100728 return( ret );
729 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200730
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500731 mbedtls_platform_zeroize( handshake->premaster,
732 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 }
734 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
737 /*
738 * Swap the client and server random values.
739 */
Paul Bakker48916f92012-09-16 19:57:18 +0000740 memcpy( tmp, handshake->randbytes, 64 );
741 memcpy( handshake->randbytes, tmp + 32, 32 );
742 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
745 /*
746 * SSLv3:
747 * key block =
748 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
749 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
750 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
751 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
752 * ...
753 *
754 * TLSv1:
755 * key block = PRF( master, "key expansion", randbytes )
756 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100757 ret = handshake->tls_prf( session->master, 48, "key expansion",
758 handshake->randbytes, 64, keyblk, 256 );
759 if( ret != 0 )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100762 return( ret );
763 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
766 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
767 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
768 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
769 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500771 mbedtls_platform_zeroize( handshake->randbytes,
772 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774 /*
775 * Determine the appropriate key, IV and MAC length.
776 */
Paul Bakker68884e32013-01-07 18:20:04 +0100777
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200778 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200781 cipher_info->mode == MBEDTLS_MODE_CCM ||
782 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000783 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200784 size_t taglen, explicit_ivlen;
785
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200786 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000787 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200788
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200789 /* All modes haves 96-bit IVs;
790 * GCM and CCM has 4 implicit and 8 explicit bytes
791 * ChachaPoly has all 12 bytes implicit
792 */
Paul Bakker68884e32013-01-07 18:20:04 +0100793 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200794 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
795 transform->fixed_ivlen = 12;
796 else
797 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200798
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200799 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
800 taglen = transform->ciphersuite_info->flags &
801 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
802
803
804 /* Minimum length of encrypted record */
805 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
806 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100807 }
808 else
809 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200810 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
812 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200815 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100816 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200818 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000819 mac_key_len = mbedtls_md_get_size( md_info );
820 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200823 /*
824 * If HMAC is to be truncated, we shall keep the leftmost bytes,
825 * (rfc 6066 page 13 or rfc 2104 section 4),
826 * so we only need to adjust the length here.
827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000831
832#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
833 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000834 * HMAC implementation which also truncates the key
835 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000836 mac_key_len = transform->maclen;
837#endif
838 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200840
841 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100842 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200844 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200846 transform->minlen = transform->maclen;
847 else
Paul Bakker68884e32013-01-07 18:20:04 +0100848 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200849 /*
850 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100851 * 1. if EtM is in use: one block plus MAC
852 * otherwise: * first multiple of blocklen greater than maclen
853 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
856 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100857 {
858 transform->minlen = transform->maclen
859 + cipher_info->block_size;
860 }
861 else
862#endif
863 {
864 transform->minlen = transform->maclen
865 + cipher_info->block_size
866 - transform->maclen % cipher_info->block_size;
867 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
870 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
871 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200872 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100873 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
876 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
877 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200878 {
879 transform->minlen += transform->ivlen;
880 }
881 else
882#endif
883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200886 }
Paul Bakker68884e32013-01-07 18:20:04 +0100887 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 }
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000891 transform->keylen, transform->minlen, transform->ivlen,
892 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
894 /*
895 * Finally setup the cipher contexts, IVs and MAC secrets.
896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200898 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000900 key1 = keyblk + mac_key_len * 2;
901 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Paul Bakker68884e32013-01-07 18:20:04 +0100903 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000904 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000906 /*
907 * This is not used in TLS v1.1.
908 */
Paul Bakker48916f92012-09-16 19:57:18 +0000909 iv_copy_len = ( transform->fixed_ivlen ) ?
910 transform->fixed_ivlen : transform->ivlen;
911 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
912 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000913 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 }
915 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#endif /* MBEDTLS_SSL_CLI_C */
917#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200918 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000920 key1 = keyblk + mac_key_len * 2 + transform->keylen;
921 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Hanno Becker81c7b182017-11-09 18:39:33 +0000923 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100924 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000926 /*
927 * This is not used in TLS v1.1.
928 */
Paul Bakker48916f92012-09-16 19:57:18 +0000929 iv_copy_len = ( transform->fixed_ivlen ) ?
930 transform->fixed_ivlen : transform->ivlen;
931 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
932 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000933 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100935 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100940 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_PROTO_SSL3)
943 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100944 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000945 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
948 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100949 }
950
Hanno Becker81c7b182017-11-09 18:39:33 +0000951 memcpy( transform->mac_enc, mac_enc, mac_key_len );
952 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100953 }
954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
956#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
957 defined(MBEDTLS_SSL_PROTO_TLS1_2)
958 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100959 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100960 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
961 For AEAD-based ciphersuites, there is nothing to do here. */
962 if( mac_key_len != 0 )
963 {
964 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
965 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
966 }
Paul Bakker68884e32013-01-07 18:20:04 +0100967 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200968 else
969#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200973 }
Paul Bakker68884e32013-01-07 18:20:04 +0100974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
976 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000977 {
978 int ret = 0;
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100983 transform->iv_enc, transform->iv_dec,
984 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100985 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000986 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
989 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000990 }
991 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000993
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200994#if defined(MBEDTLS_SSL_EXPORT_KEYS)
995 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100996 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200997 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
998 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001000 iv_copy_len );
1001 }
1002#endif
1003
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001004 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001005 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001008 return( ret );
1009 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001010
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001011 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001012 cipher_info ) ) != 0 )
1013 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001015 return( ret );
1016 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001019 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001023 return( ret );
1024 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001027 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001031 return( ret );
1032 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_CIPHER_MODE_CBC)
1035 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1038 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001041 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001042 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1045 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001048 return( ret );
1049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001053 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001056 // Initialize compression
1057 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001059 {
Paul Bakker16770332013-10-11 09:59:44 +02001060 if( ssl->compress_buf == NULL )
1061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001063 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001064 if( ssl->compress_buf == NULL )
1065 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001067 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001069 }
1070 }
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001073
Paul Bakker48916f92012-09-16 19:57:18 +00001074 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1075 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001076
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001077 if( deflateInit( &transform->ctx_deflate,
1078 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001079 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1082 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001083 }
1084 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
1089 return( 0 );
1090}
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_SSL_PROTO_SSL3)
1093void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001094{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001095 mbedtls_md5_context md5;
1096 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 unsigned char pad_1[48];
1098 unsigned char pad_2[48];
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001102 mbedtls_md5_init( &md5 );
1103 mbedtls_sha1_init( &sha1 );
1104
1105 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1106 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Paul Bakker380da532012-04-18 16:10:25 +00001108 memset( pad_1, 0x36, 48 );
1109 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001111 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1112 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1113 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001115 mbedtls_md5_starts_ret( &md5 );
1116 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1117 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1118 mbedtls_md5_update_ret( &md5, hash, 16 );
1119 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1122 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1123 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001124
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001125 mbedtls_sha1_starts_ret( &sha1 );
1126 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1127 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1128 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1129 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001133
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001134 mbedtls_md5_free( &md5 );
1135 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001136
Paul Bakker380da532012-04-18 16:10:25 +00001137 return;
1138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1142void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001143{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001144 mbedtls_md5_context md5;
1145 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001148
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001149 mbedtls_md5_init( &md5 );
1150 mbedtls_sha1_init( &sha1 );
1151
1152 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1153 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001154
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001155 mbedtls_md5_finish_ret( &md5, hash );
1156 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001160
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001161 mbedtls_md5_free( &md5 );
1162 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001163
Paul Bakker380da532012-04-18 16:10:25 +00001164 return;
1165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1169#if defined(MBEDTLS_SHA256_C)
1170void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001171{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001172 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001174 mbedtls_sha256_init( &sha256 );
1175
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001177
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001178 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001179 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001183
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001184 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001185
Paul Bakker380da532012-04-18 16:10:25 +00001186 return;
1187}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SHA512_C)
1191void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001192{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001193 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001194
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001195 mbedtls_sha512_init( &sha512 );
1196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001198
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001199 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001200 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001205 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001206
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 return;
1208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_SHA512_C */
1210#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1213int 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 +02001214{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001215 unsigned char *p = ssl->handshake->premaster;
1216 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001217 const unsigned char *psk = ssl->conf->psk;
1218 size_t psk_len = ssl->conf->psk_len;
1219
1220 /* If the psk callback was called, use its result */
1221 if( ssl->handshake->psk != NULL )
1222 {
1223 psk = ssl->handshake->psk;
1224 psk_len = ssl->handshake->psk_len;
1225 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001226
1227 /*
1228 * PMS = struct {
1229 * opaque other_secret<0..2^16-1>;
1230 * opaque psk<0..2^16-1>;
1231 * };
1232 * with "other_secret" depending on the particular key exchange
1233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1235 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001236 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001237 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001239
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001240 *(p++) = (unsigned char)( psk_len >> 8 );
1241 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001242
1243 if( end < p || (size_t)( end - p ) < psk_len )
1244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1245
1246 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001247 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001248 }
1249 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1251#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1252 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001253 {
1254 /*
1255 * other_secret already set by the ClientKeyExchange message,
1256 * and is 48 bytes long
1257 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001258 if( end - p < 2 )
1259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1260
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001261 *p++ = 0;
1262 *p++ = 48;
1263 p += 48;
1264 }
1265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1267#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1268 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001269 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001270 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001271 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001272
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001273 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001275 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001276 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001279 return( ret );
1280 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001281 *(p++) = (unsigned char)( len >> 8 );
1282 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001283 p += len;
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001286 }
1287 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1289#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1290 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001291 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001292 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001293 size_t zlen;
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001296 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001297 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001300 return( ret );
1301 }
1302
1303 *(p++) = (unsigned char)( zlen >> 8 );
1304 *(p++) = (unsigned char)( zlen );
1305 p += zlen;
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001308 }
1309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001314 }
1315
1316 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001317 if( end - p < 2 )
1318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001319
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001320 *(p++) = (unsigned char)( psk_len >> 8 );
1321 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001322
1323 if( end < p || (size_t)( end - p ) < psk_len )
1324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1325
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001326 memcpy( p, psk, psk_len );
1327 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001328
1329 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1330
1331 return( 0 );
1332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001336/*
1337 * SSLv3.0 MAC functions
1338 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001339#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001340static void ssl_mac( mbedtls_md_context_t *md_ctx,
1341 const unsigned char *secret,
1342 const unsigned char *buf, size_t len,
1343 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001344 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001345{
1346 unsigned char header[11];
1347 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001348 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1350 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001351
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001352 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001354 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001355 else
Paul Bakker68884e32013-01-07 18:20:04 +01001356 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
1358 memcpy( header, ctr, 8 );
1359 header[ 8] = (unsigned char) type;
1360 header[ 9] = (unsigned char)( len >> 8 );
1361 header[10] = (unsigned char)( len );
1362
Paul Bakker68884e32013-01-07 18:20:04 +01001363 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_md_starts( md_ctx );
1365 mbedtls_md_update( md_ctx, secret, md_size );
1366 mbedtls_md_update( md_ctx, padding, padlen );
1367 mbedtls_md_update( md_ctx, header, 11 );
1368 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001369 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Paul Bakker68884e32013-01-07 18:20:04 +01001371 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_md_starts( md_ctx );
1373 mbedtls_md_update( md_ctx, secret, md_size );
1374 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001375 mbedtls_md_update( md_ctx, out, md_size );
1376 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1381 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001382 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001383#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001384#endif
1385
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001386/* The function below is only used in the Lucky 13 counter-measure in
1387 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1388#if defined(SSL_SOME_MODES_USE_MAC) && \
1389 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1390 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1391 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1392/* This function makes sure every byte in the memory region is accessed
1393 * (in ascending addresses order) */
1394static void ssl_read_memory( unsigned char *p, size_t len )
1395{
1396 unsigned char acc = 0;
1397 volatile unsigned char force;
1398
1399 for( ; len != 0; p++, len-- )
1400 acc ^= *p;
1401
1402 force = acc;
1403 (void) force;
1404}
1405#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1406
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001407/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001411{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001413 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001417 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001421 }
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001426 ssl->out_msg, ssl->out_msglen );
1427
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001429 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001431#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 if( mode == MBEDTLS_MODE_STREAM ||
1433 ( mode == MBEDTLS_MODE_CBC
1434#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1435 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436#endif
1437 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_PROTO_SSL3)
1440 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001441 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001442 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001443
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001444 ssl_mac( &ssl->transform_out->md_ctx_enc,
1445 ssl->transform_out->mac_enc,
1446 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001447 ssl->out_ctr, ssl->out_msgtype,
1448 mac );
1449
1450 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001451 }
1452 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1455 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1456 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001457 {
Hanno Becker992b6872017-11-09 18:57:39 +00001458 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1461 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1462 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1463 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001464 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001465 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001467
1468 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001469 }
1470 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001471#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001475 }
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001478 ssl->out_msg + ssl->out_msglen,
1479 ssl->transform_out->maclen );
1480
1481 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001482 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001483 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001484#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001485
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001486 /*
1487 * Encrypt
1488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1490 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001492 int ret;
1493 size_t olen = 0;
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 "including %d bytes of padding",
1497 ssl->out_msglen, 0 ) );
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001500 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001501 ssl->transform_out->ivlen,
1502 ssl->out_msg, ssl->out_msglen,
1503 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001506 return( ret );
1507 }
1508
1509 if( ssl->out_msglen != olen )
1510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1512 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
Paul Bakker68884e32013-01-07 18:20:04 +01001515 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001517#if defined(MBEDTLS_GCM_C) || \
1518 defined(MBEDTLS_CCM_C) || \
1519 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001521 mode == MBEDTLS_MODE_CCM ||
1522 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001523 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001524 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001525 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001526 unsigned char *enc_msg;
1527 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001528 unsigned char iv[12];
1529 mbedtls_ssl_transform *transform = ssl->transform_out;
1530 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001532 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001534 /*
1535 * Prepare additional authenticated data
1536 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001537 memcpy( add_data, ssl->out_ctr, 8 );
1538 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001540 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1542 add_data[12] = ssl->out_msglen & 0xFF;
1543
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001544 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001545
Paul Bakker68884e32013-01-07 18:20:04 +01001546 /*
1547 * Generate IV
1548 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001549 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1550 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001551 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001552 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1553 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1554 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1555
1556 }
1557 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1558 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001559 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001560 unsigned char i;
1561
1562 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1563
1564 for( i = 0; i < 8; i++ )
1565 iv[i+4] ^= ssl->out_ctr[i];
1566 }
1567 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001568 {
1569 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001572 }
1573
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001574 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1575 iv, transform->ivlen );
1576 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1577 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001578
Paul Bakker68884e32013-01-07 18:20:04 +01001579 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001581 */
1582 enc_msg = ssl->out_msg;
1583 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001584 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001587 "including 0 bytes of padding",
1588 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001589
Paul Bakker68884e32013-01-07 18:20:04 +01001590 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001592 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001593 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1594 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001595 add_data, 13,
1596 enc_msg, enc_msglen,
1597 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001598 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001601 return( ret );
1602 }
1603
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001604 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001608 }
1609
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001610 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001611 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1617#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001618 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001621 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001622 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001623 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001624
Paul Bakker48916f92012-09-16 19:57:18 +00001625 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1626 ssl->transform_out->ivlen;
1627 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 padlen = 0;
1629
1630 for( i = 0; i <= padlen; i++ )
1631 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1632
1633 ssl->out_msglen += padlen + 1;
1634
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001635 enc_msglen = ssl->out_msglen;
1636 enc_msg = ssl->out_msg;
1637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001640 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1641 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 {
1645 /*
1646 * Generate IV
1647 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001648 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001649 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001650 if( ret != 0 )
1651 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652
Paul Bakker92be97b2013-01-02 17:30:03 +01001653 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001654 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001655
1656 /*
1657 * Fix pointer positions and message length with added IV
1658 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001659 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001660 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001661 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001666 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001667 ssl->out_msglen, ssl->transform_out->ivlen,
1668 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001671 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001672 ssl->transform_out->ivlen,
1673 enc_msg, enc_msglen,
1674 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001677 return( ret );
1678 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001679
Paul Bakkercca5b812013-08-31 17:40:26 +02001680 if( enc_msglen != olen )
1681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001684 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1687 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001688 {
1689 /*
1690 * Save IV in SSL3 and TLS1
1691 */
1692 memcpy( ssl->transform_out->iv_enc,
1693 ssl->transform_out->cipher_ctx_enc.iv,
1694 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001696#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001699 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001700 {
1701 /*
1702 * MAC(MAC_write_key, seq_num +
1703 * TLSCipherText.type +
1704 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001705 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001706 * IV + // except for TLS 1.0
1707 * ENC(content + padding + padding_length));
1708 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001709 unsigned char pseudo_hdr[13];
1710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001712
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001713 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1714 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001715 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1716 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1721 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001722 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001724 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726
1727 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001728 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001732 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001734 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001740 /* Make extra sure authentication was performed, exactly once */
1741 if( auth_done != 1 )
1742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001745 }
1746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
1749 return( 0 );
1750}
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001755 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001756#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001757 size_t padlen = 0, correct = 1;
1758#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001766 }
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001769
Paul Bakker48916f92012-09-16 19:57:18 +00001770 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001773 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 }
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1778 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001779 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001780 int ret;
1781 size_t olen = 0;
1782
Paul Bakker68884e32013-01-07 18:20:04 +01001783 padlen = 0;
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001786 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001787 ssl->transform_in->ivlen,
1788 ssl->in_msg, ssl->in_msglen,
1789 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001792 return( ret );
1793 }
1794
1795 if( ssl->in_msglen != olen )
1796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001799 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 }
Paul Bakker68884e32013-01-07 18:20:04 +01001801 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803#if defined(MBEDTLS_GCM_C) || \
1804 defined(MBEDTLS_CCM_C) || \
1805 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001807 mode == MBEDTLS_MODE_CCM ||
1808 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001809 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001810 int ret;
1811 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001812 unsigned char *dec_msg;
1813 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001814 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001815 unsigned char iv[12];
1816 mbedtls_ssl_transform *transform = ssl->transform_in;
1817 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001819 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001820
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001821 /*
1822 * Compute and update sizes
1823 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001824 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001827 "+ taglen (%d)", ssl->in_msglen,
1828 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001830 }
1831 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1832
Paul Bakker68884e32013-01-07 18:20:04 +01001833 dec_msg = ssl->in_msg;
1834 dec_msg_result = ssl->in_msg;
1835 ssl->in_msglen = dec_msglen;
1836
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001837 /*
1838 * Prepare additional authenticated data
1839 */
Paul Bakker68884e32013-01-07 18:20:04 +01001840 memcpy( add_data, ssl->in_ctr, 8 );
1841 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001843 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001844 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1845 add_data[12] = ssl->in_msglen & 0xFF;
1846
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001847 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001848
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001849 /*
1850 * Prepare IV
1851 */
1852 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1853 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001854 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1856 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001857
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001858 }
1859 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1860 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001861 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862 unsigned char i;
1863
1864 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1865
1866 for( i = 0; i < 8; i++ )
1867 iv[i+4] ^= ssl->in_ctr[i];
1868 }
1869 else
1870 {
1871 /* Reminder if we ever add an AEAD mode with a different size */
1872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1874 }
1875
1876 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001878
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001879 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001880 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001883 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001884 add_data, 13,
1885 dec_msg, dec_msglen,
1886 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001887 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1892 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001893
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001894 return( ret );
1895 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001896 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001897
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001898 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1901 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001902 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1906#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001907 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 {
Paul Bakker45829992013-01-03 14:52:21 +01001910 /*
1911 * Decrypt and check the padding
1912 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001913 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001914 unsigned char *dec_msg;
1915 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001916 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001917 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001918 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 /*
Paul Bakker45829992013-01-03 14:52:21 +01001921 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1924 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001925 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001926#endif
Paul Bakker45829992013-01-03 14:52:21 +01001927
1928 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1929 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001932 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1933 ssl->transform_in->ivlen,
1934 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001936 }
1937
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001938 dec_msglen = ssl->in_msglen;
1939 dec_msg = ssl->in_msg;
1940 dec_msg_result = ssl->in_msg;
1941
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001942 /*
1943 * Authenticate before decrypt if enabled
1944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1946 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001947 {
Hanno Becker992b6872017-11-09 18:57:39 +00001948 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001949 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001952
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001953 dec_msglen -= ssl->transform_in->maclen;
1954 ssl->in_msglen -= ssl->transform_in->maclen;
1955
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001956 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1957 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1958 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1959 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1964 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001965 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001966 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001971 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 ssl->transform_in->maclen );
1973
Hanno Becker992b6872017-11-09 18:57:39 +00001974 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1975 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001981 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001982 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984
1985 /*
1986 * Check length sanity
1987 */
1988 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001991 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001993 }
1994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001997 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002001 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002002 dec_msglen -= ssl->transform_in->ivlen;
2003 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004
Paul Bakker48916f92012-09-16 19:57:18 +00002005 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002006 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002007 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002011 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002012 ssl->transform_in->ivlen,
2013 dec_msg, dec_msglen,
2014 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002017 return( ret );
2018 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002019
Paul Bakkercca5b812013-08-31 17:40:26 +02002020 if( dec_msglen != olen )
2021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2027 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002028 {
2029 /*
2030 * Save IV in SSL3 and TLS1
2031 */
2032 memcpy( ssl->transform_in->iv_dec,
2033 ssl->transform_in->cipher_ctx_dec.iv,
2034 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
2038 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002039
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002040 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002041 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_SSL_DEBUG_ALL)
2044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002045 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002046#endif
Paul Bakker45829992013-01-03 14:52:21 +01002047 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002048 correct = 0;
2049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#if defined(MBEDTLS_SSL_PROTO_SSL3)
2052 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 {
Paul Bakker48916f92012-09-16 19:57:18 +00002054 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#if defined(MBEDTLS_SSL_DEBUG_ALL)
2057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002059 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002060#endif
Paul Bakker45829992013-01-03 14:52:21 +01002061 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
2063 }
2064 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2066#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2067 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2068 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 {
2070 /*
Paul Bakker45829992013-01-03 14:52:21 +01002071 * TLSv1+: always check the padding up to the first failure
2072 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002074 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002075 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002076 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002077
Paul Bakker956c9e02013-12-19 14:42:28 +01002078 /*
2079 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002080 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002081 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002082 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002083 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002084 *
2085 * In both cases we reset padding_idx to a safe value (0) to
2086 * prevent out-of-buffer reads.
2087 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002088 correct &= ( padlen <= ssl->in_msglen );
2089 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002090 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002091
2092 padding_idx *= correct;
2093
Angus Grattonb512bc12018-06-19 15:57:50 +10002094 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002095 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002096 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002097 pad_count += real_count *
2098 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2099 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002100
2101 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002104 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002106#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002107 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2111 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002115 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002116
2117 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002119 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002121 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002127#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
2132 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002133 * Authenticate if not done yet.
2134 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002136#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002137 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002138 {
Hanno Becker992b6872017-11-09 18:57:39 +00002139 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002140
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002141 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002143 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2144 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#if defined(MBEDTLS_SSL_PROTO_SSL3)
2147 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002148 {
2149 ssl_mac( &ssl->transform_in->md_ctx_dec,
2150 ssl->transform_in->mac_dec,
2151 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002152 ssl->in_ctr, ssl->in_msgtype,
2153 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002154 }
2155 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2157#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2158 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2159 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 {
2161 /*
2162 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002163 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002164 *
2165 * Known timing attacks:
2166 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2167 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002168 * To compensate for different timings for the MAC calculation
2169 * depending on how much padding was removed (which is determined
2170 * by padlen), process extra_run more blocks through the hash
2171 * function.
2172 *
2173 * The formula in the paper is
2174 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2175 * where L1 is the size of the header plus the decrypted message
2176 * plus CBC padding and L2 is the size of the header plus the
2177 * decrypted message. This is for an underlying hash function
2178 * with 64-byte blocks.
2179 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2180 * correctly. We round down instead of up, so -56 is the correct
2181 * value for our calculations instead of -55.
2182 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002183 * Repeat the formula rather than defining a block_size variable.
2184 * This avoids requiring division by a variable at runtime
2185 * (which would be marginally less efficient and would require
2186 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002187 */
2188 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002189
2190 /*
2191 * The next two sizes are the minimum and maximum values of
2192 * in_msglen over all padlen values.
2193 *
2194 * They're independent of padlen, since we previously did
2195 * in_msglen -= padlen.
2196 *
2197 * Note that max_len + maclen is never more than the buffer
2198 * length, as we previously did in_msglen -= maclen too.
2199 */
2200 const size_t max_len = ssl->in_msglen + padlen;
2201 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2202
Gilles Peskine20b44082018-05-29 14:06:49 +02002203 switch( ssl->transform_in->ciphersuite_info->mac )
2204 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002205#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2206 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002207 case MBEDTLS_MD_MD5:
2208 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002209 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002210 /* 8 bytes of message size, 64-byte compression blocks */
2211 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2212 ( 13 + ssl->in_msglen + 8 ) / 64;
2213 break;
2214#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002215#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002216 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002217 /* 16 bytes of message size, 128-byte compression blocks */
2218 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2219 ( 13 + ssl->in_msglen + 16 ) / 128;
2220 break;
2221#endif
2222 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2225 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002226
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002227 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2230 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2231 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2232 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002234 /* Make sure we access everything even when padlen > 0. This
2235 * makes the synchronisation requirements for just-in-time
2236 * Prime+Probe attacks much tighter and hopefully impractical. */
2237 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002238 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002239
2240 /* Call mbedtls_md_process at least once due to cache attacks
2241 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002242 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002246
2247 /* Make sure we access all the memory that could contain the MAC,
2248 * before we check it in the next code block. This makes the
2249 * synchronisation requirements for just-in-time Prime+Probe
2250 * attacks much tighter and hopefully impractical. */
2251 ssl_read_memory( ssl->in_msg + min_len,
2252 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 }
2254 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2256 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002262#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002263 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2264 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2265 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Hanno Becker992b6872017-11-09 18:57:39 +00002268 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2269 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#if defined(MBEDTLS_SSL_DEBUG_ALL)
2272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002273#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002274 correct = 0;
2275 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002276 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002278 /*
2279 * Finally check the correct flag
2280 */
2281 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002283 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002284#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002285
2286 /* Make extra sure authentication was performed, exactly once */
2287 if( auth_done != 1 )
2288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
2293 if( ssl->in_msglen == 0 )
2294 {
Angus Gratton34817922018-06-19 15:58:22 +10002295#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2296 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2297 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2298 {
2299 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2301 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2302 }
2303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2304
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 ssl->nb_zero++;
2306
2307 /*
2308 * Three or more empty messages may be a DoS attack
2309 * (excessive CPU consumption).
2310 */
2311 if( ssl->nb_zero > 3 )
2312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316 }
2317 }
2318 else
2319 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002323 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002324 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002325 }
2326 else
2327#endif
2328 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002329 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002330 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2331 if( ++ssl->in_ctr[i - 1] != 0 )
2332 break;
2333
2334 /* The loop goes to its end iff the counter is wrapping */
2335 if( i == ssl_ep_len( ssl ) )
2336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2338 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002339 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002340 }
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
2344 return( 0 );
2345}
2346
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002347#undef MAC_NONE
2348#undef MAC_PLAINTEXT
2349#undef MAC_CIPHERTEXT
2350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352/*
2353 * Compression/decompression functions
2354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356{
2357 int ret;
2358 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002359 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002361 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002365 if( len_pre == 0 )
2366 return( 0 );
2367
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368 memcpy( msg_pre, ssl->out_msg, len_pre );
2369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002371 ssl->out_msglen ) );
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374 ssl->out_msg, ssl->out_msglen );
2375
Paul Bakker48916f92012-09-16 19:57:18 +00002376 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2377 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2378 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002379 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380
Paul Bakker48916f92012-09-16 19:57:18 +00002381 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 if( ret != Z_OK )
2383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2385 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386 }
2387
Angus Grattond8213d02016-05-25 20:56:48 +10002388 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002389 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392 ssl->out_msglen ) );
2393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002395 ssl->out_msg, ssl->out_msglen );
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002398
2399 return( 0 );
2400}
2401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403{
2404 int ret;
2405 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002406 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002407 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002408 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002411
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002412 if( len_pre == 0 )
2413 return( 0 );
2414
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415 memcpy( msg_pre, ssl->in_msg, len_pre );
2416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 ssl->in_msglen ) );
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421 ssl->in_msg, ssl->in_msglen );
2422
Paul Bakker48916f92012-09-16 19:57:18 +00002423 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2424 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2425 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002426 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002427 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002428
Paul Bakker48916f92012-09-16 19:57:18 +00002429 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002430 if( ret != Z_OK )
2431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2433 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434 }
2435
Angus Grattond8213d02016-05-25 20:56:48 +10002436 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002437 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440 ssl->in_msglen ) );
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002443 ssl->in_msg, ssl->in_msglen );
2444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446
2447 return( 0 );
2448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2452static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SSL_PROTO_DTLS)
2455static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002456{
2457 /* If renegotiation is not enforced, retransmit until we would reach max
2458 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002459 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002460 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002461 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002462 unsigned char doublings = 1;
2463
2464 while( ratio != 0 )
2465 {
2466 ++doublings;
2467 ratio >>= 1;
2468 }
2469
2470 if( ++ssl->renego_records_seen > doublings )
2471 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002473 return( 0 );
2474 }
2475 }
2476
2477 return( ssl_write_hello_request( ssl ) );
2478}
2479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002481
Paul Bakker5121ce52009-01-03 21:22:43 +00002482/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002483 * Fill the input message buffer by appending data to it.
2484 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002485 *
2486 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2487 * available (from this read and/or a previous one). Otherwise, an error code
2488 * is returned (possibly EOF or WANT_READ).
2489 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002490 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2491 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2492 * since we always read a whole datagram at once.
2493 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002494 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002495 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498{
Paul Bakker23986e52011-04-24 08:57:21 +00002499 int ret;
2500 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002504 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002507 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002509 }
2510
Angus Grattond8213d02016-05-25 20:56:48 +10002511 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002515 }
2516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002520 uint32_t timeout;
2521
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002522 /* Just to be sure */
2523 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2524 {
2525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2526 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2528 }
2529
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002530 /*
2531 * The point is, we need to always read a full datagram at once, so we
2532 * sometimes read more then requested, and handle the additional data.
2533 * It could be the rest of the current record (while fetching the
2534 * header) and/or some other records in the same datagram.
2535 */
2536
2537 /*
2538 * Move to the next record in the already read datagram if applicable
2539 */
2540 if( ssl->next_record_offset != 0 )
2541 {
2542 if( ssl->in_left < ssl->next_record_offset )
2543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002546 }
2547
2548 ssl->in_left -= ssl->next_record_offset;
2549
2550 if( ssl->in_left != 0 )
2551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002553 ssl->next_record_offset ) );
2554 memmove( ssl->in_hdr,
2555 ssl->in_hdr + ssl->next_record_offset,
2556 ssl->in_left );
2557 }
2558
2559 ssl->next_record_offset = 0;
2560 }
2561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002564
2565 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002566 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002567 */
2568 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002571 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002572 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002573
2574 /*
2575 * A record can't be split accross datagrams. If we need to read but
2576 * are not at the beginning of a new record, the caller did something
2577 * wrong.
2578 */
2579 if( ssl->in_left != 0 )
2580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002583 }
2584
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002585 /*
2586 * Don't even try to read if time's out already.
2587 * This avoids by-passing the timer when repeatedly receiving messages
2588 * that will end up being dropped.
2589 */
2590 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002593 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002594 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002595 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002596 {
Angus Grattond8213d02016-05-25 20:56:48 +10002597 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002600 timeout = ssl->handshake->retransmit_timeout;
2601 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002602 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002605
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002606 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002607 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2608 timeout );
2609 else
2610 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002613
2614 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002616 }
2617
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002618 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002621 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002624 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002625 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002628 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002629 }
2630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002634 return( ret );
2635 }
2636
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002637 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002640 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002642 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002643 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002646 return( ret );
2647 }
2648
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002649 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002652 }
2653
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 if( ret < 0 )
2655 return( ret );
2656
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002657 ssl->in_left = ret;
2658 }
2659 else
2660#endif
2661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002663 ssl->in_left, nb_want ) );
2664
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002665 while( ssl->in_left < nb_want )
2666 {
2667 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002668
2669 if( ssl_check_timer( ssl ) != 0 )
2670 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2671 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002672 {
2673 if( ssl->f_recv_timeout != NULL )
2674 {
2675 ret = ssl->f_recv_timeout( ssl->p_bio,
2676 ssl->in_hdr + ssl->in_left, len,
2677 ssl->conf->read_timeout );
2678 }
2679 else
2680 {
2681 ret = ssl->f_recv( ssl->p_bio,
2682 ssl->in_hdr + ssl->in_left, len );
2683 }
2684 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002687 ssl->in_left, nb_want ) );
2688 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002689
2690 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002692
2693 if( ret < 0 )
2694 return( ret );
2695
mohammad160352aecb92018-03-28 23:41:40 -07002696 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002697 {
Darryl Green11999bb2018-03-13 15:22:58 +00002698 MBEDTLS_SSL_DEBUG_MSG( 1,
2699 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002700 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2702 }
2703
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002704 ssl->in_left += ret;
2705 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 }
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
2710 return( 0 );
2711}
2712
2713/*
2714 * Flush any data not yet written
2715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002717{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002718 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002719 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002723 if( ssl->f_send == NULL )
2724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002726 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002728 }
2729
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002730 /* Avoid incrementing counter if data is flushed */
2731 if( ssl->out_left == 0 )
2732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002734 return( 0 );
2735 }
2736
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 while( ssl->out_left > 0 )
2738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2740 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Hanno Becker2b1e3542018-08-06 11:19:13 +01002742 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002743 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
2747 if( ret <= 0 )
2748 return( ret );
2749
mohammad160352aecb92018-03-28 23:41:40 -07002750 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002751 {
Darryl Green11999bb2018-03-13 15:22:58 +00002752 MBEDTLS_SSL_DEBUG_MSG( 1,
2753 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002754 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2756 }
2757
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 ssl->out_left -= ret;
2759 }
2760
Hanno Becker2b1e3542018-08-06 11:19:13 +01002761#if defined(MBEDTLS_SSL_PROTO_DTLS)
2762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2763 {
2764 ssl->out_hdr = ssl->out_buf;
2765 }
2766 else
2767#endif
2768 {
2769 ssl->out_hdr = ssl->out_buf + 8;
2770 }
2771 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
2775 return( 0 );
2776}
2777
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002778/*
2779 * Functions to handle the DTLS retransmission state machine
2780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002782/*
2783 * Append current handshake message to current outgoing flight
2784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002786{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2789 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2790 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002791
2792 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002793 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002797 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002798 }
2799
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002800 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002801 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002804 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002805 }
2806
2807 /* Copy current handshake message with headers */
2808 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2809 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002810 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002811 msg->next = NULL;
2812
2813 /* Append to the current flight */
2814 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002815 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002816 else
2817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002819 while( cur->next != NULL )
2820 cur = cur->next;
2821 cur->next = msg;
2822 }
2823
Hanno Becker3b235902018-08-06 09:54:53 +01002824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825 return( 0 );
2826}
2827
2828/*
2829 * Free the current flight of handshake messages
2830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_ssl_flight_item *cur = flight;
2834 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002835
2836 while( cur != NULL )
2837 {
2838 next = cur->next;
2839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 mbedtls_free( cur->p );
2841 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002842
2843 cur = next;
2844 }
2845}
2846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2848static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002849#endif
2850
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002851/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002852 * Swap transform_out and out_ctr with the alternative ones
2853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002857 unsigned char tmp_out_ctr[8];
2858
2859 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002862 return;
2863 }
2864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002866
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002867 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 tmp_transform = ssl->transform_out;
2869 ssl->transform_out = ssl->handshake->alt_transform_out;
2870 ssl->handshake->alt_transform_out = tmp_transform;
2871
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002873 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2874 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002875 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002876
2877 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002878 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2881 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2886 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002887 }
2888 }
2889#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002890}
2891
2892/*
2893 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002894 */
2895int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2896{
2897 int ret = 0;
2898
2899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2900
2901 ret = mbedtls_ssl_flight_transmit( ssl );
2902
2903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2904
2905 return( ret );
2906}
2907
2908/*
2909 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002910 *
2911 * Need to remember the current message in case flush_output returns
2912 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002913 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002914 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002915int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002916{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002917 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002921 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002923
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002924 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002925 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002926 ssl_swap_epochs( ssl );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002929 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002930
2931 while( ssl->handshake->cur_msg != NULL )
2932 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002933 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002934 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002935
Hanno Beckere1dcb032018-08-17 16:47:58 +01002936 int const is_finished =
2937 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2938 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2939
Hanno Becker04da1892018-08-14 13:22:10 +01002940 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2941 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2942
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002943 /* Swap epochs before sending Finished: we can't do it after
2944 * sending ChangeCipherSpec, in case write returns WANT_READ.
2945 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002946 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002947 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002949 ssl_swap_epochs( ssl );
2950 }
2951
Hanno Becker67bc7c32018-08-06 11:33:50 +01002952 ret = ssl_get_remaining_payload_in_datagram( ssl );
2953 if( ret < 0 )
2954 return( ret );
2955 max_frag_len = (size_t) ret;
2956
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002957 /* CCS is copied as is, while HS messages may need fragmentation */
2958 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2959 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002960 if( max_frag_len == 0 )
2961 {
2962 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2963 return( ret );
2964
2965 continue;
2966 }
2967
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002968 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002969 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002970 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002971
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002972 /* Update position inside current message */
2973 ssl->handshake->cur_msg_p += cur->len;
2974 }
2975 else
2976 {
2977 const unsigned char * const p = ssl->handshake->cur_msg_p;
2978 const size_t hs_len = cur->len - 12;
2979 const size_t frag_off = p - ( cur->p + 12 );
2980 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002981 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002982
Hanno Beckere1dcb032018-08-17 16:47:58 +01002983 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002984 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002985 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002986 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002987
2988 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2989 return( ret );
2990
2991 continue;
2992 }
2993 max_hs_frag_len = max_frag_len - 12;
2994
2995 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2996 max_hs_frag_len : rem_len;
2997
2998 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002999 {
3000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003001 (unsigned) cur_hs_frag_len,
3002 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003003 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003004
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003005 /* Messages are stored with handshake headers as if not fragmented,
3006 * copy beginning of headers then fill fragmentation fields.
3007 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3008 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003009
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003010 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3011 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3012 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3013
Hanno Becker67bc7c32018-08-06 11:33:50 +01003014 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3015 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3016 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003017
3018 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3019
Hanno Becker67bc7c32018-08-06 11:33:50 +01003020 /* Copy the handshame message content and set records fields */
3021 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3022 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003023 ssl->out_msgtype = cur->type;
3024
3025 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003026 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003027 }
3028
3029 /* If done with the current message move to the next one if any */
3030 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3031 {
3032 if( cur->next != NULL )
3033 {
3034 ssl->handshake->cur_msg = cur->next;
3035 ssl->handshake->cur_msg_p = cur->next->p + 12;
3036 }
3037 else
3038 {
3039 ssl->handshake->cur_msg = NULL;
3040 ssl->handshake->cur_msg_p = NULL;
3041 }
3042 }
3043
3044 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003045 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003048 return( ret );
3049 }
3050 }
3051
Hanno Becker67bc7c32018-08-06 11:33:50 +01003052 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3053 return( ret );
3054
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003055 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3057 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003058 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003061 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3062 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003063
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003065
3066 return( 0 );
3067}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003068
3069/*
3070 * To be called when the last message of an incoming flight is received.
3071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003073{
3074 /* We won't need to resend that one any more */
3075 ssl_flight_free( ssl->handshake->flight );
3076 ssl->handshake->flight = NULL;
3077 ssl->handshake->cur_msg = NULL;
3078
3079 /* The next incoming flight will start with this msg_seq */
3080 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3081
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003082 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003083 ssl_set_timer( ssl, 0 );
3084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3086 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003089 }
3090 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003092}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003093
3094/*
3095 * To be called when the last message of an outgoing flight is send.
3096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003098{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003099 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003100 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3103 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003106 }
3107 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003111
Paul Bakker5121ce52009-01-03 21:22:43 +00003112/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003113 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003115
3116/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003117 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003118 *
3119 * - fill in handshake headers
3120 * - update handshake checksum
3121 * - DTLS: save message for resending
3122 * - then pass to the record layer
3123 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003124 * DTLS: except for HelloRequest, messages are only queued, and will only be
3125 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003126 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003127 * Inputs:
3128 * - ssl->out_msglen: 4 + actual handshake message len
3129 * (4 is the size of handshake headers for TLS)
3130 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3131 * - ssl->out_msg + 4: the handshake message body
3132 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003133 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003134 * - ssl->out_msglen: the length of the record contents
3135 * (including handshake headers but excluding record headers)
3136 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003137 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003138int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003139{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003140 int ret;
3141 const size_t hs_len = ssl->out_msglen - 4;
3142 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3145
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003146 /*
3147 * Sanity checks
3148 */
Hanno Becker2c98db22018-08-22 16:05:47 +01003149 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003150 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3151 {
Hanno Becker2c98db22018-08-22 16:05:47 +01003152 /* In SSLv3, the client might send a NoCertificate alert. */
3153#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3154 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3155 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3156 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3157#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3158 {
3159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3161 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003164 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3165 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3166 ssl->handshake == NULL )
3167 {
3168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3170 }
3171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003174 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003176 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003179 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003181
Hanno Beckerb50a2532018-08-06 11:52:54 +01003182 /* Double-check that we did not exceed the bounds
3183 * of the outgoing record buffer.
3184 * This should never fail as the various message
3185 * writing functions must obey the bounds of the
3186 * outgoing record buffer, but better be safe.
3187 *
3188 * Note: We deliberately do not check for the MTU or MFL here.
3189 */
3190 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3191 {
3192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3193 "size %u, maximum %u",
3194 (unsigned) ssl->out_msglen,
3195 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3197 }
3198
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003199 /*
3200 * Fill handshake headers
3201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003204 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3205 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3206 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003208 /*
3209 * DTLS has additional fields in the Handshake layer,
3210 * between the length field and the actual payload:
3211 * uint16 message_seq;
3212 * uint24 fragment_offset;
3213 * uint24 fragment_length;
3214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003217 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003218 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003219 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003220 {
3221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3222 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003223 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003224 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3226 }
3227
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003228 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003229 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003230
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003231 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003232 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003233 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003234 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3235 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3236 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003237 }
3238 else
3239 {
3240 ssl->out_msg[4] = 0;
3241 ssl->out_msg[5] = 0;
3242 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003243
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003244 /* Handshake hashes are computed without fragmentation,
3245 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003246 memset( ssl->out_msg + 6, 0x00, 3 );
3247 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003248 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003250
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003251 /* Update running hashes of hanshake messages seen */
3252 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3253 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254 }
3255
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003256 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker551835d2018-08-22 16:07:59 +01003259 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3260 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003261 {
3262 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003265 return( ret );
3266 }
3267 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003268 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003269#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003270 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003271 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003272 {
3273 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3274 return( ret );
3275 }
3276 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003277
3278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3279
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003280 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003281}
3282
3283/*
3284 * Record layer functions
3285 */
3286
3287/*
3288 * Write current record.
3289 *
3290 * Uses:
3291 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3292 * - ssl->out_msglen: length of the record content (excl headers)
3293 * - ssl->out_msg: record content
3294 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003295int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003296{
3297 int ret, done = 0;
3298 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003299 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003300
3301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003304 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003306 {
3307 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003310 return( ret );
3311 }
3312
3313 len = ssl->out_msglen;
3314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3318 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 ret = mbedtls_ssl_hw_record_write( ssl );
3323 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3326 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003327 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003328
3329 if( ret == 0 )
3330 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003333 if( !done )
3334 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003335 unsigned i;
3336 size_t protected_record_size;
3337
Paul Bakker05ef8352012-05-08 09:17:57 +00003338 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003340 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003341
Hanno Becker19859472018-08-06 09:40:20 +01003342 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003343 ssl->out_len[0] = (unsigned char)( len >> 8 );
3344 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003345
Paul Bakker48916f92012-09-16 19:57:18 +00003346 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003347 {
3348 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003351 return( ret );
3352 }
3353
3354 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003355 ssl->out_len[0] = (unsigned char)( len >> 8 );
3356 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003357 }
3358
Hanno Becker2b1e3542018-08-06 11:19:13 +01003359 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3360
3361#if defined(MBEDTLS_SSL_PROTO_DTLS)
3362 /* In case of DTLS, double-check that we don't exceed
3363 * the remaining space in the datagram. */
3364 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3365 {
3366 ret = ssl_get_maximum_datagram_size( ssl );
3367 if( ret < 0 )
3368 return( ret );
3369
3370 if( protected_record_size > (size_t) ret )
3371 {
3372 /* Should never happen */
3373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3374 }
3375 }
3376#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003379 "version = [%d:%d], msglen = %d",
3380 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3381
Paul Bakker05ef8352012-05-08 09:17:57 +00003382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003384 ssl->out_hdr, protected_record_size );
3385
3386 ssl->out_left += protected_record_size;
3387 ssl->out_hdr += protected_record_size;
3388 ssl_update_out_pointers( ssl, ssl->transform_out );
3389
Hanno Becker04484622018-08-06 09:49:38 +01003390 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3391 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3392 break;
3393
3394 /* The loop goes to its end iff the counter is wrapping */
3395 if( i == ssl_ep_len( ssl ) )
3396 {
3397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3398 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 }
3401
Hanno Becker67bc7c32018-08-06 11:33:50 +01003402#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3404 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003405 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003406 size_t remaining;
3407 ret = ssl_get_remaining_payload_in_datagram( ssl );
3408 if( ret < 0 )
3409 {
3410 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3411 ret );
3412 return( ret );
3413 }
3414
3415 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003416 if( remaining == 0 )
3417 flush = SSL_FORCE_FLUSH;
3418 else
3419 {
Hanno Becker513815a2018-08-20 11:56:09 +01003420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003421 }
3422 }
3423#endif /* MBEDTLS_SSL_PROTO_DTLS */
3424
3425 if( ( flush == SSL_FORCE_FLUSH ) &&
3426 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 return( ret );
3430 }
3431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
3434 return( 0 );
3435}
3436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003438/*
3439 * Mark bits in bitmask (used for DTLS HS reassembly)
3440 */
3441static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3442{
3443 unsigned int start_bits, end_bits;
3444
3445 start_bits = 8 - ( offset % 8 );
3446 if( start_bits != 8 )
3447 {
3448 size_t first_byte_idx = offset / 8;
3449
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003450 /* Special case */
3451 if( len <= start_bits )
3452 {
3453 for( ; len != 0; len-- )
3454 mask[first_byte_idx] |= 1 << ( start_bits - len );
3455
3456 /* Avoid potential issues with offset or len becoming invalid */
3457 return;
3458 }
3459
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003460 offset += start_bits; /* Now offset % 8 == 0 */
3461 len -= start_bits;
3462
3463 for( ; start_bits != 0; start_bits-- )
3464 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3465 }
3466
3467 end_bits = len % 8;
3468 if( end_bits != 0 )
3469 {
3470 size_t last_byte_idx = ( offset + len ) / 8;
3471
3472 len -= end_bits; /* Now len % 8 == 0 */
3473
3474 for( ; end_bits != 0; end_bits-- )
3475 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3476 }
3477
3478 memset( mask + offset / 8, 0xFF, len / 8 );
3479}
3480
3481/*
3482 * Check that bitmask is full
3483 */
3484static int ssl_bitmask_check( unsigned char *mask, size_t len )
3485{
3486 size_t i;
3487
3488 for( i = 0; i < len / 8; i++ )
3489 if( mask[i] != 0xFF )
3490 return( -1 );
3491
3492 for( i = 0; i < len % 8; i++ )
3493 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3494 return( -1 );
3495
3496 return( 0 );
3497}
3498
3499/*
3500 * Reassemble fragmented DTLS handshake messages.
3501 *
3502 * Use a temporary buffer for reassembly, divided in two parts:
3503 * - the first holds the reassembled message (including handshake header),
3504 * - the second holds a bitmask indicating which parts of the message
3505 * (excluding headers) have been received so far.
3506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003508{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003509 unsigned char *msg, *bitmask;
3510 size_t frag_len, frag_off;
3511 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3512
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003513 if( ssl->handshake == NULL )
3514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3516 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003517 }
3518
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003519 /*
3520 * For first fragment, check size and allocate buffer
3521 */
3522 if( ssl->handshake->hs_msg == NULL )
3523 {
3524 size_t alloc_len;
3525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003527 msg_len ) );
3528
Angus Grattond8213d02016-05-25 20:56:48 +10003529 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3532 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003533 }
3534
3535 /* The bitmask needs one bit per byte of message excluding header */
3536 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3537
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003538 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003539 if( ssl->handshake->hs_msg == NULL )
3540 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003542 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003543 }
3544
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003545 /* Prepare final header: copy msg_type, length and message_seq,
3546 * then add standardised fragment_offset and fragment_length */
3547 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3548 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3549 memcpy( ssl->handshake->hs_msg + 9,
3550 ssl->handshake->hs_msg + 1, 3 );
3551 }
3552 else
3553 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003554 /* Make sure msg_type and length are consistent */
3555 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3558 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003559 }
3560 }
3561
3562 msg = ssl->handshake->hs_msg + 12;
3563 bitmask = msg + msg_len;
3564
3565 /*
3566 * Check and copy current fragment
3567 */
3568 frag_off = ( ssl->in_msg[6] << 16 ) |
3569 ( ssl->in_msg[7] << 8 ) |
3570 ssl->in_msg[8];
3571 frag_len = ( ssl->in_msg[9] << 16 ) |
3572 ( ssl->in_msg[10] << 8 ) |
3573 ssl->in_msg[11];
3574
3575 if( frag_off + frag_len > msg_len )
3576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003578 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003580 }
3581
3582 if( frag_len + 12 > ssl->in_msglen )
3583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003585 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003587 }
3588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003590 frag_off, frag_len ) );
3591
3592 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3593 ssl_bitmask_set( bitmask, frag_off, frag_len );
3594
3595 /*
3596 * Do we have the complete message by now?
3597 * If yes, finalize it, else ask to read the next record.
3598 */
3599 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003602 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003603 }
3604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003606
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003607 if( frag_len + 12 < ssl->in_msglen )
3608 {
3609 /*
3610 * We'got more handshake messages in the same record.
3611 * This case is not handled now because no know implementation does
3612 * that and it's hard to test, so we prefer to fail cleanly for now.
3613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3615 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003616 }
3617
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003618 if( ssl->in_left > ssl->next_record_offset )
3619 {
3620 /*
3621 * We've got more data in the buffer after the current record,
3622 * that we don't want to overwrite. Move it before writing the
3623 * reassembled message, and adjust in_left and next_record_offset.
3624 */
3625 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3626 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3627 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3628
3629 /* First compute and check new lengths */
3630 ssl->next_record_offset = new_remain - ssl->in_hdr;
3631 ssl->in_left = ssl->next_record_offset + remain_len;
3632
Angus Grattond8213d02016-05-25 20:56:48 +10003633 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003634 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3637 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003638 }
3639
3640 memmove( new_remain, cur_remain, remain_len );
3641 }
3642
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003643 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003646 ssl->handshake->hs_msg = NULL;
3647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003649 ssl->in_msg, ssl->in_hslen );
3650
3651 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003654
Simon Butcher99000142016-10-13 17:21:01 +01003655int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003660 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003662 }
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003665 ( ssl->in_msg[1] << 16 ) |
3666 ( ssl->in_msg[2] << 8 ) |
3667 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003670 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003671 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003674 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003675 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003676 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003677 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003678
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003679 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003680 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3681 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3682 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3683 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003684 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003685 /* Retransmit only on last message from previous flight, to avoid
3686 * too many retransmissions.
3687 * Besides, No sane server ever retransmits HelloVerifyRequest */
3688 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003692 "message_seq = %d, start_of_flight = %d",
3693 recv_msg_seq,
3694 ssl->handshake->in_flight_start_seq ) );
3695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003699 return( ret );
3700 }
3701 }
3702 else
3703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003705 "message_seq = %d, expected = %d",
3706 recv_msg_seq,
3707 ssl->handshake->in_msg_seq ) );
3708 }
3709
Hanno Becker90333da2017-10-10 11:27:13 +01003710 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003711 }
3712 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003713
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003714 /* Reassemble if current message is fragmented or reassembly is
3715 * already in progress */
3716 if( ssl->in_msglen < ssl->in_hslen ||
3717 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3718 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3719 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003722
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003723 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003726 return( ret );
3727 }
3728 }
3729 }
3730 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003732 /* With TLS we don't handle fragmentation (for now) */
3733 if( ssl->in_msglen < ssl->in_hslen )
3734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3736 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003737 }
3738
Simon Butcher99000142016-10-13 17:21:01 +01003739 return( 0 );
3740}
3741
3742void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3743{
3744
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003745 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3746 ssl->handshake != NULL )
3747 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003748 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003749 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003750
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003751 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003753 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003754 ssl->handshake != NULL )
3755 {
3756 ssl->handshake->in_msg_seq++;
3757 }
3758#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003759}
3760
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003761/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003762 * DTLS anti-replay: RFC 6347 4.1.2.6
3763 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003764 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3765 * Bit n is set iff record number in_window_top - n has been seen.
3766 *
3767 * Usually, in_window_top is the last record number seen and the lsb of
3768 * in_window is set. The only exception is the initial state (record number 0
3769 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3772static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003773{
3774 ssl->in_window_top = 0;
3775 ssl->in_window = 0;
3776}
3777
3778static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3779{
3780 return( ( (uint64_t) buf[0] << 40 ) |
3781 ( (uint64_t) buf[1] << 32 ) |
3782 ( (uint64_t) buf[2] << 24 ) |
3783 ( (uint64_t) buf[3] << 16 ) |
3784 ( (uint64_t) buf[4] << 8 ) |
3785 ( (uint64_t) buf[5] ) );
3786}
3787
3788/*
3789 * Return 0 if sequence number is acceptable, -1 otherwise
3790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003792{
3793 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3794 uint64_t bit;
3795
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003796 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003797 return( 0 );
3798
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003799 if( rec_seqnum > ssl->in_window_top )
3800 return( 0 );
3801
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003802 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003803
3804 if( bit >= 64 )
3805 return( -1 );
3806
3807 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3808 return( -1 );
3809
3810 return( 0 );
3811}
3812
3813/*
3814 * Update replay window on new validated record
3815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003817{
3818 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3819
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003820 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003821 return;
3822
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003823 if( rec_seqnum > ssl->in_window_top )
3824 {
3825 /* Update window_top and the contents of the window */
3826 uint64_t shift = rec_seqnum - ssl->in_window_top;
3827
3828 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003829 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003830 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003831 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003832 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003833 ssl->in_window |= 1;
3834 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003835
3836 ssl->in_window_top = rec_seqnum;
3837 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003838 else
3839 {
3840 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003841 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003842
3843 if( bit < 64 ) /* Always true, but be extra sure */
3844 ssl->in_window |= (uint64_t) 1 << bit;
3845 }
3846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003848
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003849#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003850/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003851static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3852
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003853/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003854 * Without any SSL context, check if a datagram looks like a ClientHello with
3855 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003856 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003857 *
3858 * - if cookie is valid, return 0
3859 * - if ClientHello looks superficially valid but cookie is not,
3860 * fill obuf and set olen, then
3861 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3862 * - otherwise return a specific error code
3863 */
3864static int ssl_check_dtls_clihlo_cookie(
3865 mbedtls_ssl_cookie_write_t *f_cookie_write,
3866 mbedtls_ssl_cookie_check_t *f_cookie_check,
3867 void *p_cookie,
3868 const unsigned char *cli_id, size_t cli_id_len,
3869 const unsigned char *in, size_t in_len,
3870 unsigned char *obuf, size_t buf_len, size_t *olen )
3871{
3872 size_t sid_len, cookie_len;
3873 unsigned char *p;
3874
3875 if( f_cookie_write == NULL || f_cookie_check == NULL )
3876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3877
3878 /*
3879 * Structure of ClientHello with record and handshake headers,
3880 * and expected values. We don't need to check a lot, more checks will be
3881 * done when actually parsing the ClientHello - skipping those checks
3882 * avoids code duplication and does not make cookie forging any easier.
3883 *
3884 * 0-0 ContentType type; copied, must be handshake
3885 * 1-2 ProtocolVersion version; copied
3886 * 3-4 uint16 epoch; copied, must be 0
3887 * 5-10 uint48 sequence_number; copied
3888 * 11-12 uint16 length; (ignored)
3889 *
3890 * 13-13 HandshakeType msg_type; (ignored)
3891 * 14-16 uint24 length; (ignored)
3892 * 17-18 uint16 message_seq; copied
3893 * 19-21 uint24 fragment_offset; copied, must be 0
3894 * 22-24 uint24 fragment_length; (ignored)
3895 *
3896 * 25-26 ProtocolVersion client_version; (ignored)
3897 * 27-58 Random random; (ignored)
3898 * 59-xx SessionID session_id; 1 byte len + sid_len content
3899 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3900 * ...
3901 *
3902 * Minimum length is 61 bytes.
3903 */
3904 if( in_len < 61 ||
3905 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3906 in[3] != 0 || in[4] != 0 ||
3907 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3908 {
3909 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3910 }
3911
3912 sid_len = in[59];
3913 if( sid_len > in_len - 61 )
3914 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3915
3916 cookie_len = in[60 + sid_len];
3917 if( cookie_len > in_len - 60 )
3918 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3919
3920 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3921 cli_id, cli_id_len ) == 0 )
3922 {
3923 /* Valid cookie */
3924 return( 0 );
3925 }
3926
3927 /*
3928 * If we get here, we've got an invalid cookie, let's prepare HVR.
3929 *
3930 * 0-0 ContentType type; copied
3931 * 1-2 ProtocolVersion version; copied
3932 * 3-4 uint16 epoch; copied
3933 * 5-10 uint48 sequence_number; copied
3934 * 11-12 uint16 length; olen - 13
3935 *
3936 * 13-13 HandshakeType msg_type; hello_verify_request
3937 * 14-16 uint24 length; olen - 25
3938 * 17-18 uint16 message_seq; copied
3939 * 19-21 uint24 fragment_offset; copied
3940 * 22-24 uint24 fragment_length; olen - 25
3941 *
3942 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3943 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3944 *
3945 * Minimum length is 28.
3946 */
3947 if( buf_len < 28 )
3948 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3949
3950 /* Copy most fields and adapt others */
3951 memcpy( obuf, in, 25 );
3952 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3953 obuf[25] = 0xfe;
3954 obuf[26] = 0xff;
3955
3956 /* Generate and write actual cookie */
3957 p = obuf + 28;
3958 if( f_cookie_write( p_cookie,
3959 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3960 {
3961 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3962 }
3963
3964 *olen = p - obuf;
3965
3966 /* Go back and fill length fields */
3967 obuf[27] = (unsigned char)( *olen - 28 );
3968
3969 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3970 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3971 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3972
3973 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3974 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3975
3976 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3977}
3978
3979/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003980 * Handle possible client reconnect with the same UDP quadruplet
3981 * (RFC 6347 Section 4.2.8).
3982 *
3983 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3984 * that looks like a ClientHello.
3985 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003986 * - if the input looks like a ClientHello without cookies,
3987 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003988 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003989 * - if the input looks like a ClientHello with a valid cookie,
3990 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003991 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003992 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003993 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003994 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003995 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3996 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003997 */
3998static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3999{
4000 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004001 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004002
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004003 ret = ssl_check_dtls_clihlo_cookie(
4004 ssl->conf->f_cookie_write,
4005 ssl->conf->f_cookie_check,
4006 ssl->conf->p_cookie,
4007 ssl->cli_id, ssl->cli_id_len,
4008 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004009 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004010
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004011 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4012
4013 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004014 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004015 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004016 * If the error is permanent we'll catch it later,
4017 * if it's not, then hopefully it'll work next time. */
4018 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4019
4020 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004021 }
4022
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004023 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004024 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004025 /* Got a valid cookie, partially reset context */
4026 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4027 {
4028 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4029 return( ret );
4030 }
4031
4032 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004033 }
4034
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004035 return( ret );
4036}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004037#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004038
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004039/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004040 * ContentType type;
4041 * ProtocolVersion version;
4042 * uint16 epoch; // DTLS only
4043 * uint48 sequence_number; // DTLS only
4044 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004045 *
4046 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004047 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004048 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4049 *
4050 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004051 * 1. proceed with the record if this function returns 0
4052 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4053 * 3. return CLIENT_RECONNECT if this function return that value
4054 * 4. drop the whole datagram if this function returns anything else.
4055 * Point 2 is needed when the peer is resending, and we have already received
4056 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004059{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004060 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 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 +02004063
Paul Bakker5121ce52009-01-03 21:22:43 +00004064 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004065 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004066 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004070 ssl->in_msgtype,
4071 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004072
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004073 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4075 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4076 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4077 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004080
4081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004082 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4083 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004084 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4085#endif /* MBEDTLS_SSL_PROTO_DTLS */
4086 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4087 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004090 }
4091
4092 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004093 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4096 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 }
4098
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004099 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4102 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 }
4104
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004105 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004106 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004107 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4110 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004111 }
4112
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004113 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004114 * DTLS-related tests.
4115 * Check epoch before checking length constraint because
4116 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4117 * message gets duplicated before the corresponding Finished message,
4118 * the second ChangeCipherSpec should be discarded because it belongs
4119 * to an old epoch, but not because its length is shorter than
4120 * the minimum record length for packets using the new record transform.
4121 * Note that these two kinds of failures are handled differently,
4122 * as an unexpected record is silently skipped but an invalid
4123 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004124 */
4125#if defined(MBEDTLS_SSL_PROTO_DTLS)
4126 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4127 {
4128 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4129
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004130 /* Check epoch (and sequence number) with DTLS */
4131 if( rec_epoch != ssl->in_epoch )
4132 {
4133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4134 "expected %d, received %d",
4135 ssl->in_epoch, rec_epoch ) );
4136
4137#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4138 /*
4139 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4140 * access the first byte of record content (handshake type), as we
4141 * have an active transform (possibly iv_len != 0), so use the
4142 * fact that the record header len is 13 instead.
4143 */
4144 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4145 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4146 rec_epoch == 0 &&
4147 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4148 ssl->in_left > 13 &&
4149 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4150 {
4151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4152 "from the same port" ) );
4153 return( ssl_handle_possible_reconnect( ssl ) );
4154 }
4155 else
4156#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4157 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4158 }
4159
4160#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4161 /* Replay detection only works for the current epoch */
4162 if( rec_epoch == ssl->in_epoch &&
4163 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4164 {
4165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4166 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4167 }
4168#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004169
4170 /* Drop unexpected ChangeCipherSpec messages */
4171 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4172 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4173 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4176 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4177 }
4178
4179 /* Drop unexpected ApplicationData records,
4180 * except at the beginning of renegotiations */
4181 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4182 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4183#if defined(MBEDTLS_SSL_RENEGOTIATION)
4184 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4185 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4186#endif
4187 )
4188 {
4189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4190 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4191 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004192 }
4193#endif /* MBEDTLS_SSL_PROTO_DTLS */
4194
Hanno Becker52c6dc62017-05-26 16:07:36 +01004195
4196 /* Check length against bounds of the current transform and version */
4197 if( ssl->transform_in == NULL )
4198 {
4199 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004200 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004201 {
4202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4203 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4204 }
4205 }
4206 else
4207 {
4208 if( ssl->in_msglen < ssl->transform_in->minlen )
4209 {
4210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4211 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4212 }
4213
4214#if defined(MBEDTLS_SSL_PROTO_SSL3)
4215 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004216 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004217 {
4218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4219 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4220 }
4221#endif
4222#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4223 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4224 /*
4225 * TLS encrypted messages can have up to 256 bytes of padding
4226 */
4227 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4228 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004229 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004230 {
4231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4232 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4233 }
4234#endif
4235 }
4236
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004237 return( 0 );
4238}
Paul Bakker5121ce52009-01-03 21:22:43 +00004239
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004240/*
4241 * If applicable, decrypt (and decompress) record content
4242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004244{
4245 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4248 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4251 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 ret = mbedtls_ssl_hw_record_read( ssl );
4256 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4259 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004260 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004261
4262 if( ret == 0 )
4263 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004266 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 {
4268 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004271 return( ret );
4272 }
4273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004275 ssl->in_msg, ssl->in_msglen );
4276
Angus Grattond8213d02016-05-25 20:56:48 +10004277 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4280 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 }
4282 }
4283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004285 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004287 {
4288 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004291 return( ret );
4292 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004293 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004300 }
4301#endif
4302
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004303 return( 0 );
4304}
4305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004307
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004308/*
4309 * Read a record.
4310 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004311 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4312 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4313 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004316{
4317 int ret;
4318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004320
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004321 if( ssl->keep_current_message == 0 )
4322 {
4323 do {
Simon Butcher99000142016-10-13 17:21:01 +01004324
Hanno Becker90333da2017-10-10 11:27:13 +01004325 do ret = mbedtls_ssl_read_record_layer( ssl );
4326 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4327
4328 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004329 {
4330 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4331 return( ret );
4332 }
4333
4334 ret = mbedtls_ssl_handle_message_type( ssl );
4335
Hanno Becker90333da2017-10-10 11:27:13 +01004336 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4337 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004338
4339 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004340 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004341 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004342 return( ret );
4343 }
4344
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004345 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4346 {
4347 mbedtls_ssl_update_handshake_status( ssl );
4348 }
Simon Butcher99000142016-10-13 17:21:01 +01004349 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004350 else
Simon Butcher99000142016-10-13 17:21:01 +01004351 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4353 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004354 }
4355
4356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4357
4358 return( 0 );
4359}
4360
4361int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4362{
4363 int ret;
4364
Hanno Becker4a810fb2017-05-24 16:27:30 +01004365 /*
4366 * Step A
4367 *
4368 * Consume last content-layer message and potentially
4369 * update in_msglen which keeps track of the contents'
4370 * consumption state.
4371 *
4372 * (1) Handshake messages:
4373 * Remove last handshake message, move content
4374 * and adapt in_msglen.
4375 *
4376 * (2) Alert messages:
4377 * Consume whole record content, in_msglen = 0.
4378 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004379 * (3) Change cipher spec:
4380 * Consume whole record content, in_msglen = 0.
4381 *
4382 * (4) Application data:
4383 * Don't do anything - the record layer provides
4384 * the application data as a stream transport
4385 * and consumes through mbedtls_ssl_read only.
4386 *
4387 */
4388
4389 /* Case (1): Handshake messages */
4390 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004391 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004392 /* Hard assertion to be sure that no application data
4393 * is in flight, as corrupting ssl->in_msglen during
4394 * ssl->in_offt != NULL is fatal. */
4395 if( ssl->in_offt != NULL )
4396 {
4397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4399 }
4400
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004401 /*
4402 * Get next Handshake message in the current record
4403 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004404
Hanno Becker4a810fb2017-05-24 16:27:30 +01004405 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004406 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004407 * current handshake content: If DTLS handshake
4408 * fragmentation is used, that's the fragment
4409 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004410 * size here is faulty and should be changed at
4411 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004412 * (2) While it doesn't seem to cause problems, one
4413 * has to be very careful not to assume that in_hslen
4414 * is always <= in_msglen in a sensible communication.
4415 * Again, it's wrong for DTLS handshake fragmentation.
4416 * The following check is therefore mandatory, and
4417 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004418 * Additionally, ssl->in_hslen might be arbitrarily out of
4419 * bounds after handling a DTLS message with an unexpected
4420 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004421 */
4422 if( ssl->in_hslen < ssl->in_msglen )
4423 {
4424 ssl->in_msglen -= ssl->in_hslen;
4425 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4426 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004427
Hanno Becker4a810fb2017-05-24 16:27:30 +01004428 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4429 ssl->in_msg, ssl->in_msglen );
4430 }
4431 else
4432 {
4433 ssl->in_msglen = 0;
4434 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004435
Hanno Becker4a810fb2017-05-24 16:27:30 +01004436 ssl->in_hslen = 0;
4437 }
4438 /* Case (4): Application data */
4439 else if( ssl->in_offt != NULL )
4440 {
4441 return( 0 );
4442 }
4443 /* Everything else (CCS & Alerts) */
4444 else
4445 {
4446 ssl->in_msglen = 0;
4447 }
4448
4449 /*
4450 * Step B
4451 *
4452 * Fetch and decode new record if current one is fully consumed.
4453 *
4454 */
4455
4456 if( ssl->in_msglen > 0 )
4457 {
4458 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004459 return( 0 );
4460 }
4461
Hanno Becker4a810fb2017-05-24 16:27:30 +01004462 /* Current record either fully processed or to be discarded. */
4463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004467 return( ret );
4468 }
4469
4470 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004473 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4474 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004475 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004476 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4477 {
4478 /* Skip unexpected record (but not whole datagram) */
4479 ssl->next_record_offset = ssl->in_msglen
4480 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004481
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4483 "(header)" ) );
4484 }
4485 else
4486 {
4487 /* Skip invalid record and the rest of the datagram */
4488 ssl->next_record_offset = 0;
4489 ssl->in_left = 0;
4490
4491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4492 "(header)" ) );
4493 }
4494
4495 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004496 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004497 }
4498#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004499 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004500 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004501
4502 /*
4503 * Read and optionally decrypt the message contents
4504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4506 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004509 return( ret );
4510 }
4511
4512 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004517 if( ssl->next_record_offset < ssl->in_left )
4518 {
4519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4520 }
4521 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004522 else
4523#endif
4524 ssl->in_left = 0;
4525
4526 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004529 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004530 {
4531 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4533 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004534 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004535 /* Except when waiting for Finished as a bad mac here
4536 * probably means something went wrong in the handshake
4537 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4538 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4539 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4540 {
4541#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4542 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4543 {
4544 mbedtls_ssl_send_alert_message( ssl,
4545 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4546 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4547 }
4548#endif
4549 return( ret );
4550 }
4551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004553 if( ssl->conf->badmac_limit != 0 &&
4554 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4557 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004558 }
4559#endif
4560
Hanno Becker4a810fb2017-05-24 16:27:30 +01004561 /* As above, invalid records cause
4562 * dismissal of the whole datagram. */
4563
4564 ssl->next_record_offset = 0;
4565 ssl->in_left = 0;
4566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004568 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004569 }
4570
4571 return( ret );
4572 }
4573 else
4574#endif
4575 {
4576 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4578 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 mbedtls_ssl_send_alert_message( ssl,
4581 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4582 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004583 }
4584#endif
4585 return( ret );
4586 }
4587 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004588
Simon Butcher99000142016-10-13 17:21:01 +01004589 return( 0 );
4590}
4591
4592int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4593{
4594 int ret;
4595
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004596 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004597 * Handle particular types of records
4598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 {
Simon Butcher99000142016-10-13 17:21:01 +01004601 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4602 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004603 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004605 }
4606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004609 if( ssl->in_msglen != 2 )
4610 {
4611 /* Note: Standard allows for more than one 2 byte alert
4612 to be packed in a single message, but Mbed TLS doesn't
4613 currently support this. */
4614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4615 ssl->in_msglen ) );
4616 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4617 }
4618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 ssl->in_msg[0], ssl->in_msg[1] ) );
4621
4622 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004623 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004628 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004630 }
4631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4633 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4636 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004637 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004638
4639#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4640 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4641 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4642 {
Hanno Becker90333da2017-10-10 11:27:13 +01004643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004644 /* Will be handled when trying to parse ServerHello */
4645 return( 0 );
4646 }
4647#endif
4648
4649#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4650 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4651 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4652 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4653 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4654 {
4655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4656 /* Will be handled in mbedtls_ssl_parse_certificate() */
4657 return( 0 );
4658 }
4659#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4660
4661 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004662 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 }
4664
Hanno Beckerc76c6192017-06-06 10:03:17 +01004665#if defined(MBEDTLS_SSL_PROTO_DTLS)
4666 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4667 ssl->handshake != NULL &&
4668 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4669 {
4670 ssl_handshake_wrapup_free_hs_transform( ssl );
4671 }
4672#endif
4673
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 return( 0 );
4675}
4676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004678{
4679 int ret;
4680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4682 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4683 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004684 {
4685 return( ret );
4686 }
4687
4688 return( 0 );
4689}
4690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004692 unsigned char level,
4693 unsigned char message )
4694{
4695 int ret;
4696
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004697 if( ssl == NULL || ssl->conf == NULL )
4698 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004704 ssl->out_msglen = 2;
4705 ssl->out_msg[0] = level;
4706 ssl->out_msg[1] = message;
4707
Hanno Becker67bc7c32018-08-06 11:33:50 +01004708 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004711 return( ret );
4712 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004714
4715 return( 0 );
4716}
4717
Paul Bakker5121ce52009-01-03 21:22:43 +00004718/*
4719 * Handshake functions
4720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4722 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4723 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4724 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4725 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4726 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4727 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004728/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4736 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004737 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4738 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004741 ssl->state++;
4742 return( 0 );
4743 }
4744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004747}
4748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4756 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004757 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4758 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004761 ssl->state++;
4762 return( 0 );
4763 }
4764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004767}
Gilles Peskinef9828522017-05-03 12:28:43 +02004768
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004769#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004770/* Some certificate support -> implement write and parse */
4771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004775 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 const mbedtls_x509_crt *crt;
4777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4782 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004783 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4784 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004787 ssl->state++;
4788 return( 0 );
4789 }
4790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004792 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004793 {
4794 if( ssl->client_auth == 0 )
4795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 ssl->state++;
4798 return( 0 );
4799 }
4800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 /*
4803 * If using SSLv3 and got no cert, send an Alert message
4804 * (otherwise an empty Certificate message will be sent).
4805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4807 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 {
4809 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4811 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4812 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004815 goto write_msg;
4816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819#endif /* MBEDTLS_SSL_CLI_C */
4820#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4826 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004827 }
4828 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004829#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004832
4833 /*
4834 * 0 . 0 handshake type
4835 * 1 . 3 handshake length
4836 * 4 . 6 length of all certs
4837 * 7 . 9 length of cert. 1
4838 * 10 . n-1 peer certificate
4839 * n . n+2 length of cert. 2
4840 * n+3 . ... upper level cert, etc.
4841 */
4842 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004844
Paul Bakker29087132010-03-21 21:03:34 +00004845 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 {
4847 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004848 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004851 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004853 }
4854
4855 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4856 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4857 ssl->out_msg[i + 2] = (unsigned char)( n );
4858
4859 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4860 i += n; crt = crt->next;
4861 }
4862
4863 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4864 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4865 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4866
4867 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4869 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004870
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004871#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004872write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004873#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004874
4875 ssl->state++;
4876
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004877 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004878 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 return( ret );
4881 }
4882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004884
Paul Bakkered27a042013-04-18 22:46:23 +02004885 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004886}
4887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004891 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004893 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004894 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4899 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004900 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4901 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004904 ssl->state++;
4905 return( 0 );
4906 }
4907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004909 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004910 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4911 {
4912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4913 ssl->state++;
4914 return( 0 );
4915 }
4916
4917#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4918 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4919 authmode = ssl->handshake->sni_authmode;
4920#endif
4921
4922 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4923 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004924 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004925 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004927 ssl->state++;
4928 return( 0 );
4929 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004930#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004934 /* mbedtls_ssl_read_record may have sent an alert already. We
4935 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004937 return( ret );
4938 }
4939
4940 ssl->state++;
4941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_SRV_C)
4943#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004944 /*
4945 * Check if the client sent an empty certificate
4946 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004949 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004950 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4952 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4953 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004956
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004957 /* The client was asked for a certificate but didn't send
4958 one. The client should know what's going on, so we
4959 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004960 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004961 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004962 return( 0 );
4963 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004965 }
4966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4970 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004971 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4975 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4976 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4977 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004980
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004981 /* The client was asked for a certificate but didn't send
4982 one. The client should know what's going on, so we
4983 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004984 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004985 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004986 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004987 else
4988 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004989 }
4990 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4992 MBEDTLS_SSL_PROTO_TLS1_2 */
4993#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004998 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4999 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005001 }
5002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5004 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005007 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5008 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005010 }
5011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005012 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005013
Paul Bakker5121ce52009-01-03 21:22:43 +00005014 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005016 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005017 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005018
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005019 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005023 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5024 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005026 }
5027
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005028 /* In case we tried to reuse a session but it failed */
5029 if( ssl->session_negotiate->peer_cert != NULL )
5030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5032 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005033 }
5034
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005035 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005037 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005040 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5041 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005042 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005043 }
5044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005046
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005047 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005048
5049 while( i < ssl->in_hslen )
5050 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005051 if ( i + 3 > ssl->in_hslen ) {
5052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5053 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5054 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5055 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005057 if( ssl->in_msg[i] != 0 )
5058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005060 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5061 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005063 }
5064
5065 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5066 | (unsigned int) ssl->in_msg[i + 2];
5067 i += 3;
5068
5069 if( n < 128 || i + n > ssl->in_hslen )
5070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005072 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5073 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005075 }
5076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005078 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005079 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005080 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005081 case 0: /*ok*/
5082 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5083 /* Ignore certificate with an unknown algorithm: maybe a
5084 prior certificate was already trusted. */
5085 break;
5086
5087 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5088 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5089 goto crt_parse_der_failed;
5090
5091 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5092 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5093 goto crt_parse_der_failed;
5094
5095 default:
5096 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5097 crt_parse_der_failed:
5098 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005100 return( ret );
5101 }
5102
5103 i += n;
5104 }
5105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005107
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005108 /*
5109 * On client, make sure the server cert doesn't change during renego to
5110 * avoid "triple handshake" attack: https://secure-resumption.com/
5111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005113 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005115 {
5116 if( ssl->session->peer_cert == NULL )
5117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5120 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005122 }
5123
5124 if( ssl->session->peer_cert->raw.len !=
5125 ssl->session_negotiate->peer_cert->raw.len ||
5126 memcmp( ssl->session->peer_cert->raw.p,
5127 ssl->session_negotiate->peer_cert->raw.p,
5128 ssl->session->peer_cert->raw.len ) != 0 )
5129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005131 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5132 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005134 }
5135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005137
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005138 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005139 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005140 mbedtls_x509_crt *ca_chain;
5141 mbedtls_x509_crl *ca_crl;
5142
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005143#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005144 if( ssl->handshake->sni_ca_chain != NULL )
5145 {
5146 ca_chain = ssl->handshake->sni_ca_chain;
5147 ca_crl = ssl->handshake->sni_ca_crl;
5148 }
5149 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005150#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005151 {
5152 ca_chain = ssl->conf->ca_chain;
5153 ca_crl = ssl->conf->ca_crl;
5154 }
5155
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005156 /*
5157 * Main check: verify certificate
5158 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005159 ret = mbedtls_x509_crt_verify_with_profile(
5160 ssl->session_negotiate->peer_cert,
5161 ca_chain, ca_crl,
5162 ssl->conf->cert_profile,
5163 ssl->hostname,
5164 &ssl->session_negotiate->verify_result,
5165 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005166
5167 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005170 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005171
5172 /*
5173 * Secondary checks: always done, but change 'ret' only if it was 0
5174 */
5175
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005176#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005179
5180 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005182 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005183 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005184 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005187 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005189 }
5190 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005191#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005194 ciphersuite_info,
5195 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005196 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005199 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005201 }
5202
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005203 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5204 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5205 * with details encoded in the verification flags. All other kinds
5206 * of error codes, including those from the user provided f_vrfy
5207 * functions, are treated as fatal and lead to a failure of
5208 * ssl_parse_certificate even if verification was optional. */
5209 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5210 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5211 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5212 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005214 }
5215
5216 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5217 {
5218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5219 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5220 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005221
5222 if( ret != 0 )
5223 {
5224 /* The certificate may have been rejected for several reasons.
5225 Pick one and send the corresponding alert. Which alert to send
5226 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005227 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5228 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5229 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5230 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5231 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5232 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5233 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5234 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5235 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5236 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5237 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5238 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5239 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5240 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5241 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5242 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5243 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5244 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5245 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5246 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005247 else
5248 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005249 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5250 alert );
5251 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005252
Hanno Beckere6706e62017-05-15 16:05:15 +01005253#if defined(MBEDTLS_DEBUG_C)
5254 if( ssl->session_negotiate->verify_result != 0 )
5255 {
5256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5257 ssl->session_negotiate->verify_result ) );
5258 }
5259 else
5260 {
5261 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5262 }
5263#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005264 }
5265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005267
5268 return( ret );
5269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5271 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5272 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5273 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5274 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5275 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5276 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005279{
5280 int ret;
5281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005285 ssl->out_msglen = 1;
5286 ssl->out_msg[0] = 1;
5287
Paul Bakker5121ce52009-01-03 21:22:43 +00005288 ssl->state++;
5289
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005290 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005291 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005293 return( ret );
5294 }
5295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
5298 return( 0 );
5299}
5300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005302{
5303 int ret;
5304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005310 return( ret );
5311 }
5312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5317 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005319 }
5320
5321 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005324 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5325 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 }
5328
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005329 /*
5330 * Switch to our negotiated transform and session parameters for inbound
5331 * data.
5332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005334 ssl->transform_in = ssl->transform_negotiate;
5335 ssl->session_in = ssl->session_negotiate;
5336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005341 ssl_dtls_replay_reset( ssl );
5342#endif
5343
5344 /* Increment epoch */
5345 if( ++ssl->in_epoch == 0 )
5346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005348 /* This is highly unlikely to happen for legitimate reasons, so
5349 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005351 }
5352 }
5353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005355 memset( ssl->in_ctr, 0, 8 );
5356
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005357 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5360 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005365 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5366 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005368 }
5369 }
5370#endif
5371
Paul Bakker5121ce52009-01-03 21:22:43 +00005372 ssl->state++;
5373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005375
5376 return( 0 );
5377}
5378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5380 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005381{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005382 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5385 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5386 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005387 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005388 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5391#if defined(MBEDTLS_SHA512_C)
5392 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005393 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5394 else
5395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396#if defined(MBEDTLS_SHA256_C)
5397 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005398 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005399 else
5400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005404 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005405 }
Paul Bakker380da532012-04-18 16:10:25 +00005406}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5411 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005412 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5413 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5416#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005417 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005420 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005423}
5424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005426 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5429 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005430 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5431 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5434#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005435 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005436#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005438 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005439#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005441}
5442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5444 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5445static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005446 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005447{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005448 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5449 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005450}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005451#endif
Paul Bakker380da532012-04-18 16:10:25 +00005452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5454#if defined(MBEDTLS_SHA256_C)
5455static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005456 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005457{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005458 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005459}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005460#endif
Paul Bakker380da532012-04-18 16:10:25 +00005461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462#if defined(MBEDTLS_SHA512_C)
5463static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005464 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005465{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005466 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005467}
Paul Bakker769075d2012-11-24 11:26:46 +01005468#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005472static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005474{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005475 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005476 mbedtls_md5_context md5;
5477 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005478
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 unsigned char padbuf[48];
5480 unsigned char md5sum[16];
5481 unsigned char sha1sum[20];
5482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005484 if( !session )
5485 session = ssl->session;
5486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005488
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005489 mbedtls_md5_init( &md5 );
5490 mbedtls_sha1_init( &sha1 );
5491
5492 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5493 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005494
5495 /*
5496 * SSLv3:
5497 * hash =
5498 * MD5( master + pad2 +
5499 * MD5( handshake + sender + master + pad1 ) )
5500 * + SHA1( master + pad2 +
5501 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 */
5503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005505 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5506 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005510 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5511 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005512#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005515 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005516
Paul Bakker1ef83d62012-04-11 12:09:53 +00005517 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005518
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005519 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5520 mbedtls_md5_update_ret( &md5, session->master, 48 );
5521 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5522 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005523
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005524 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5525 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5526 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5527 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005528
Paul Bakker1ef83d62012-04-11 12:09:53 +00005529 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005530
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005531 mbedtls_md5_starts_ret( &md5 );
5532 mbedtls_md5_update_ret( &md5, session->master, 48 );
5533 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5534 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5535 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005536
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005537 mbedtls_sha1_starts_ret( &sha1 );
5538 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5539 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5540 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5541 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005544
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005545 mbedtls_md5_free( &md5 );
5546 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005547
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005548 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5549 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5550 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005557static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005559{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005560 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005561 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005562 mbedtls_md5_context md5;
5563 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005564 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005567 if( !session )
5568 session = ssl->session;
5569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005571
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005572 mbedtls_md5_init( &md5 );
5573 mbedtls_sha1_init( &sha1 );
5574
5575 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5576 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005577
Paul Bakker1ef83d62012-04-11 12:09:53 +00005578 /*
5579 * TLSv1:
5580 * hash = PRF( master, finished_label,
5581 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5582 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005585 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5586 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005587#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005590 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5591 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005592#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005595 ? "client finished"
5596 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005597
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005598 mbedtls_md5_finish_ret( &md5, padbuf );
5599 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005600
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005601 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005602 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005605
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005606 mbedtls_md5_free( &md5 );
5607 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005608
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005609 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5616#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005617static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005619{
5620 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005621 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005622 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005623 unsigned char padbuf[32];
5624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005626 if( !session )
5627 session = ssl->session;
5628
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005629 mbedtls_sha256_init( &sha256 );
5630
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005632
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005633 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005634
5635 /*
5636 * TLSv1.2:
5637 * hash = PRF( master, finished_label,
5638 * Hash( handshake ) )[0.11]
5639 */
5640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641#if !defined(MBEDTLS_SHA256_ALT)
5642 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005643 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005644#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005647 ? "client finished"
5648 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005649
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005650 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005651
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005652 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005653 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005656
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005657 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005658
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005659 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005666static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005668{
5669 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005670 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005671 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005672 unsigned char padbuf[48];
5673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005675 if( !session )
5676 session = ssl->session;
5677
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005678 mbedtls_sha512_init( &sha512 );
5679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005681
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005682 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005683
5684 /*
5685 * TLSv1.2:
5686 * hash = PRF( master, finished_label,
5687 * Hash( handshake ) )[0.11]
5688 */
5689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005691 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5692 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005693#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005696 ? "client finished"
5697 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005698
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005699 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005700
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005701 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005702 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005705
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005706 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005707
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005708 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#endif /* MBEDTLS_SHA512_C */
5713#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005718
5719 /*
5720 * Free our handshake params
5721 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005722 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005724 ssl->handshake = NULL;
5725
5726 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005727 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005728 */
5729 if( ssl->transform )
5730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 mbedtls_ssl_transform_free( ssl->transform );
5732 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005733 }
5734 ssl->transform = ssl->transform_negotiate;
5735 ssl->transform_negotiate = NULL;
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005738}
5739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005741{
5742 int resume = ssl->handshake->resume;
5743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_RENEGOTIATION)
5747 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005750 ssl->renego_records_seen = 0;
5751 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005752#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005753
5754 /*
5755 * Free the previous session and switch in the current one
5756 */
Paul Bakker0a597072012-09-25 21:55:46 +00005757 if( ssl->session )
5758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005760 /* RFC 7366 3.1: keep the EtM state */
5761 ssl->session_negotiate->encrypt_then_mac =
5762 ssl->session->encrypt_then_mac;
5763#endif
5764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 mbedtls_ssl_session_free( ssl->session );
5766 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005767 }
5768 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005769 ssl->session_negotiate = NULL;
5770
Paul Bakker0a597072012-09-25 21:55:46 +00005771 /*
5772 * Add cache entry
5773 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005774 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005775 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005776 resume == 0 )
5777 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005778 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005780 }
Paul Bakker0a597072012-09-25 21:55:46 +00005781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005784 ssl->handshake->flight != NULL )
5785 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005786 /* Cancel handshake timer */
5787 ssl_set_timer( ssl, 0 );
5788
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005789 /* Keep last flight around in case we need to resend it:
5790 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005792 }
5793 else
5794#endif
5795 ssl_handshake_wrapup_free_hs_transform( ssl );
5796
Paul Bakker48916f92012-09-16 19:57:18 +00005797 ssl->state++;
5798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005800}
5801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005803{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005804 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005807
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005808 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01005809
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005810 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005811
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005812 /*
5813 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5814 * may define some other value. Currently (early 2016), no defined
5815 * ciphersuite does this (and this is unlikely to change as activity has
5816 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005821 ssl->verify_data_len = hash_len;
5822 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005823#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005824
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5827 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005828
5829 /*
5830 * In case of session resuming, invert the client and server
5831 * ChangeCipherSpec messages order.
5832 */
Paul Bakker0a597072012-09-25 21:55:46 +00005833 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005836 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005838#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005840 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005842#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 }
5844 else
5845 ssl->state++;
5846
Paul Bakker48916f92012-09-16 19:57:18 +00005847 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005848 * Switch to our negotiated transform and session parameters for outbound
5849 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005854 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005855 {
5856 unsigned char i;
5857
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005858 /* Remember current epoch settings for resending */
5859 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01005860 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005861
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005862 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01005863 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005864
5865 /* Increment epoch */
5866 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01005867 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005868 break;
5869
5870 /* The loop goes to its end iff the counter is wrapping */
5871 if( i == 0 )
5872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5874 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005875 }
5876 }
5877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01005879 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005881 ssl->transform_out = ssl->transform_negotiate;
5882 ssl->session_out = ssl->session_negotiate;
5883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5885 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5890 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005891 }
5892 }
5893#endif
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005896 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005898#endif
5899
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005900 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005901 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 return( ret );
5904 }
5905
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005906#if defined(MBEDTLS_SSL_PROTO_DTLS)
5907 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5908 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5909 {
5910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5911 return( ret );
5912 }
5913#endif
5914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005916
5917 return( 0 );
5918}
5919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005921#define SSL_MAX_HASH_LEN 36
5922#else
5923#define SSL_MAX_HASH_LEN 12
5924#endif
5925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005927{
Paul Bakker23986e52011-04-24 08:57:21 +00005928 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005929 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005930 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005934 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939 return( ret );
5940 }
5941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005945 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5946 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005948 }
5949
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005950 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951#if defined(MBEDTLS_SSL_PROTO_SSL3)
5952 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005953 hash_len = 36;
5954 else
5955#endif
5956 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5959 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005962 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5963 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005965 }
5966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005968 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005971 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5972 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005974 }
5975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005977 ssl->verify_data_len = hash_len;
5978 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005979#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005980
Paul Bakker0a597072012-09-25 21:55:46 +00005981 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005984 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005986#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005988 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005990#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005991 }
5992 else
5993 ssl->state++;
5994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005996 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005998#endif
5999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006001
6002 return( 0 );
6003}
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6010 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6011 mbedtls_md5_init( &handshake->fin_md5 );
6012 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006013 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6014 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6017#if defined(MBEDTLS_SHA256_C)
6018 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006019 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006020#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021#if defined(MBEDTLS_SHA512_C)
6022 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006023 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006026
6027 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006028
6029#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6030 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6031 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6032#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#if defined(MBEDTLS_DHM_C)
6035 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037#if defined(MBEDTLS_ECDH_C)
6038 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006039#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006040#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006041 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006042#if defined(MBEDTLS_SSL_CLI_C)
6043 handshake->ecjpake_cache = NULL;
6044 handshake->ecjpake_cache_len = 0;
6045#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006046#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006047
6048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6049 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6050#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006051}
6052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6058 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 mbedtls_md_init( &transform->md_ctx_enc );
6061 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006062}
6063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006067}
6068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006070{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006071 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006072 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006074 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006076 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006077 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006078
6079 /*
6080 * Either the pointers are now NULL or cleared properly and can be freed.
6081 * Now allocate missing structures.
6082 */
6083 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006084 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006085 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006086 }
Paul Bakker48916f92012-09-16 19:57:18 +00006087
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006088 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006089 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006090 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006091 }
Paul Bakker48916f92012-09-16 19:57:18 +00006092
Paul Bakker82788fb2014-10-20 13:59:19 +02006093 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006094 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006095 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006096 }
Paul Bakker48916f92012-09-16 19:57:18 +00006097
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006098 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006099 if( ssl->handshake == NULL ||
6100 ssl->transform_negotiate == NULL ||
6101 ssl->session_negotiate == NULL )
6102 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 mbedtls_free( ssl->handshake );
6106 mbedtls_free( ssl->transform_negotiate );
6107 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006108
6109 ssl->handshake = NULL;
6110 ssl->transform_negotiate = NULL;
6111 ssl->session_negotiate = NULL;
6112
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006113 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006114 }
6115
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006116 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006118 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006119 ssl_handshake_params_init( ssl->handshake );
6120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6123 {
6124 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006125
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006126 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6127 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6128 else
6129 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006130
6131 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006132 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006133#endif
6134
Paul Bakker48916f92012-09-16 19:57:18 +00006135 return( 0 );
6136}
6137
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006138#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006139/* Dummy cookie callbacks for defaults */
6140static int ssl_cookie_write_dummy( void *ctx,
6141 unsigned char **p, unsigned char *end,
6142 const unsigned char *cli_id, size_t cli_id_len )
6143{
6144 ((void) ctx);
6145 ((void) p);
6146 ((void) end);
6147 ((void) cli_id);
6148 ((void) cli_id_len);
6149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006151}
6152
6153static int ssl_cookie_check_dummy( void *ctx,
6154 const unsigned char *cookie, size_t cookie_len,
6155 const unsigned char *cli_id, size_t cli_id_len )
6156{
6157 ((void) ctx);
6158 ((void) cookie);
6159 ((void) cookie_len);
6160 ((void) cli_id);
6161 ((void) cli_id_len);
6162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006164}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006165#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006166
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006167/* Once ssl->out_hdr as the address of the beginning of the
6168 * next outgoing record is set, deduce the other pointers.
6169 *
6170 * Note: For TLS, we save the implicit record sequence number
6171 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6172 * and the caller has to make sure there's space for this.
6173 */
6174
6175static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6176 mbedtls_ssl_transform *transform )
6177{
6178#if defined(MBEDTLS_SSL_PROTO_DTLS)
6179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6180 {
6181 ssl->out_ctr = ssl->out_hdr + 3;
6182 ssl->out_len = ssl->out_hdr + 11;
6183 ssl->out_iv = ssl->out_hdr + 13;
6184 }
6185 else
6186#endif
6187 {
6188 ssl->out_ctr = ssl->out_hdr - 8;
6189 ssl->out_len = ssl->out_hdr + 3;
6190 ssl->out_iv = ssl->out_hdr + 5;
6191 }
6192
6193 /* Adjust out_msg to make space for explicit IV, if used. */
6194 if( transform != NULL &&
6195 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6196 {
6197 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6198 }
6199 else
6200 ssl->out_msg = ssl->out_iv;
6201}
6202
6203/* Once ssl->in_hdr as the address of the beginning of the
6204 * next incoming record is set, deduce the other pointers.
6205 *
6206 * Note: For TLS, we save the implicit record sequence number
6207 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6208 * and the caller has to make sure there's space for this.
6209 */
6210
6211static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6212 mbedtls_ssl_transform *transform )
6213{
6214#if defined(MBEDTLS_SSL_PROTO_DTLS)
6215 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6216 {
6217 ssl->in_ctr = ssl->in_hdr + 3;
6218 ssl->in_len = ssl->in_hdr + 11;
6219 ssl->in_iv = ssl->in_hdr + 13;
6220 }
6221 else
6222#endif
6223 {
6224 ssl->in_ctr = ssl->in_hdr - 8;
6225 ssl->in_len = ssl->in_hdr + 3;
6226 ssl->in_iv = ssl->in_hdr + 5;
6227 }
6228
6229 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6230 if( transform != NULL &&
6231 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6232 {
6233 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6234 }
6235 else
6236 ssl->in_msg = ssl->in_iv;
6237}
6238
Paul Bakker5121ce52009-01-03 21:22:43 +00006239/*
6240 * Initialize an SSL context
6241 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006242void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6243{
6244 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6245}
6246
6247/*
6248 * Setup an SSL context
6249 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006250
6251static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6252{
6253 /* Set the incoming and outgoing record pointers. */
6254#if defined(MBEDTLS_SSL_PROTO_DTLS)
6255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6256 {
6257 ssl->out_hdr = ssl->out_buf;
6258 ssl->in_hdr = ssl->in_buf;
6259 }
6260 else
6261#endif /* MBEDTLS_SSL_PROTO_DTLS */
6262 {
6263 ssl->out_hdr = ssl->out_buf + 8;
6264 ssl->in_hdr = ssl->in_buf + 8;
6265 }
6266
6267 /* Derive other internal pointers. */
6268 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6269 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6270}
6271
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006272int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006273 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006274{
Paul Bakker48916f92012-09-16 19:57:18 +00006275 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006276
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006277 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006278
6279 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006280 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006281 */
Angus Grattond8213d02016-05-25 20:56:48 +10006282 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6283 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 {
Angus Grattond8213d02016-05-25 20:56:48 +10006285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6286 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6287 }
6288
6289 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6290 if( ssl->out_buf == NULL )
6291 {
6292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006294 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 }
6297
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006298 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006299
Paul Bakker48916f92012-09-16 19:57:18 +00006300 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6301 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006302
6303 return( 0 );
6304}
6305
6306/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006307 * Reset an initialized and used SSL context for re-use while retaining
6308 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006309 *
6310 * If partial is non-zero, keep data in the input buffer and client ID.
6311 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006312 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006313static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006314{
Paul Bakker48916f92012-09-16 19:57:18 +00006315 int ret;
6316
Hanno Becker7e772132018-08-10 12:38:21 +01006317#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6318 !defined(MBEDTLS_SSL_SRV_C)
6319 ((void) partial);
6320#endif
6321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006323
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006324 /* Cancel any possibly running timer */
6325 ssl_set_timer( ssl, 0 );
6326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327#if defined(MBEDTLS_SSL_RENEGOTIATION)
6328 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006329 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006330
6331 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6333 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006336
Paul Bakker7eb013f2011-10-06 12:37:39 +00006337 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006338 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006339
6340 ssl->in_msgtype = 0;
6341 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006343 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006344 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006345#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006347 ssl_dtls_replay_reset( ssl );
6348#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006349
6350 ssl->in_hslen = 0;
6351 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006352
6353 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006354
6355 ssl->out_msgtype = 0;
6356 ssl->out_msglen = 0;
6357 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6359 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006360 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006361#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006362
Hanno Becker19859472018-08-06 09:40:20 +01006363 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6364
Paul Bakker48916f92012-09-16 19:57:18 +00006365 ssl->transform_in = NULL;
6366 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006367
Hanno Becker78640902018-08-13 16:35:15 +01006368 ssl->session_in = NULL;
6369 ssl->session_out = NULL;
6370
Angus Grattond8213d02016-05-25 20:56:48 +10006371 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006372
6373#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006374 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006375#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6376 {
6377 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006378 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006379 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6382 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6385 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6388 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006389 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006390 }
6391#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006392
Paul Bakker48916f92012-09-16 19:57:18 +00006393 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 mbedtls_ssl_transform_free( ssl->transform );
6396 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006397 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006398 }
Paul Bakker48916f92012-09-16 19:57:18 +00006399
Paul Bakkerc0463502013-02-14 11:19:38 +01006400 if( ssl->session )
6401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 mbedtls_ssl_session_free( ssl->session );
6403 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006404 ssl->session = NULL;
6405 }
6406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006408 ssl->alpn_chosen = NULL;
6409#endif
6410
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006411#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006412#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006413 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006414#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006415 {
6416 mbedtls_free( ssl->cli_id );
6417 ssl->cli_id = NULL;
6418 ssl->cli_id_len = 0;
6419 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006420#endif
6421
Paul Bakker48916f92012-09-16 19:57:18 +00006422 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6423 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006424
6425 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006426}
6427
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006428/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006429 * Reset an initialized and used SSL context for re-use while retaining
6430 * all application-set variables, function pointers and data.
6431 */
6432int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6433{
6434 return( ssl_session_reset_int( ssl, 0 ) );
6435}
6436
6437/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006438 * SSL set accessors
6439 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006440void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006442 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006443}
6444
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006445void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006447 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006448}
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006451void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006453 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006454}
6455#endif
6456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006458void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006459{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006460 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006461}
6462#endif
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006465
6466void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6467 unsigned allow_packing )
6468{
6469 ssl->disable_datagram_packing = !allow_packing;
6470}
6471
6472void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6473 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006474{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006475 conf->hs_timeout_min = min;
6476 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006477}
6478#endif
6479
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006480void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006481{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006482 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006483}
6484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006486void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006487 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006488 void *p_vrfy )
6489{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006490 conf->f_vrfy = f_vrfy;
6491 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006494
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006495void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006496 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006497 void *p_rng )
6498{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006499 conf->f_rng = f_rng;
6500 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006501}
6502
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006503void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006504 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006505 void *p_dbg )
6506{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006507 conf->f_dbg = f_dbg;
6508 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006509}
6510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006512 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006513 mbedtls_ssl_send_t *f_send,
6514 mbedtls_ssl_recv_t *f_recv,
6515 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006516{
6517 ssl->p_bio = p_bio;
6518 ssl->f_send = f_send;
6519 ssl->f_recv = f_recv;
6520 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006521}
6522
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006523#if defined(MBEDTLS_SSL_PROTO_DTLS)
6524void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6525{
6526 ssl->mtu = mtu;
6527}
6528#endif
6529
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006530void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006531{
6532 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006533}
6534
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006535void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6536 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006537 mbedtls_ssl_set_timer_t *f_set_timer,
6538 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006539{
6540 ssl->p_timer = p_timer;
6541 ssl->f_set_timer = f_set_timer;
6542 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006543
6544 /* Make sure we start with no timer running */
6545 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006546}
6547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006549void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006550 void *p_cache,
6551 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6552 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006554 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006555 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006556 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560#if defined(MBEDTLS_SSL_CLI_C)
6561int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006562{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006563 int ret;
6564
6565 if( ssl == NULL ||
6566 session == NULL ||
6567 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006568 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006571 }
6572
6573 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6574 return( ret );
6575
Paul Bakker0a597072012-09-25 21:55:46 +00006576 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006577
6578 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006581
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006582void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006583 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006585 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6586 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6587 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6588 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006589}
6590
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006591void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006592 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006593 int major, int minor )
6594{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006596 return;
6597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006599 return;
6600
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006601 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006602}
6603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006605void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006606 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006607{
6608 conf->cert_profile = profile;
6609}
6610
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006611/* Append a new keycert entry to a (possibly empty) list */
6612static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6613 mbedtls_x509_crt *cert,
6614 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006615{
niisato8ee24222018-06-25 19:05:48 +09006616 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006617
niisato8ee24222018-06-25 19:05:48 +09006618 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6619 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006620 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006621
niisato8ee24222018-06-25 19:05:48 +09006622 new_cert->cert = cert;
6623 new_cert->key = key;
6624 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006625
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006626 /* Update head is the list was null, else add to the end */
6627 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006628 {
niisato8ee24222018-06-25 19:05:48 +09006629 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006630 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006631 else
6632 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006633 mbedtls_ssl_key_cert *cur = *head;
6634 while( cur->next != NULL )
6635 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006636 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006637 }
6638
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006639 return( 0 );
6640}
6641
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006642int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006643 mbedtls_x509_crt *own_cert,
6644 mbedtls_pk_context *pk_key )
6645{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006646 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006647}
6648
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006649void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006650 mbedtls_x509_crt *ca_chain,
6651 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006652{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006653 conf->ca_chain = ca_chain;
6654 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006657
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006658#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6659int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6660 mbedtls_x509_crt *own_cert,
6661 mbedtls_pk_context *pk_key )
6662{
6663 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6664 own_cert, pk_key ) );
6665}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006666
6667void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6668 mbedtls_x509_crt *ca_chain,
6669 mbedtls_x509_crl *ca_crl )
6670{
6671 ssl->handshake->sni_ca_chain = ca_chain;
6672 ssl->handshake->sni_ca_crl = ca_crl;
6673}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006674
6675void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6676 int authmode )
6677{
6678 ssl->handshake->sni_authmode = authmode;
6679}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006680#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6681
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006682#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006683/*
6684 * Set EC J-PAKE password for current handshake
6685 */
6686int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6687 const unsigned char *pw,
6688 size_t pw_len )
6689{
6690 mbedtls_ecjpake_role role;
6691
Janos Follath8eb64132016-06-03 15:40:57 +01006692 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6694
6695 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6696 role = MBEDTLS_ECJPAKE_SERVER;
6697 else
6698 role = MBEDTLS_ECJPAKE_CLIENT;
6699
6700 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6701 role,
6702 MBEDTLS_MD_SHA256,
6703 MBEDTLS_ECP_DP_SECP256R1,
6704 pw, pw_len ) );
6705}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006706#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006709int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006710 const unsigned char *psk, size_t psk_len,
6711 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006712{
Paul Bakker6db455e2013-09-18 17:29:31 +02006713 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006718
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006719 /* Identity len will be encoded on two bytes */
6720 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006721 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006722 {
6723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6724 }
6725
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006726 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006727 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006728 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006729
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006730 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006731 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006732 conf->psk_len = 0;
6733 }
6734 if( conf->psk_identity != NULL )
6735 {
6736 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006737 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006738 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006739 }
6740
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006741 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6742 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006743 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006744 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006745 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006746 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006747 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006748 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006749 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006750
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006751 conf->psk_len = psk_len;
6752 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006753
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006754 memcpy( conf->psk, psk, conf->psk_len );
6755 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006756
6757 return( 0 );
6758}
6759
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006760int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6761 const unsigned char *psk, size_t psk_len )
6762{
6763 if( psk == NULL || ssl->handshake == NULL )
6764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6765
6766 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6768
6769 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006770 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006771 mbedtls_platform_zeroize( ssl->handshake->psk,
6772 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006773 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006774 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006775 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006776
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006777 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006779
6780 ssl->handshake->psk_len = psk_len;
6781 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6782
6783 return( 0 );
6784}
6785
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006786void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006788 size_t),
6789 void *p_psk )
6790{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006791 conf->f_psk = f_psk;
6792 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006795
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006796#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006797
6798#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006799int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800{
6801 int ret;
6802
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006803 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6804 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6805 {
6806 mbedtls_mpi_free( &conf->dhm_P );
6807 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006808 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006809 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
6811 return( 0 );
6812}
Hanno Becker470a8c42017-10-04 15:28:46 +01006813#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
Hanno Beckera90658f2017-10-04 15:29:08 +01006815int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6816 const unsigned char *dhm_P, size_t P_len,
6817 const unsigned char *dhm_G, size_t G_len )
6818{
6819 int ret;
6820
6821 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6822 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6823 {
6824 mbedtls_mpi_free( &conf->dhm_P );
6825 mbedtls_mpi_free( &conf->dhm_G );
6826 return( ret );
6827 }
6828
6829 return( 0 );
6830}
Paul Bakker5121ce52009-01-03 21:22:43 +00006831
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006832int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006833{
6834 int ret;
6835
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006836 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6837 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6838 {
6839 mbedtls_mpi_free( &conf->dhm_P );
6840 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006841 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006842 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006843
6844 return( 0 );
6845}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006846#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006847
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006848#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6849/*
6850 * Set the minimum length for Diffie-Hellman parameters
6851 */
6852void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6853 unsigned int bitlen )
6854{
6855 conf->dhm_min_bitlen = bitlen;
6856}
6857#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6858
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006859#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006860/*
6861 * Set allowed/preferred hashes for handshake signatures
6862 */
6863void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6864 const int *hashes )
6865{
6866 conf->sig_hashes = hashes;
6867}
Hanno Becker947194e2017-04-07 13:25:49 +01006868#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006869
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006870#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006871/*
6872 * Set the allowed elliptic curves
6873 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006874void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006875 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006877 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006878}
Hanno Becker947194e2017-04-07 13:25:49 +01006879#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006880
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006881#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006883{
Hanno Becker947194e2017-04-07 13:25:49 +01006884 /* Initialize to suppress unnecessary compiler warning */
6885 size_t hostname_len = 0;
6886
6887 /* Check if new hostname is valid before
6888 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006889 if( hostname != NULL )
6890 {
6891 hostname_len = strlen( hostname );
6892
6893 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6894 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6895 }
6896
6897 /* Now it's clear that we will overwrite the old hostname,
6898 * so we can free it safely */
6899
6900 if( ssl->hostname != NULL )
6901 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006902 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006903 mbedtls_free( ssl->hostname );
6904 }
6905
6906 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006907
Paul Bakker5121ce52009-01-03 21:22:43 +00006908 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006909 {
6910 ssl->hostname = NULL;
6911 }
6912 else
6913 {
6914 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006915 if( ssl->hostname == NULL )
6916 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006917
Hanno Becker947194e2017-04-07 13:25:49 +01006918 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006919
Hanno Becker947194e2017-04-07 13:25:49 +01006920 ssl->hostname[hostname_len] = '\0';
6921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006922
6923 return( 0 );
6924}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006925#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006926
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006927#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006928void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006930 const unsigned char *, size_t),
6931 void *p_sni )
6932{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006933 conf->f_sni = f_sni;
6934 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006939int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006940{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006941 size_t cur_len, tot_len;
6942 const char **p;
6943
6944 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006945 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6946 * MUST NOT be truncated."
6947 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006948 */
6949 tot_len = 0;
6950 for( p = protos; *p != NULL; p++ )
6951 {
6952 cur_len = strlen( *p );
6953 tot_len += cur_len;
6954
6955 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006957 }
6958
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006959 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006960
6961 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006962}
6963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006965{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006966 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006969
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006970void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006971{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006972 conf->max_major_ver = major;
6973 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006974}
6975
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006976void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006978 conf->min_major_ver = major;
6979 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006980}
6981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006983void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006984{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006985 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006986}
6987#endif
6988
Janos Follath088ce432017-04-10 12:42:31 +01006989#if defined(MBEDTLS_SSL_SRV_C)
6990void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6991 char cert_req_ca_list )
6992{
6993 conf->cert_req_ca_list = cert_req_ca_list;
6994}
6995#endif
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006998void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006999{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007000 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007001}
7002#endif
7003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007005void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007006{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007007 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007008}
7009#endif
7010
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007011#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007012void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007013{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007014 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007015}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007016#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007019int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007022 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007025 }
7026
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007027 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007028
7029 return( 0 );
7030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007034void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007035{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007036 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007041void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007042{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007043 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007044}
7045#endif
7046
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007047void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007049 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007050}
7051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007053void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007055 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007056}
7057
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007058void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007060 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007061}
7062
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007063void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007064 const unsigned char period[8] )
7065{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007066 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007071#if defined(MBEDTLS_SSL_CLI_C)
7072void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007073{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007074 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007075}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007076#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007077
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007078#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007079void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7080 mbedtls_ssl_ticket_write_t *f_ticket_write,
7081 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7082 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007083{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007084 conf->f_ticket_write = f_ticket_write;
7085 conf->f_ticket_parse = f_ticket_parse;
7086 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007087}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007090
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007091#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7092void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7093 mbedtls_ssl_export_keys_t *f_export_keys,
7094 void *p_export_keys )
7095{
7096 conf->f_export_keys = f_export_keys;
7097 conf->p_export_keys = p_export_keys;
7098}
7099#endif
7100
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007101#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007102void mbedtls_ssl_conf_async_private_cb(
7103 mbedtls_ssl_config *conf,
7104 mbedtls_ssl_async_sign_t *f_async_sign,
7105 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7106 mbedtls_ssl_async_resume_t *f_async_resume,
7107 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007108 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007109{
7110 conf->f_async_sign_start = f_async_sign;
7111 conf->f_async_decrypt_start = f_async_decrypt;
7112 conf->f_async_resume = f_async_resume;
7113 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007114 conf->p_async_config_data = async_config_data;
7115}
7116
Gilles Peskine8f97af72018-04-26 11:46:10 +02007117void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7118{
7119 return( conf->p_async_config_data );
7120}
7121
Gilles Peskine1febfef2018-04-30 11:54:39 +02007122void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007123{
7124 if( ssl->handshake == NULL )
7125 return( NULL );
7126 else
7127 return( ssl->handshake->user_async_ctx );
7128}
7129
Gilles Peskine1febfef2018-04-30 11:54:39 +02007130void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007131 void *ctx )
7132{
7133 if( ssl->handshake != NULL )
7134 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007135}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007136#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007137
Paul Bakker5121ce52009-01-03 21:22:43 +00007138/*
7139 * SSL get accessors
7140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007142{
7143 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7144}
7145
Hanno Becker8b170a02017-10-10 11:51:19 +01007146int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7147{
7148 /*
7149 * Case A: We're currently holding back
7150 * a message for further processing.
7151 */
7152
7153 if( ssl->keep_current_message == 1 )
7154 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007156 return( 1 );
7157 }
7158
7159 /*
7160 * Case B: Further records are pending in the current datagram.
7161 */
7162
7163#if defined(MBEDTLS_SSL_PROTO_DTLS)
7164 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7165 ssl->in_left > ssl->next_record_offset )
7166 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007168 return( 1 );
7169 }
7170#endif /* MBEDTLS_SSL_PROTO_DTLS */
7171
7172 /*
7173 * Case C: A handshake message is being processed.
7174 */
7175
Hanno Becker8b170a02017-10-10 11:51:19 +01007176 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7177 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007179 return( 1 );
7180 }
7181
7182 /*
7183 * Case D: An application data message is being processed
7184 */
7185 if( ssl->in_offt != NULL )
7186 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007188 return( 1 );
7189 }
7190
7191 /*
7192 * In all other cases, the rest of the message can be dropped.
7193 * As in ssl_read_record_layer, this needs to be adapted if
7194 * we implement support for multiple alerts in single records.
7195 */
7196
7197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7198 return( 0 );
7199}
7200
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007201uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007202{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007203 if( ssl->session != NULL )
7204 return( ssl->session->verify_result );
7205
7206 if( ssl->session_negotiate != NULL )
7207 return( ssl->session_negotiate->verify_result );
7208
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007209 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007210}
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007213{
Paul Bakker926c8e42013-03-06 10:23:34 +01007214 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007215 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007218}
7219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007224 {
7225 switch( ssl->minor_ver )
7226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007228 return( "DTLSv1.0" );
7229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007231 return( "DTLSv1.2" );
7232
7233 default:
7234 return( "unknown (DTLS)" );
7235 }
7236 }
7237#endif
7238
Paul Bakker43ca69c2011-01-15 17:35:19 +00007239 switch( ssl->minor_ver )
7240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007242 return( "SSLv3.0" );
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007245 return( "TLSv1.0" );
7246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007248 return( "TLSv1.1" );
7249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007251 return( "TLSv1.2" );
7252
Paul Bakker43ca69c2011-01-15 17:35:19 +00007253 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007254 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007255 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007256}
7257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007259{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007260 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007262 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007263
Hanno Becker78640902018-08-13 16:35:15 +01007264 if( transform == NULL )
7265 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267#if defined(MBEDTLS_ZLIB_SUPPORT)
7268 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7269 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007270#endif
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 case MBEDTLS_MODE_GCM:
7275 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007276 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007278 transform_expansion = transform->minlen;
7279 break;
7280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007282
7283 block_size = mbedtls_cipher_get_block_size(
7284 &transform->cipher_ctx_enc );
7285
7286#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7287 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7288 {
7289 /* Expansion due to addition of
7290 * - MAC
7291 * - CBC padding (theoretically up to 256 bytes, but
7292 * we never use more than block_size)
7293 * - explicit IV
7294 */
7295 transform_expansion = transform->maclen + 2 * block_size;
7296 }
7297 else
7298#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7299 {
7300 /* No explicit IV prior to TLS 1.1. */
7301 transform_expansion = transform->maclen + block_size;
7302 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007303 break;
7304
7305 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007308 }
7309
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007310 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007311}
7312
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007313#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7314size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7315{
7316 size_t max_len;
7317
7318 /*
7319 * Assume mfl_code is correct since it was checked when set
7320 */
Angus Grattond8213d02016-05-25 20:56:48 +10007321 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007322
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007323 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007324 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007325 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007326 {
Angus Grattond8213d02016-05-25 20:56:48 +10007327 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007328 }
7329
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007330 /* During a handshake, use the value being negotiated */
7331 if( ssl->session_negotiate != NULL &&
7332 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7333 {
7334 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7335 }
7336
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007337 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007338}
7339#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7340
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007341#if defined(MBEDTLS_SSL_PROTO_DTLS)
7342static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7343{
7344 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7345 return( ssl->mtu );
7346
7347 if( ssl->mtu == 0 )
7348 return( ssl->handshake->mtu );
7349
7350 return( ssl->mtu < ssl->handshake->mtu ?
7351 ssl->mtu : ssl->handshake->mtu );
7352}
7353#endif /* MBEDTLS_SSL_PROTO_DTLS */
7354
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007355int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7356{
7357 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7358
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007359#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7360 !defined(MBEDTLS_SSL_PROTO_DTLS)
7361 (void) ssl;
7362#endif
7363
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007364#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7365 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7366
7367 if( max_len > mfl )
7368 max_len = mfl;
7369#endif
7370
7371#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007372 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007373 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007374 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007375 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7376 const size_t overhead = (size_t) ret;
7377
7378 if( ret < 0 )
7379 return( ret );
7380
7381 if( mtu <= overhead )
7382 {
7383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7384 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7385 }
7386
7387 if( max_len > mtu - overhead )
7388 max_len = mtu - overhead;
7389 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007390#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007391
Hanno Becker0defedb2018-08-10 12:35:02 +01007392#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7393 !defined(MBEDTLS_SSL_PROTO_DTLS)
7394 ((void) ssl);
7395#endif
7396
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007397 return( (int) max_len );
7398}
7399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400#if defined(MBEDTLS_X509_CRT_PARSE_C)
7401const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007402{
7403 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007404 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007405
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007406 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#if defined(MBEDTLS_SSL_CLI_C)
7411int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007412{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007413 if( ssl == NULL ||
7414 dst == NULL ||
7415 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007416 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007419 }
7420
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007421 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007424
Paul Bakker5121ce52009-01-03 21:22:43 +00007425/*
Paul Bakker1961b702013-01-25 14:49:24 +01007426 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007431
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007432 if( ssl == NULL || ssl->conf == NULL )
7433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007436 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007440 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007442#endif
7443
Paul Bakker1961b702013-01-25 14:49:24 +01007444 return( ret );
7445}
7446
7447/*
7448 * Perform the SSL handshake
7449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007451{
7452 int ret = 0;
7453
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007454 if( ssl == NULL || ssl->conf == NULL )
7455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007462
7463 if( ret != 0 )
7464 break;
7465 }
7466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007468
7469 return( ret );
7470}
7471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472#if defined(MBEDTLS_SSL_RENEGOTIATION)
7473#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007474/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007475 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007478{
7479 int ret;
7480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007482
7483 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7485 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007486
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007487 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007488 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007490 return( ret );
7491 }
7492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007494
7495 return( 0 );
7496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007498
7499/*
7500 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 * - any side: calling mbedtls_ssl_renegotiate(),
7502 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7503 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007504 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007505 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007509{
7510 int ret;
7511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007513
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007514 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7515 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007516
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007517 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7518 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007522 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007523 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007524 ssl->handshake->out_msg_seq = 1;
7525 else
7526 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007527 }
7528#endif
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7531 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007536 return( ret );
7537 }
7538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007540
7541 return( 0 );
7542}
7543
7544/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007545 * Renegotiate current connection on client,
7546 * or request renegotiation on server
7547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007551
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#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007556 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007557 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007563
7564 /* Did we already try/start sending HelloRequest? */
7565 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007567
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007568 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007573 /*
7574 * On client, either start the renegotiation process or,
7575 * if already in progress, continue the handshake
7576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007581
7582 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007585 return( ret );
7586 }
7587 }
7588 else
7589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007593 return( ret );
7594 }
7595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007597
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007598 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007599}
7600
7601/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007602 * Check record counters and renegotiate if they're above the limit.
7603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007605{
Andres AG2196c7f2016-12-15 17:01:16 +00007606 size_t ep_len = ssl_ep_len( ssl );
7607 int in_ctr_cmp;
7608 int out_ctr_cmp;
7609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7611 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007612 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007613 {
7614 return( 0 );
7615 }
7616
Andres AG2196c7f2016-12-15 17:01:16 +00007617 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7618 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007619 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007620 ssl->conf->renego_period + ep_len, 8 - ep_len );
7621
7622 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007623 {
7624 return( 0 );
7625 }
7626
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007631
7632/*
7633 * Receive application data decrypted from the SSL layer
7634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007636{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007637 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007638 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007639
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007640 if( ssl == NULL || ssl->conf == NULL )
7641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007646 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007649 return( ret );
7650
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007651 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007653 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007654 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007655 return( ret );
7656 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007657 }
7658#endif
7659
Hanno Becker4a810fb2017-05-24 16:27:30 +01007660 /*
7661 * Check if renegotiation is necessary and/or handshake is
7662 * in process. If yes, perform/continue, and fall through
7663 * if an unexpected packet is received while the client
7664 * is waiting for the ServerHello.
7665 *
7666 * (There is no equivalent to the last condition on
7667 * the server-side as it is not treated as within
7668 * a handshake while waiting for the ClientHello
7669 * after a renegotiation request.)
7670 */
7671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007673 ret = ssl_check_ctr_renegotiate( ssl );
7674 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7675 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007678 return( ret );
7679 }
7680#endif
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007685 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7686 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007689 return( ret );
7690 }
7691 }
7692
Hanno Beckere41158b2017-10-23 13:30:32 +01007693 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007694 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007695 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007696 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007697 if( ssl->f_get_timer != NULL &&
7698 ssl->f_get_timer( ssl->p_timer ) == -1 )
7699 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007700 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007701 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007702
Hanno Becker4a810fb2017-05-24 16:27:30 +01007703 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007704 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007705 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7706 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007707
Hanno Becker4a810fb2017-05-24 16:27:30 +01007708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7709 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007710 }
7711
7712 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007714 {
7715 /*
7716 * OpenSSL sends empty messages to randomize the IV
7717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007721 return( 0 );
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 return( ret );
7725 }
7726 }
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007731
Hanno Becker4a810fb2017-05-24 16:27:30 +01007732 /*
7733 * - For client-side, expect SERVER_HELLO_REQUEST.
7734 * - For server-side, expect CLIENT_HELLO.
7735 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7736 */
7737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007741 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007744
7745 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007747 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007748 {
7749 continue;
7750 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007751#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007753 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007754#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007755
Hanno Becker4a810fb2017-05-24 16:27:30 +01007756#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007757 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007761
7762 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007764 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007765 {
7766 continue;
7767 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007770 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007771#endif /* MBEDTLS_SSL_SRV_C */
7772
Hanno Becker21df7f92017-10-17 11:03:26 +01007773#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007774 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007775 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7776 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7777 ssl->conf->allow_legacy_renegotiation ==
7778 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7779 {
7780 /*
7781 * Accept renegotiation request
7782 */
Paul Bakker48916f92012-09-16 19:57:18 +00007783
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007784 /* DTLS clients need to know renego is server-initiated */
7785#if defined(MBEDTLS_SSL_PROTO_DTLS)
7786 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7787 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7788 {
7789 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7790 }
7791#endif
7792 ret = ssl_start_renegotiation( ssl );
7793 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7794 ret != 0 )
7795 {
7796 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7797 return( ret );
7798 }
7799 }
7800 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007801#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007802 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007803 /*
7804 * Refuse renegotiation
7805 */
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809#if defined(MBEDTLS_SSL_PROTO_SSL3)
7810 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007811 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007812 /* SSLv3 does not have a "no_renegotiation" warning, so
7813 we send a fatal alert and abort the connection. */
7814 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7815 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007817 }
7818 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7820#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7821 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7822 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7825 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7826 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007827 {
7828 return( ret );
7829 }
Paul Bakker48916f92012-09-16 19:57:18 +00007830 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007831 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7833 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007837 }
Paul Bakker48916f92012-09-16 19:57:18 +00007838 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007839
Hanno Becker90333da2017-10-10 11:27:13 +01007840 /* At this point, we don't know whether the renegotiation has been
7841 * completed or not. The cases to consider are the following:
7842 * 1) The renegotiation is complete. In this case, no new record
7843 * has been read yet.
7844 * 2) The renegotiation is incomplete because the client received
7845 * an application data record while awaiting the ServerHello.
7846 * 3) The renegotiation is incomplete because the client received
7847 * a non-handshake, non-application data message while awaiting
7848 * the ServerHello.
7849 * In each of these case, looping will be the proper action:
7850 * - For 1), the next iteration will read a new record and check
7851 * if it's application data.
7852 * - For 2), the loop condition isn't satisfied as application data
7853 * is present, hence continue is the same as break
7854 * - For 3), the loop condition is satisfied and read_record
7855 * will re-deliver the message that was held back by the client
7856 * when expecting the ServerHello.
7857 */
7858 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007859 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007860#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007862 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007863 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007864 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007865 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007868 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007870 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007871 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007872 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7876 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007879 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007880 }
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7885 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 }
7887
7888 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007889
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007890 /* We're going to return something now, cancel timer,
7891 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007893 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007894
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007896 /* If we requested renego but received AppData, resend HelloRequest.
7897 * Do it now, after setting in_offt, to avoid taking this branch
7898 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007900 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007902 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007903 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007906 return( ret );
7907 }
7908 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007910#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007911 }
7912
7913 n = ( len < ssl->in_msglen )
7914 ? len : ssl->in_msglen;
7915
7916 memcpy( buf, ssl->in_offt, n );
7917 ssl->in_msglen -= n;
7918
7919 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007920 {
7921 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007922 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007923 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007924 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007925 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007926 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007927 /* more data available */
7928 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007932
Paul Bakker23986e52011-04-24 08:57:21 +00007933 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007934}
7935
7936/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007937 * Send application data to be encrypted by the SSL layer, taking care of max
7938 * fragment length and buffer size.
7939 *
7940 * According to RFC 5246 Section 6.2.1:
7941 *
7942 * Zero-length fragments of Application data MAY be sent as they are
7943 * potentially useful as a traffic analysis countermeasure.
7944 *
7945 * Therefore, it is possible that the input message length is 0 and the
7946 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007947 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007948static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007949 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007950{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007951 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7952 const size_t max_len = (size_t) ret;
7953
7954 if( ret < 0 )
7955 {
7956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7957 return( ret );
7958 }
7959
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007960 if( len > max_len )
7961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007966 "maximum fragment length: %d > %d",
7967 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007969 }
7970 else
7971#endif
7972 len = max_len;
7973 }
Paul Bakker887bd502011-06-08 13:10:54 +00007974
Paul Bakker5121ce52009-01-03 21:22:43 +00007975 if( ssl->out_left != 0 )
7976 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007977 /*
7978 * The user has previously tried to send the data and
7979 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7980 * written. In this case, we expect the high-level write function
7981 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007986 return( ret );
7987 }
7988 }
Paul Bakker887bd502011-06-08 13:10:54 +00007989 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007990 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007991 /*
7992 * The user is trying to send a message the first time, so we need to
7993 * copy the data into the internal buffers and setup the data structure
7994 * to keep track of partial writes
7995 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007996 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007998 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007999
Hanno Becker67bc7c32018-08-06 11:33:50 +01008000 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008003 return( ret );
8004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008005 }
8006
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008007 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008008}
8009
8010/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008011 * Write application data, doing 1/n-1 splitting if necessary.
8012 *
8013 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008014 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008015 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008018static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008019 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008020{
8021 int ret;
8022
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008023 if( ssl->conf->cbc_record_splitting ==
8024 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008025 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8027 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8028 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008029 {
8030 return( ssl_write_real( ssl, buf, len ) );
8031 }
8032
8033 if( ssl->split_done == 0 )
8034 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008035 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008036 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008037 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008038 }
8039
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008040 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8041 return( ret );
8042 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008043
8044 return( ret + 1 );
8045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008047
8048/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008049 * Write application data (public-facing wrapper)
8050 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008051int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008052{
8053 int ret;
8054
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008056
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008057 if( ssl == NULL || ssl->conf == NULL )
8058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8059
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008060#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008061 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8062 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008064 return( ret );
8065 }
8066#endif
8067
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008068 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008069 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008070 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008071 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008073 return( ret );
8074 }
8075 }
8076
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008077#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008078 ret = ssl_write_split( ssl, buf, len );
8079#else
8080 ret = ssl_write_real( ssl, buf, len );
8081#endif
8082
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008084
8085 return( ret );
8086}
8087
8088/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008089 * Notify the peer that the connection is being closed
8090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008092{
8093 int ret;
8094
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008095 if( ssl == NULL || ssl->conf == NULL )
8096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008099
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008100 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8106 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8107 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008110 return( ret );
8111 }
8112 }
8113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008115
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008116 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008117}
8118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008120{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008121 if( transform == NULL )
8122 return;
8123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008125 deflateEnd( &transform->ctx_deflate );
8126 inflateEnd( &transform->ctx_inflate );
8127#endif
8128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8130 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 mbedtls_md_free( &transform->md_ctx_enc );
8133 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008134
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008135 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008136}
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#if defined(MBEDTLS_X509_CRT_PARSE_C)
8139static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008142
8143 while( cur != NULL )
8144 {
8145 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008147 cur = next;
8148 }
8149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008151
Gilles Peskine9b562d52018-04-25 20:32:43 +02008152void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008153{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008154 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8155
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008156 if( handshake == NULL )
8157 return;
8158
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008159#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8160 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8161 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008162 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008163 handshake->async_in_progress = 0;
8164 }
8165#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8166
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008167#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8168 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8169 mbedtls_md5_free( &handshake->fin_md5 );
8170 mbedtls_sha1_free( &handshake->fin_sha1 );
8171#endif
8172#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8173#if defined(MBEDTLS_SHA256_C)
8174 mbedtls_sha256_free( &handshake->fin_sha256 );
8175#endif
8176#if defined(MBEDTLS_SHA512_C)
8177 mbedtls_sha512_free( &handshake->fin_sha512 );
8178#endif
8179#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_DHM_C)
8182 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184#if defined(MBEDTLS_ECDH_C)
8185 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008186#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008187#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008188 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008189#if defined(MBEDTLS_SSL_CLI_C)
8190 mbedtls_free( handshake->ecjpake_cache );
8191 handshake->ecjpake_cache = NULL;
8192 handshake->ecjpake_cache_len = 0;
8193#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008194#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008195
Janos Follath4ae5c292016-02-10 11:27:43 +00008196#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8197 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008198 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008200#endif
8201
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008202#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8203 if( handshake->psk != NULL )
8204 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008205 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008206 mbedtls_free( handshake->psk );
8207 }
8208#endif
8209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8211 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008212 /*
8213 * Free only the linked list wrapper, not the keys themselves
8214 * since the belong to the SNI callback
8215 */
8216 if( handshake->sni_key_cert != NULL )
8217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008219
8220 while( cur != NULL )
8221 {
8222 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008224 cur = next;
8225 }
8226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229#if defined(MBEDTLS_SSL_PROTO_DTLS)
8230 mbedtls_free( handshake->verify_cookie );
8231 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008232 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008233#endif
8234
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008235 mbedtls_platform_zeroize( handshake,
8236 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008237}
8238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008240{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008241 if( session == NULL )
8242 return;
8243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008245 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247 mbedtls_x509_crt_free( session->peer_cert );
8248 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008249 }
Paul Bakkered27a042013-04-18 22:46:23 +02008250#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008251
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008252#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008254#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008255
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008256 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008257}
8258
Paul Bakker5121ce52009-01-03 21:22:43 +00008259/*
8260 * Free an SSL context
8261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008263{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008264 if( ssl == NULL )
8265 return;
8266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008268
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008269 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008270 {
Angus Grattond8213d02016-05-25 20:56:48 +10008271 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008273 }
8274
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008275 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008276 {
Angus Grattond8213d02016-05-25 20:56:48 +10008277 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008279 }
8280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008282 if( ssl->compress_buf != NULL )
8283 {
Angus Grattond8213d02016-05-25 20:56:48 +10008284 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008286 }
8287#endif
8288
Paul Bakker48916f92012-09-16 19:57:18 +00008289 if( ssl->transform )
8290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291 mbedtls_ssl_transform_free( ssl->transform );
8292 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008293 }
8294
8295 if( ssl->handshake )
8296 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008297 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8299 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 mbedtls_free( ssl->handshake );
8302 mbedtls_free( ssl->transform_negotiate );
8303 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008304 }
8305
Paul Bakkerc0463502013-02-14 11:19:38 +01008306 if( ssl->session )
8307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308 mbedtls_ssl_session_free( ssl->session );
8309 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008310 }
8311
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008312#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008313 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008314 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008315 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008317 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008318#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8321 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8324 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008325 }
8326#endif
8327
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008328#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008330#endif
8331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008333
Paul Bakker86f04f42013-02-14 11:20:09 +01008334 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008335 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008336}
8337
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008338/*
8339 * Initialze mbedtls_ssl_config
8340 */
8341void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8342{
8343 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8344}
8345
Simon Butcherc97b6972015-12-27 23:48:17 +00008346#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008347static int ssl_preset_default_hashes[] = {
8348#if defined(MBEDTLS_SHA512_C)
8349 MBEDTLS_MD_SHA512,
8350 MBEDTLS_MD_SHA384,
8351#endif
8352#if defined(MBEDTLS_SHA256_C)
8353 MBEDTLS_MD_SHA256,
8354 MBEDTLS_MD_SHA224,
8355#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008356#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008357 MBEDTLS_MD_SHA1,
8358#endif
8359 MBEDTLS_MD_NONE
8360};
Simon Butcherc97b6972015-12-27 23:48:17 +00008361#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008362
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008363static int ssl_preset_suiteb_ciphersuites[] = {
8364 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8365 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8366 0
8367};
8368
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008369#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008370static int ssl_preset_suiteb_hashes[] = {
8371 MBEDTLS_MD_SHA256,
8372 MBEDTLS_MD_SHA384,
8373 MBEDTLS_MD_NONE
8374};
8375#endif
8376
8377#if defined(MBEDTLS_ECP_C)
8378static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8379 MBEDTLS_ECP_DP_SECP256R1,
8380 MBEDTLS_ECP_DP_SECP384R1,
8381 MBEDTLS_ECP_DP_NONE
8382};
8383#endif
8384
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008385/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008386 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008387 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008388int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008389 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008390{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008391#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008392 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008393#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008394
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008395 /* Use the functions here so that they are covered in tests,
8396 * but otherwise access member directly for efficiency */
8397 mbedtls_ssl_conf_endpoint( conf, endpoint );
8398 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008399
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008400 /*
8401 * Things that are common to all presets
8402 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008403#if defined(MBEDTLS_SSL_CLI_C)
8404 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8405 {
8406 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8407#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8408 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8409#endif
8410 }
8411#endif
8412
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008413#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008414 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008415#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008416
8417#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8418 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8419#endif
8420
8421#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8422 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8423#endif
8424
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008425#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8426 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8427#endif
8428
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008429#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008430 conf->f_cookie_write = ssl_cookie_write_dummy;
8431 conf->f_cookie_check = ssl_cookie_check_dummy;
8432#endif
8433
8434#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8435 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8436#endif
8437
Janos Follath088ce432017-04-10 12:42:31 +01008438#if defined(MBEDTLS_SSL_SRV_C)
8439 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8440#endif
8441
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008442#if defined(MBEDTLS_SSL_PROTO_DTLS)
8443 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8444 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8445#endif
8446
8447#if defined(MBEDTLS_SSL_RENEGOTIATION)
8448 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008449 memset( conf->renego_period, 0x00, 2 );
8450 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008451#endif
8452
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008453#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8454 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8455 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008456 const unsigned char dhm_p[] =
8457 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8458 const unsigned char dhm_g[] =
8459 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8460
Hanno Beckera90658f2017-10-04 15:29:08 +01008461 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8462 dhm_p, sizeof( dhm_p ),
8463 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008464 {
8465 return( ret );
8466 }
8467 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008468#endif
8469
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008470 /*
8471 * Preset-specific defaults
8472 */
8473 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008474 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008475 /*
8476 * NSA Suite B
8477 */
8478 case MBEDTLS_SSL_PRESET_SUITEB:
8479 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8480 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8481 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8482 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8483
8484 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8485 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8486 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8487 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8488 ssl_preset_suiteb_ciphersuites;
8489
8490#if defined(MBEDTLS_X509_CRT_PARSE_C)
8491 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008492#endif
8493
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008494#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008495 conf->sig_hashes = ssl_preset_suiteb_hashes;
8496#endif
8497
8498#if defined(MBEDTLS_ECP_C)
8499 conf->curve_list = ssl_preset_suiteb_curves;
8500#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008501 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008502
8503 /*
8504 * Default
8505 */
8506 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008507 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8508 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8509 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8510 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8511 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8512 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8513 MBEDTLS_SSL_MIN_MINOR_VERSION :
8514 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008515 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8516 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8517
8518#if defined(MBEDTLS_SSL_PROTO_DTLS)
8519 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8520 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8521#endif
8522
8523 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8524 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8525 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8526 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8527 mbedtls_ssl_list_ciphersuites();
8528
8529#if defined(MBEDTLS_X509_CRT_PARSE_C)
8530 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8531#endif
8532
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008533#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008534 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008535#endif
8536
8537#if defined(MBEDTLS_ECP_C)
8538 conf->curve_list = mbedtls_ecp_grp_id_list();
8539#endif
8540
8541#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8542 conf->dhm_min_bitlen = 1024;
8543#endif
8544 }
8545
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008546 return( 0 );
8547}
8548
8549/*
8550 * Free mbedtls_ssl_config
8551 */
8552void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8553{
8554#if defined(MBEDTLS_DHM_C)
8555 mbedtls_mpi_free( &conf->dhm_P );
8556 mbedtls_mpi_free( &conf->dhm_G );
8557#endif
8558
8559#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8560 if( conf->psk != NULL )
8561 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008562 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008563 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008564 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008565 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008566 }
8567
8568 if( conf->psk_identity != NULL )
8569 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008570 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008571 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008572 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008573 conf->psk_identity_len = 0;
8574 }
8575#endif
8576
8577#if defined(MBEDTLS_X509_CRT_PARSE_C)
8578 ssl_key_cert_free( conf->key_cert );
8579#endif
8580
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008581 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008582}
8583
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008584#if defined(MBEDTLS_PK_C) && \
8585 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008586/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591#if defined(MBEDTLS_RSA_C)
8592 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8593 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#if defined(MBEDTLS_ECDSA_C)
8596 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8597 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008600}
8601
Hanno Becker7e5437a2017-04-28 17:15:26 +01008602unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8603{
8604 switch( type ) {
8605 case MBEDTLS_PK_RSA:
8606 return( MBEDTLS_SSL_SIG_RSA );
8607 case MBEDTLS_PK_ECDSA:
8608 case MBEDTLS_PK_ECKEY:
8609 return( MBEDTLS_SSL_SIG_ECDSA );
8610 default:
8611 return( MBEDTLS_SSL_SIG_ANON );
8612 }
8613}
8614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008616{
8617 switch( sig )
8618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_RSA_C)
8620 case MBEDTLS_SSL_SIG_RSA:
8621 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008622#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623#if defined(MBEDTLS_ECDSA_C)
8624 case MBEDTLS_SSL_SIG_ECDSA:
8625 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008626#endif
8627 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008629 }
8630}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008631#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008632
Hanno Becker7e5437a2017-04-28 17:15:26 +01008633#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8634 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8635
8636/* Find an entry in a signature-hash set matching a given hash algorithm. */
8637mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8638 mbedtls_pk_type_t sig_alg )
8639{
8640 switch( sig_alg )
8641 {
8642 case MBEDTLS_PK_RSA:
8643 return( set->rsa );
8644 case MBEDTLS_PK_ECDSA:
8645 return( set->ecdsa );
8646 default:
8647 return( MBEDTLS_MD_NONE );
8648 }
8649}
8650
8651/* Add a signature-hash-pair to a signature-hash set */
8652void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8653 mbedtls_pk_type_t sig_alg,
8654 mbedtls_md_type_t md_alg )
8655{
8656 switch( sig_alg )
8657 {
8658 case MBEDTLS_PK_RSA:
8659 if( set->rsa == MBEDTLS_MD_NONE )
8660 set->rsa = md_alg;
8661 break;
8662
8663 case MBEDTLS_PK_ECDSA:
8664 if( set->ecdsa == MBEDTLS_MD_NONE )
8665 set->ecdsa = md_alg;
8666 break;
8667
8668 default:
8669 break;
8670 }
8671}
8672
8673/* Allow exactly one hash algorithm for each signature. */
8674void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8675 mbedtls_md_type_t md_alg )
8676{
8677 set->rsa = md_alg;
8678 set->ecdsa = md_alg;
8679}
8680
8681#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8682 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8683
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008684/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008685 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008688{
8689 switch( hash )
8690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691#if defined(MBEDTLS_MD5_C)
8692 case MBEDTLS_SSL_HASH_MD5:
8693 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695#if defined(MBEDTLS_SHA1_C)
8696 case MBEDTLS_SSL_HASH_SHA1:
8697 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699#if defined(MBEDTLS_SHA256_C)
8700 case MBEDTLS_SSL_HASH_SHA224:
8701 return( MBEDTLS_MD_SHA224 );
8702 case MBEDTLS_SSL_HASH_SHA256:
8703 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705#if defined(MBEDTLS_SHA512_C)
8706 case MBEDTLS_SSL_HASH_SHA384:
8707 return( MBEDTLS_MD_SHA384 );
8708 case MBEDTLS_SSL_HASH_SHA512:
8709 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008710#endif
8711 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008713 }
8714}
8715
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008716/*
8717 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8718 */
8719unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8720{
8721 switch( md )
8722 {
8723#if defined(MBEDTLS_MD5_C)
8724 case MBEDTLS_MD_MD5:
8725 return( MBEDTLS_SSL_HASH_MD5 );
8726#endif
8727#if defined(MBEDTLS_SHA1_C)
8728 case MBEDTLS_MD_SHA1:
8729 return( MBEDTLS_SSL_HASH_SHA1 );
8730#endif
8731#if defined(MBEDTLS_SHA256_C)
8732 case MBEDTLS_MD_SHA224:
8733 return( MBEDTLS_SSL_HASH_SHA224 );
8734 case MBEDTLS_MD_SHA256:
8735 return( MBEDTLS_SSL_HASH_SHA256 );
8736#endif
8737#if defined(MBEDTLS_SHA512_C)
8738 case MBEDTLS_MD_SHA384:
8739 return( MBEDTLS_SSL_HASH_SHA384 );
8740 case MBEDTLS_MD_SHA512:
8741 return( MBEDTLS_SSL_HASH_SHA512 );
8742#endif
8743 default:
8744 return( MBEDTLS_SSL_HASH_NONE );
8745 }
8746}
8747
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008748#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008749/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008750 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008751 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008752 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008753int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008754{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008756
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008757 if( ssl->conf->curve_list == NULL )
8758 return( -1 );
8759
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008760 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008761 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008762 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008763
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008764 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008765}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008766#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008767
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008768#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008769/*
8770 * Check if a hash proposed by the peer is in our list.
8771 * Return 0 if we're willing to use it, -1 otherwise.
8772 */
8773int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8774 mbedtls_md_type_t md )
8775{
8776 const int *cur;
8777
8778 if( ssl->conf->sig_hashes == NULL )
8779 return( -1 );
8780
8781 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8782 if( *cur == (int) md )
8783 return( 0 );
8784
8785 return( -1 );
8786}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008787#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008789#if defined(MBEDTLS_X509_CRT_PARSE_C)
8790int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8791 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008792 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008793 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008794{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008795 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008797 int usage = 0;
8798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008800 const char *ext_oid;
8801 size_t ext_len;
8802#endif
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8805 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008806 ((void) cert);
8807 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008808 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008809#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8812 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008813 {
8814 /* Server part of the key exchange */
8815 switch( ciphersuite->key_exchange )
8816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 case MBEDTLS_KEY_EXCHANGE_RSA:
8818 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008819 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008820 break;
8821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8823 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8824 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8825 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008826 break;
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8829 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008830 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008831 break;
8832
8833 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 case MBEDTLS_KEY_EXCHANGE_NONE:
8835 case MBEDTLS_KEY_EXCHANGE_PSK:
8836 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8837 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008838 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008839 usage = 0;
8840 }
8841 }
8842 else
8843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8845 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008846 }
8847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008849 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008850 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008851 ret = -1;
8852 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008853#else
8854 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8858 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8861 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008862 }
8863 else
8864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8866 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008867 }
8868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008870 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008871 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008872 ret = -1;
8873 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008875
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008876 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008879
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008880/*
8881 * Convert version numbers to/from wire format
8882 * and, for DTLS, to/from TLS equivalent.
8883 *
8884 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008885 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008886 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8887 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008890 unsigned char ver[2] )
8891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#if defined(MBEDTLS_SSL_PROTO_DTLS)
8893 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008896 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8897
8898 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8899 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8900 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008901 else
8902#else
8903 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008904#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008905 {
8906 ver[0] = (unsigned char) major;
8907 ver[1] = (unsigned char) minor;
8908 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008909}
8910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008912 const unsigned char ver[2] )
8913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008914#if defined(MBEDTLS_SSL_PROTO_DTLS)
8915 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008916 {
8917 *major = 255 - ver[0] + 2;
8918 *minor = 255 - ver[1] + 1;
8919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008921 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8922 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008923 else
8924#else
8925 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008926#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008927 {
8928 *major = ver[0];
8929 *minor = ver[1];
8930 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008931}
8932
Simon Butcher99000142016-10-13 17:21:01 +01008933int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8934{
8935#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8936 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8937 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8938
8939 switch( md )
8940 {
8941#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8942#if defined(MBEDTLS_MD5_C)
8943 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008944 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008945#endif
8946#if defined(MBEDTLS_SHA1_C)
8947 case MBEDTLS_SSL_HASH_SHA1:
8948 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8949 break;
8950#endif
8951#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8952#if defined(MBEDTLS_SHA512_C)
8953 case MBEDTLS_SSL_HASH_SHA384:
8954 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8955 break;
8956#endif
8957#if defined(MBEDTLS_SHA256_C)
8958 case MBEDTLS_SSL_HASH_SHA256:
8959 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8960 break;
8961#endif
8962 default:
8963 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8964 }
8965
8966 return 0;
8967#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8968 (void) ssl;
8969 (void) md;
8970
8971 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8973}
8974
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008975#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8976 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8977int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8978 unsigned char *output,
8979 unsigned char *data, size_t data_len )
8980{
8981 int ret = 0;
8982 mbedtls_md5_context mbedtls_md5;
8983 mbedtls_sha1_context mbedtls_sha1;
8984
8985 mbedtls_md5_init( &mbedtls_md5 );
8986 mbedtls_sha1_init( &mbedtls_sha1 );
8987
8988 /*
8989 * digitally-signed struct {
8990 * opaque md5_hash[16];
8991 * opaque sha_hash[20];
8992 * };
8993 *
8994 * md5_hash
8995 * MD5(ClientHello.random + ServerHello.random
8996 * + ServerParams);
8997 * sha_hash
8998 * SHA(ClientHello.random + ServerHello.random
8999 * + ServerParams);
9000 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009001 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009002 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009004 goto exit;
9005 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009006 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009007 ssl->handshake->randbytes, 64 ) ) != 0 )
9008 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009010 goto exit;
9011 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009012 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009013 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009015 goto exit;
9016 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009017 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009018 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009020 goto exit;
9021 }
9022
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009023 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009024 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009026 goto exit;
9027 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009028 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009029 ssl->handshake->randbytes, 64 ) ) != 0 )
9030 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009032 goto exit;
9033 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009034 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009035 data_len ) ) != 0 )
9036 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009038 goto exit;
9039 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009040 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009041 output + 16 ) ) != 0 )
9042 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009044 goto exit;
9045 }
9046
9047exit:
9048 mbedtls_md5_free( &mbedtls_md5 );
9049 mbedtls_sha1_free( &mbedtls_sha1 );
9050
9051 if( ret != 0 )
9052 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9053 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9054
9055 return( ret );
9056
9057}
9058#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9059 MBEDTLS_SSL_PROTO_TLS1_1 */
9060
9061#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9062 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9063int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009064 unsigned char *hash, size_t *hashlen,
9065 unsigned char *data, size_t data_len,
9066 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009067{
9068 int ret = 0;
9069 mbedtls_md_context_t ctx;
9070 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009071 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009072
9073 mbedtls_md_init( &ctx );
9074
9075 /*
9076 * digitally-signed struct {
9077 * opaque client_random[32];
9078 * opaque server_random[32];
9079 * ServerDHParams params;
9080 * };
9081 */
9082 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9083 {
9084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9085 goto exit;
9086 }
9087 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9088 {
9089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9090 goto exit;
9091 }
9092 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9093 {
9094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9095 goto exit;
9096 }
9097 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9098 {
9099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9100 goto exit;
9101 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009102 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009103 {
9104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9105 goto exit;
9106 }
9107
9108exit:
9109 mbedtls_md_free( &ctx );
9110
9111 if( ret != 0 )
9112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9113 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9114
9115 return( ret );
9116}
9117#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9118 MBEDTLS_SSL_PROTO_TLS1_2 */
9119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#endif /* MBEDTLS_SSL_TLS_C */