blob: 8cf7aa1ce5eb5db5c4f2702d31917e4ece6278f0 [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 Becker2b1e3542018-08-06 11:19:13 +0100112static uint16_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
113{
Hanno Beckera67dee22018-08-22 10:05:20 +0100114 uint16_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 )
117 return( (int) mtu );
118
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{
124 size_t const bytes_written = ssl->out_left;
125 uint16_t const mtu = ssl_get_maximum_datagram_size( ssl );
126
127 /* Double-check that the write-index hasn't gone
128 * past what we can transmit in a single datagram. */
129 if( bytes_written > (size_t) mtu )
130 {
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 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003149 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3150 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3151 {
3152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3154 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003156 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3157 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3158 ssl->handshake == NULL )
3159 {
3160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3162 }
3163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003166 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003168 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3170 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003171 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003172#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003173
Hanno Beckerb50a2532018-08-06 11:52:54 +01003174 /* Double-check that we did not exceed the bounds
3175 * of the outgoing record buffer.
3176 * This should never fail as the various message
3177 * writing functions must obey the bounds of the
3178 * outgoing record buffer, but better be safe.
3179 *
3180 * Note: We deliberately do not check for the MTU or MFL here.
3181 */
3182 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3183 {
3184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3185 "size %u, maximum %u",
3186 (unsigned) ssl->out_msglen,
3187 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3189 }
3190
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003191 /*
3192 * Fill handshake headers
3193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003196 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3197 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3198 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003200 /*
3201 * DTLS has additional fields in the Handshake layer,
3202 * between the length field and the actual payload:
3203 * uint16 message_seq;
3204 * uint24 fragment_offset;
3205 * uint24 fragment_length;
3206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003208 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003209 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003210 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003211 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003212 {
3213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3214 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003215 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003216 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3218 }
3219
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003220 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003221 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003222
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003223 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003224 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003225 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003226 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3227 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3228 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003229 }
3230 else
3231 {
3232 ssl->out_msg[4] = 0;
3233 ssl->out_msg[5] = 0;
3234 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003235
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003236 /* Handshake hashes are computed without fragmentation,
3237 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003238 memset( ssl->out_msg + 6, 0x00, 3 );
3239 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003240 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003242
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003243 /* Update running hashes of hanshake messages seen */
3244 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3245 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 }
3247
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003248 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003251 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252 {
3253 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003256 return( ret );
3257 }
3258 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003259 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003260#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003261 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003262 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003263 {
3264 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3265 return( ret );
3266 }
3267 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003268
3269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3270
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003271 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003272}
3273
3274/*
3275 * Record layer functions
3276 */
3277
3278/*
3279 * Write current record.
3280 *
3281 * Uses:
3282 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3283 * - ssl->out_msglen: length of the record content (excl headers)
3284 * - ssl->out_msg: record content
3285 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003286int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003287{
3288 int ret, done = 0;
3289 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003290 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003291
3292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003295 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003297 {
3298 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003301 return( ret );
3302 }
3303
3304 len = ssl->out_msglen;
3305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3309 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 ret = mbedtls_ssl_hw_record_write( ssl );
3314 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3317 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003318 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003319
3320 if( ret == 0 )
3321 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003324 if( !done )
3325 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003326 unsigned i;
3327 size_t protected_record_size;
3328
Paul Bakker05ef8352012-05-08 09:17:57 +00003329 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003331 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003332
Hanno Becker19859472018-08-06 09:40:20 +01003333 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003334 ssl->out_len[0] = (unsigned char)( len >> 8 );
3335 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003336
Paul Bakker48916f92012-09-16 19:57:18 +00003337 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003338 {
3339 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003342 return( ret );
3343 }
3344
3345 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003346 ssl->out_len[0] = (unsigned char)( len >> 8 );
3347 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003348 }
3349
Hanno Becker2b1e3542018-08-06 11:19:13 +01003350 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3351
3352#if defined(MBEDTLS_SSL_PROTO_DTLS)
3353 /* In case of DTLS, double-check that we don't exceed
3354 * the remaining space in the datagram. */
3355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3356 {
3357 ret = ssl_get_maximum_datagram_size( ssl );
3358 if( ret < 0 )
3359 return( ret );
3360
3361 if( protected_record_size > (size_t) ret )
3362 {
3363 /* Should never happen */
3364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3365 }
3366 }
3367#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003370 "version = [%d:%d], msglen = %d",
3371 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3372
Paul Bakker05ef8352012-05-08 09:17:57 +00003373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003375 ssl->out_hdr, protected_record_size );
3376
3377 ssl->out_left += protected_record_size;
3378 ssl->out_hdr += protected_record_size;
3379 ssl_update_out_pointers( ssl, ssl->transform_out );
3380
Hanno Becker04484622018-08-06 09:49:38 +01003381 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3382 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3383 break;
3384
3385 /* The loop goes to its end iff the counter is wrapping */
3386 if( i == ssl_ep_len( ssl ) )
3387 {
3388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3389 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3390 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003391 }
3392
Hanno Becker67bc7c32018-08-06 11:33:50 +01003393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003394 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3395 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003396 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003397 size_t remaining;
3398 ret = ssl_get_remaining_payload_in_datagram( ssl );
3399 if( ret < 0 )
3400 {
3401 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3402 ret );
3403 return( ret );
3404 }
3405
3406 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003407 if( remaining == 0 )
3408 flush = SSL_FORCE_FLUSH;
3409 else
3410 {
Hanno Becker513815a2018-08-20 11:56:09 +01003411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003412 }
3413 }
3414#endif /* MBEDTLS_SSL_PROTO_DTLS */
3415
3416 if( ( flush == SSL_FORCE_FLUSH ) &&
3417 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 return( ret );
3421 }
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003424
3425 return( 0 );
3426}
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003429/*
3430 * Mark bits in bitmask (used for DTLS HS reassembly)
3431 */
3432static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3433{
3434 unsigned int start_bits, end_bits;
3435
3436 start_bits = 8 - ( offset % 8 );
3437 if( start_bits != 8 )
3438 {
3439 size_t first_byte_idx = offset / 8;
3440
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003441 /* Special case */
3442 if( len <= start_bits )
3443 {
3444 for( ; len != 0; len-- )
3445 mask[first_byte_idx] |= 1 << ( start_bits - len );
3446
3447 /* Avoid potential issues with offset or len becoming invalid */
3448 return;
3449 }
3450
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003451 offset += start_bits; /* Now offset % 8 == 0 */
3452 len -= start_bits;
3453
3454 for( ; start_bits != 0; start_bits-- )
3455 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3456 }
3457
3458 end_bits = len % 8;
3459 if( end_bits != 0 )
3460 {
3461 size_t last_byte_idx = ( offset + len ) / 8;
3462
3463 len -= end_bits; /* Now len % 8 == 0 */
3464
3465 for( ; end_bits != 0; end_bits-- )
3466 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3467 }
3468
3469 memset( mask + offset / 8, 0xFF, len / 8 );
3470}
3471
3472/*
3473 * Check that bitmask is full
3474 */
3475static int ssl_bitmask_check( unsigned char *mask, size_t len )
3476{
3477 size_t i;
3478
3479 for( i = 0; i < len / 8; i++ )
3480 if( mask[i] != 0xFF )
3481 return( -1 );
3482
3483 for( i = 0; i < len % 8; i++ )
3484 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3485 return( -1 );
3486
3487 return( 0 );
3488}
3489
3490/*
3491 * Reassemble fragmented DTLS handshake messages.
3492 *
3493 * Use a temporary buffer for reassembly, divided in two parts:
3494 * - the first holds the reassembled message (including handshake header),
3495 * - the second holds a bitmask indicating which parts of the message
3496 * (excluding headers) have been received so far.
3497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003499{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003500 unsigned char *msg, *bitmask;
3501 size_t frag_len, frag_off;
3502 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3503
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003504 if( ssl->handshake == NULL )
3505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3507 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003508 }
3509
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003510 /*
3511 * For first fragment, check size and allocate buffer
3512 */
3513 if( ssl->handshake->hs_msg == NULL )
3514 {
3515 size_t alloc_len;
3516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003518 msg_len ) );
3519
Angus Grattond8213d02016-05-25 20:56:48 +10003520 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3523 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003524 }
3525
3526 /* The bitmask needs one bit per byte of message excluding header */
3527 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3528
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003529 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003530 if( ssl->handshake->hs_msg == NULL )
3531 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003533 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003534 }
3535
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003536 /* Prepare final header: copy msg_type, length and message_seq,
3537 * then add standardised fragment_offset and fragment_length */
3538 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3539 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3540 memcpy( ssl->handshake->hs_msg + 9,
3541 ssl->handshake->hs_msg + 1, 3 );
3542 }
3543 else
3544 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003545 /* Make sure msg_type and length are consistent */
3546 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3549 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003550 }
3551 }
3552
3553 msg = ssl->handshake->hs_msg + 12;
3554 bitmask = msg + msg_len;
3555
3556 /*
3557 * Check and copy current fragment
3558 */
3559 frag_off = ( ssl->in_msg[6] << 16 ) |
3560 ( ssl->in_msg[7] << 8 ) |
3561 ssl->in_msg[8];
3562 frag_len = ( ssl->in_msg[9] << 16 ) |
3563 ( ssl->in_msg[10] << 8 ) |
3564 ssl->in_msg[11];
3565
3566 if( frag_off + frag_len > msg_len )
3567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003569 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003571 }
3572
3573 if( frag_len + 12 > ssl->in_msglen )
3574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003576 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003578 }
3579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003581 frag_off, frag_len ) );
3582
3583 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3584 ssl_bitmask_set( bitmask, frag_off, frag_len );
3585
3586 /*
3587 * Do we have the complete message by now?
3588 * If yes, finalize it, else ask to read the next record.
3589 */
3590 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003593 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003594 }
3595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003597
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003598 if( frag_len + 12 < ssl->in_msglen )
3599 {
3600 /*
3601 * We'got more handshake messages in the same record.
3602 * This case is not handled now because no know implementation does
3603 * that and it's hard to test, so we prefer to fail cleanly for now.
3604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3606 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003607 }
3608
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003609 if( ssl->in_left > ssl->next_record_offset )
3610 {
3611 /*
3612 * We've got more data in the buffer after the current record,
3613 * that we don't want to overwrite. Move it before writing the
3614 * reassembled message, and adjust in_left and next_record_offset.
3615 */
3616 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3617 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3618 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3619
3620 /* First compute and check new lengths */
3621 ssl->next_record_offset = new_remain - ssl->in_hdr;
3622 ssl->in_left = ssl->next_record_offset + remain_len;
3623
Angus Grattond8213d02016-05-25 20:56:48 +10003624 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003625 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3628 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003629 }
3630
3631 memmove( new_remain, cur_remain, remain_len );
3632 }
3633
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003634 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003637 ssl->handshake->hs_msg = NULL;
3638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003640 ssl->in_msg, ssl->in_hslen );
3641
3642 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003645
Simon Butcher99000142016-10-13 17:21:01 +01003646int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003651 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003653 }
3654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003656 ( ssl->in_msg[1] << 16 ) |
3657 ( ssl->in_msg[2] << 8 ) |
3658 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003661 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003662 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003666 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003667 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003668 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003669
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003670 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003671 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3672 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3673 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3674 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003675 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003676 /* Retransmit only on last message from previous flight, to avoid
3677 * too many retransmissions.
3678 * Besides, No sane server ever retransmits HelloVerifyRequest */
3679 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003683 "message_seq = %d, start_of_flight = %d",
3684 recv_msg_seq,
3685 ssl->handshake->in_flight_start_seq ) );
3686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003690 return( ret );
3691 }
3692 }
3693 else
3694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003696 "message_seq = %d, expected = %d",
3697 recv_msg_seq,
3698 ssl->handshake->in_msg_seq ) );
3699 }
3700
Hanno Becker90333da2017-10-10 11:27:13 +01003701 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003702 }
3703 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003704
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003705 /* Reassemble if current message is fragmented or reassembly is
3706 * already in progress */
3707 if( ssl->in_msglen < ssl->in_hslen ||
3708 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3709 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3710 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003713
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003714 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003717 return( ret );
3718 }
3719 }
3720 }
3721 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003723 /* With TLS we don't handle fragmentation (for now) */
3724 if( ssl->in_msglen < ssl->in_hslen )
3725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3727 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003728 }
3729
Simon Butcher99000142016-10-13 17:21:01 +01003730 return( 0 );
3731}
3732
3733void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3734{
3735
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003736 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3737 ssl->handshake != NULL )
3738 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003739 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003740 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003741
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003742 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003744 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003745 ssl->handshake != NULL )
3746 {
3747 ssl->handshake->in_msg_seq++;
3748 }
3749#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003750}
3751
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003752/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003753 * DTLS anti-replay: RFC 6347 4.1.2.6
3754 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003755 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3756 * Bit n is set iff record number in_window_top - n has been seen.
3757 *
3758 * Usually, in_window_top is the last record number seen and the lsb of
3759 * in_window is set. The only exception is the initial state (record number 0
3760 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3763static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003764{
3765 ssl->in_window_top = 0;
3766 ssl->in_window = 0;
3767}
3768
3769static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3770{
3771 return( ( (uint64_t) buf[0] << 40 ) |
3772 ( (uint64_t) buf[1] << 32 ) |
3773 ( (uint64_t) buf[2] << 24 ) |
3774 ( (uint64_t) buf[3] << 16 ) |
3775 ( (uint64_t) buf[4] << 8 ) |
3776 ( (uint64_t) buf[5] ) );
3777}
3778
3779/*
3780 * Return 0 if sequence number is acceptable, -1 otherwise
3781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003783{
3784 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3785 uint64_t bit;
3786
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003787 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003788 return( 0 );
3789
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003790 if( rec_seqnum > ssl->in_window_top )
3791 return( 0 );
3792
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003793 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003794
3795 if( bit >= 64 )
3796 return( -1 );
3797
3798 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3799 return( -1 );
3800
3801 return( 0 );
3802}
3803
3804/*
3805 * Update replay window on new validated record
3806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003808{
3809 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3810
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003811 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003812 return;
3813
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003814 if( rec_seqnum > ssl->in_window_top )
3815 {
3816 /* Update window_top and the contents of the window */
3817 uint64_t shift = rec_seqnum - ssl->in_window_top;
3818
3819 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003820 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003821 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003822 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003823 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003824 ssl->in_window |= 1;
3825 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003826
3827 ssl->in_window_top = rec_seqnum;
3828 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003829 else
3830 {
3831 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003832 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003833
3834 if( bit < 64 ) /* Always true, but be extra sure */
3835 ssl->in_window |= (uint64_t) 1 << bit;
3836 }
3837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003839
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003840#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003841/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003842static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3843
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003844/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003845 * Without any SSL context, check if a datagram looks like a ClientHello with
3846 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003847 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003848 *
3849 * - if cookie is valid, return 0
3850 * - if ClientHello looks superficially valid but cookie is not,
3851 * fill obuf and set olen, then
3852 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3853 * - otherwise return a specific error code
3854 */
3855static int ssl_check_dtls_clihlo_cookie(
3856 mbedtls_ssl_cookie_write_t *f_cookie_write,
3857 mbedtls_ssl_cookie_check_t *f_cookie_check,
3858 void *p_cookie,
3859 const unsigned char *cli_id, size_t cli_id_len,
3860 const unsigned char *in, size_t in_len,
3861 unsigned char *obuf, size_t buf_len, size_t *olen )
3862{
3863 size_t sid_len, cookie_len;
3864 unsigned char *p;
3865
3866 if( f_cookie_write == NULL || f_cookie_check == NULL )
3867 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3868
3869 /*
3870 * Structure of ClientHello with record and handshake headers,
3871 * and expected values. We don't need to check a lot, more checks will be
3872 * done when actually parsing the ClientHello - skipping those checks
3873 * avoids code duplication and does not make cookie forging any easier.
3874 *
3875 * 0-0 ContentType type; copied, must be handshake
3876 * 1-2 ProtocolVersion version; copied
3877 * 3-4 uint16 epoch; copied, must be 0
3878 * 5-10 uint48 sequence_number; copied
3879 * 11-12 uint16 length; (ignored)
3880 *
3881 * 13-13 HandshakeType msg_type; (ignored)
3882 * 14-16 uint24 length; (ignored)
3883 * 17-18 uint16 message_seq; copied
3884 * 19-21 uint24 fragment_offset; copied, must be 0
3885 * 22-24 uint24 fragment_length; (ignored)
3886 *
3887 * 25-26 ProtocolVersion client_version; (ignored)
3888 * 27-58 Random random; (ignored)
3889 * 59-xx SessionID session_id; 1 byte len + sid_len content
3890 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3891 * ...
3892 *
3893 * Minimum length is 61 bytes.
3894 */
3895 if( in_len < 61 ||
3896 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3897 in[3] != 0 || in[4] != 0 ||
3898 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3899 {
3900 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3901 }
3902
3903 sid_len = in[59];
3904 if( sid_len > in_len - 61 )
3905 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3906
3907 cookie_len = in[60 + sid_len];
3908 if( cookie_len > in_len - 60 )
3909 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3910
3911 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3912 cli_id, cli_id_len ) == 0 )
3913 {
3914 /* Valid cookie */
3915 return( 0 );
3916 }
3917
3918 /*
3919 * If we get here, we've got an invalid cookie, let's prepare HVR.
3920 *
3921 * 0-0 ContentType type; copied
3922 * 1-2 ProtocolVersion version; copied
3923 * 3-4 uint16 epoch; copied
3924 * 5-10 uint48 sequence_number; copied
3925 * 11-12 uint16 length; olen - 13
3926 *
3927 * 13-13 HandshakeType msg_type; hello_verify_request
3928 * 14-16 uint24 length; olen - 25
3929 * 17-18 uint16 message_seq; copied
3930 * 19-21 uint24 fragment_offset; copied
3931 * 22-24 uint24 fragment_length; olen - 25
3932 *
3933 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3934 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3935 *
3936 * Minimum length is 28.
3937 */
3938 if( buf_len < 28 )
3939 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3940
3941 /* Copy most fields and adapt others */
3942 memcpy( obuf, in, 25 );
3943 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3944 obuf[25] = 0xfe;
3945 obuf[26] = 0xff;
3946
3947 /* Generate and write actual cookie */
3948 p = obuf + 28;
3949 if( f_cookie_write( p_cookie,
3950 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3951 {
3952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3953 }
3954
3955 *olen = p - obuf;
3956
3957 /* Go back and fill length fields */
3958 obuf[27] = (unsigned char)( *olen - 28 );
3959
3960 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3961 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3962 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3963
3964 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3965 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3966
3967 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3968}
3969
3970/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003971 * Handle possible client reconnect with the same UDP quadruplet
3972 * (RFC 6347 Section 4.2.8).
3973 *
3974 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3975 * that looks like a ClientHello.
3976 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003977 * - if the input looks like a ClientHello without cookies,
3978 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003979 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003980 * - if the input looks like a ClientHello with a valid cookie,
3981 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003982 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003983 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003984 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003985 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003986 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3987 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003988 */
3989static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3990{
3991 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003992 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003993
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003994 ret = ssl_check_dtls_clihlo_cookie(
3995 ssl->conf->f_cookie_write,
3996 ssl->conf->f_cookie_check,
3997 ssl->conf->p_cookie,
3998 ssl->cli_id, ssl->cli_id_len,
3999 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004000 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004001
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004002 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4003
4004 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004005 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004006 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004007 * If the error is permanent we'll catch it later,
4008 * if it's not, then hopefully it'll work next time. */
4009 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4010
4011 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004012 }
4013
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004014 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004015 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004016 /* Got a valid cookie, partially reset context */
4017 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4018 {
4019 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4020 return( ret );
4021 }
4022
4023 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004024 }
4025
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004026 return( ret );
4027}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004028#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004029
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004030/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004031 * ContentType type;
4032 * ProtocolVersion version;
4033 * uint16 epoch; // DTLS only
4034 * uint48 sequence_number; // DTLS only
4035 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004036 *
4037 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004038 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004039 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4040 *
4041 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004042 * 1. proceed with the record if this function returns 0
4043 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4044 * 3. return CLIENT_RECONNECT if this function return that value
4045 * 4. drop the whole datagram if this function returns anything else.
4046 * Point 2 is needed when the peer is resending, and we have already received
4047 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004050{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004051 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 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 +02004054
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004056 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004057 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004060 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004061 ssl->in_msgtype,
4062 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004063
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004064 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4066 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4067 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4068 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004071
4072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004073 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4074 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004075 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4076#endif /* MBEDTLS_SSL_PROTO_DTLS */
4077 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4078 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004081 }
4082
4083 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004084 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4087 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004088 }
4089
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004090 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4093 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004094 }
4095
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004096 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004097 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004098 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4101 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004102 }
4103
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004104 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004105 * DTLS-related tests.
4106 * Check epoch before checking length constraint because
4107 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4108 * message gets duplicated before the corresponding Finished message,
4109 * the second ChangeCipherSpec should be discarded because it belongs
4110 * to an old epoch, but not because its length is shorter than
4111 * the minimum record length for packets using the new record transform.
4112 * Note that these two kinds of failures are handled differently,
4113 * as an unexpected record is silently skipped but an invalid
4114 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004115 */
4116#if defined(MBEDTLS_SSL_PROTO_DTLS)
4117 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4118 {
4119 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4120
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004121 /* Check epoch (and sequence number) with DTLS */
4122 if( rec_epoch != ssl->in_epoch )
4123 {
4124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4125 "expected %d, received %d",
4126 ssl->in_epoch, rec_epoch ) );
4127
4128#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4129 /*
4130 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4131 * access the first byte of record content (handshake type), as we
4132 * have an active transform (possibly iv_len != 0), so use the
4133 * fact that the record header len is 13 instead.
4134 */
4135 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4136 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4137 rec_epoch == 0 &&
4138 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4139 ssl->in_left > 13 &&
4140 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4141 {
4142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4143 "from the same port" ) );
4144 return( ssl_handle_possible_reconnect( ssl ) );
4145 }
4146 else
4147#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4148 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4149 }
4150
4151#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4152 /* Replay detection only works for the current epoch */
4153 if( rec_epoch == ssl->in_epoch &&
4154 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4155 {
4156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4157 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4158 }
4159#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004160
4161 /* Drop unexpected ChangeCipherSpec messages */
4162 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4163 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4164 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4165 {
4166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4167 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4168 }
4169
4170 /* Drop unexpected ApplicationData records,
4171 * except at the beginning of renegotiations */
4172 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4173 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4174#if defined(MBEDTLS_SSL_RENEGOTIATION)
4175 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4176 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4177#endif
4178 )
4179 {
4180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4181 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4182 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004183 }
4184#endif /* MBEDTLS_SSL_PROTO_DTLS */
4185
Hanno Becker52c6dc62017-05-26 16:07:36 +01004186
4187 /* Check length against bounds of the current transform and version */
4188 if( ssl->transform_in == NULL )
4189 {
4190 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004191 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004192 {
4193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4194 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4195 }
4196 }
4197 else
4198 {
4199 if( ssl->in_msglen < ssl->transform_in->minlen )
4200 {
4201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4202 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4203 }
4204
4205#if defined(MBEDTLS_SSL_PROTO_SSL3)
4206 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004207 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004208 {
4209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4210 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4211 }
4212#endif
4213#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4214 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4215 /*
4216 * TLS encrypted messages can have up to 256 bytes of padding
4217 */
4218 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4219 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004220 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004221 {
4222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4223 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4224 }
4225#endif
4226 }
4227
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004228 return( 0 );
4229}
Paul Bakker5121ce52009-01-03 21:22:43 +00004230
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004231/*
4232 * If applicable, decrypt (and decompress) record content
4233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004235{
4236 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4239 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4242 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 ret = mbedtls_ssl_hw_record_read( ssl );
4247 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4250 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004251 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004252
4253 if( ret == 0 )
4254 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004255 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004257 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 {
4259 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004262 return( ret );
4263 }
4264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 ssl->in_msg, ssl->in_msglen );
4267
Angus Grattond8213d02016-05-25 20:56:48 +10004268 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4271 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272 }
4273 }
4274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004276 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004278 {
4279 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004282 return( ret );
4283 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004284 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004291 }
4292#endif
4293
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004294 return( 0 );
4295}
4296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004298
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004299/*
4300 * Read a record.
4301 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004302 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4303 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4304 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004307{
4308 int ret;
4309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004311
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004312 if( ssl->keep_current_message == 0 )
4313 {
4314 do {
Simon Butcher99000142016-10-13 17:21:01 +01004315
Hanno Becker90333da2017-10-10 11:27:13 +01004316 do ret = mbedtls_ssl_read_record_layer( ssl );
4317 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4318
4319 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004320 {
4321 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4322 return( ret );
4323 }
4324
4325 ret = mbedtls_ssl_handle_message_type( ssl );
4326
Hanno Becker90333da2017-10-10 11:27:13 +01004327 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4328 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004329
4330 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004331 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004332 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004333 return( ret );
4334 }
4335
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004336 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4337 {
4338 mbedtls_ssl_update_handshake_status( ssl );
4339 }
Simon Butcher99000142016-10-13 17:21:01 +01004340 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004341 else
Simon Butcher99000142016-10-13 17:21:01 +01004342 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4344 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004345 }
4346
4347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4348
4349 return( 0 );
4350}
4351
4352int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4353{
4354 int ret;
4355
Hanno Becker4a810fb2017-05-24 16:27:30 +01004356 /*
4357 * Step A
4358 *
4359 * Consume last content-layer message and potentially
4360 * update in_msglen which keeps track of the contents'
4361 * consumption state.
4362 *
4363 * (1) Handshake messages:
4364 * Remove last handshake message, move content
4365 * and adapt in_msglen.
4366 *
4367 * (2) Alert messages:
4368 * Consume whole record content, in_msglen = 0.
4369 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004370 * (3) Change cipher spec:
4371 * Consume whole record content, in_msglen = 0.
4372 *
4373 * (4) Application data:
4374 * Don't do anything - the record layer provides
4375 * the application data as a stream transport
4376 * and consumes through mbedtls_ssl_read only.
4377 *
4378 */
4379
4380 /* Case (1): Handshake messages */
4381 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004382 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004383 /* Hard assertion to be sure that no application data
4384 * is in flight, as corrupting ssl->in_msglen during
4385 * ssl->in_offt != NULL is fatal. */
4386 if( ssl->in_offt != NULL )
4387 {
4388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4390 }
4391
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004392 /*
4393 * Get next Handshake message in the current record
4394 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004395
Hanno Becker4a810fb2017-05-24 16:27:30 +01004396 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004397 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004398 * current handshake content: If DTLS handshake
4399 * fragmentation is used, that's the fragment
4400 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004401 * size here is faulty and should be changed at
4402 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004403 * (2) While it doesn't seem to cause problems, one
4404 * has to be very careful not to assume that in_hslen
4405 * is always <= in_msglen in a sensible communication.
4406 * Again, it's wrong for DTLS handshake fragmentation.
4407 * The following check is therefore mandatory, and
4408 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004409 * Additionally, ssl->in_hslen might be arbitrarily out of
4410 * bounds after handling a DTLS message with an unexpected
4411 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004412 */
4413 if( ssl->in_hslen < ssl->in_msglen )
4414 {
4415 ssl->in_msglen -= ssl->in_hslen;
4416 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4417 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004418
Hanno Becker4a810fb2017-05-24 16:27:30 +01004419 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4420 ssl->in_msg, ssl->in_msglen );
4421 }
4422 else
4423 {
4424 ssl->in_msglen = 0;
4425 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004426
Hanno Becker4a810fb2017-05-24 16:27:30 +01004427 ssl->in_hslen = 0;
4428 }
4429 /* Case (4): Application data */
4430 else if( ssl->in_offt != NULL )
4431 {
4432 return( 0 );
4433 }
4434 /* Everything else (CCS & Alerts) */
4435 else
4436 {
4437 ssl->in_msglen = 0;
4438 }
4439
4440 /*
4441 * Step B
4442 *
4443 * Fetch and decode new record if current one is fully consumed.
4444 *
4445 */
4446
4447 if( ssl->in_msglen > 0 )
4448 {
4449 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004450 return( 0 );
4451 }
4452
Hanno Becker4a810fb2017-05-24 16:27:30 +01004453 /* Current record either fully processed or to be discarded. */
4454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004458 return( ret );
4459 }
4460
4461 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4465 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004466 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004467 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4468 {
4469 /* Skip unexpected record (but not whole datagram) */
4470 ssl->next_record_offset = ssl->in_msglen
4471 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004472
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4474 "(header)" ) );
4475 }
4476 else
4477 {
4478 /* Skip invalid record and the rest of the datagram */
4479 ssl->next_record_offset = 0;
4480 ssl->in_left = 0;
4481
4482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4483 "(header)" ) );
4484 }
4485
4486 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004487 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004488 }
4489#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004490 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004491 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004492
4493 /*
4494 * Read and optionally decrypt the message contents
4495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4497 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004500 return( ret );
4501 }
4502
4503 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004505 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004508 if( ssl->next_record_offset < ssl->in_left )
4509 {
4510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4511 }
4512 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004513 else
4514#endif
4515 ssl->in_left = 0;
4516
4517 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004521 {
4522 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4524 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004525 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004526 /* Except when waiting for Finished as a bad mac here
4527 * probably means something went wrong in the handshake
4528 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4529 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4530 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4531 {
4532#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4533 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4534 {
4535 mbedtls_ssl_send_alert_message( ssl,
4536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4537 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4538 }
4539#endif
4540 return( ret );
4541 }
4542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004544 if( ssl->conf->badmac_limit != 0 &&
4545 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4548 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004549 }
4550#endif
4551
Hanno Becker4a810fb2017-05-24 16:27:30 +01004552 /* As above, invalid records cause
4553 * dismissal of the whole datagram. */
4554
4555 ssl->next_record_offset = 0;
4556 ssl->in_left = 0;
4557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004559 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004560 }
4561
4562 return( ret );
4563 }
4564 else
4565#endif
4566 {
4567 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4569 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 mbedtls_ssl_send_alert_message( ssl,
4572 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4573 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004574 }
4575#endif
4576 return( ret );
4577 }
4578 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004579
Simon Butcher99000142016-10-13 17:21:01 +01004580 return( 0 );
4581}
4582
4583int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4584{
4585 int ret;
4586
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004587 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004588 * Handle particular types of records
4589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 {
Simon Butcher99000142016-10-13 17:21:01 +01004592 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4593 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004594 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004596 }
4597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004600 if( ssl->in_msglen != 2 )
4601 {
4602 /* Note: Standard allows for more than one 2 byte alert
4603 to be packed in a single message, but Mbed TLS doesn't
4604 currently support this. */
4605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4606 ssl->in_msglen ) );
4607 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4608 }
4609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 ssl->in_msg[0], ssl->in_msg[1] ) );
4612
4613 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004614 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004619 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 }
4622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4624 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4627 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004629
4630#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4631 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4632 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4633 {
Hanno Becker90333da2017-10-10 11:27:13 +01004634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004635 /* Will be handled when trying to parse ServerHello */
4636 return( 0 );
4637 }
4638#endif
4639
4640#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4641 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4642 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4643 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4644 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4645 {
4646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4647 /* Will be handled in mbedtls_ssl_parse_certificate() */
4648 return( 0 );
4649 }
4650#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4651
4652 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004653 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 }
4655
Hanno Beckerc76c6192017-06-06 10:03:17 +01004656#if defined(MBEDTLS_SSL_PROTO_DTLS)
4657 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4658 ssl->handshake != NULL &&
4659 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4660 {
4661 ssl_handshake_wrapup_free_hs_transform( ssl );
4662 }
4663#endif
4664
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 return( 0 );
4666}
4667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004669{
4670 int ret;
4671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4673 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4674 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004675 {
4676 return( ret );
4677 }
4678
4679 return( 0 );
4680}
4681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004683 unsigned char level,
4684 unsigned char message )
4685{
4686 int ret;
4687
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004688 if( ssl == NULL || ssl->conf == NULL )
4689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004695 ssl->out_msglen = 2;
4696 ssl->out_msg[0] = level;
4697 ssl->out_msg[1] = message;
4698
Hanno Becker67bc7c32018-08-06 11:33:50 +01004699 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004702 return( ret );
4703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004705
4706 return( 0 );
4707}
4708
Paul Bakker5121ce52009-01-03 21:22:43 +00004709/*
4710 * Handshake functions
4711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4713 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4714 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4715 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4716 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4717 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4718 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004719/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004722 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4727 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004728 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4729 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004732 ssl->state++;
4733 return( 0 );
4734 }
4735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004738}
4739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4747 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004748 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4749 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004752 ssl->state++;
4753 return( 0 );
4754 }
4755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4757 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004758}
Gilles Peskinef9828522017-05-03 12:28:43 +02004759
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004760#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004761/* Some certificate support -> implement write and parse */
4762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004766 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 const mbedtls_x509_crt *crt;
4768 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4773 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004774 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4775 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004778 ssl->state++;
4779 return( 0 );
4780 }
4781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004783 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004784 {
4785 if( ssl->client_auth == 0 )
4786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004788 ssl->state++;
4789 return( 0 );
4790 }
4791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004793 /*
4794 * If using SSLv3 and got no cert, send an Alert message
4795 * (otherwise an empty Certificate message will be sent).
4796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4798 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004799 {
4800 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4802 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4803 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004806 goto write_msg;
4807 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004809 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810#endif /* MBEDTLS_SSL_CLI_C */
4811#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004812 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4817 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 }
4819 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004820#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004823
4824 /*
4825 * 0 . 0 handshake type
4826 * 1 . 3 handshake length
4827 * 4 . 6 length of all certs
4828 * 7 . 9 length of cert. 1
4829 * 10 . n-1 peer certificate
4830 * n . n+2 length of cert. 2
4831 * n+3 . ... upper level cert, etc.
4832 */
4833 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835
Paul Bakker29087132010-03-21 21:03:34 +00004836 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004837 {
4838 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004839 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004842 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004844 }
4845
4846 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4847 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4848 ssl->out_msg[i + 2] = (unsigned char)( n );
4849
4850 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4851 i += n; crt = crt->next;
4852 }
4853
4854 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4855 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4856 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4857
4858 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4860 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004861
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004862#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004863write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004864#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004865
4866 ssl->state++;
4867
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004868 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004869 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004871 return( ret );
4872 }
4873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004875
Paul Bakkered27a042013-04-18 22:46:23 +02004876 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004877}
4878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004882 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004884 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004885 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4890 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004891 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4892 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004895 ssl->state++;
4896 return( 0 );
4897 }
4898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004900 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004901 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4902 {
4903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4904 ssl->state++;
4905 return( 0 );
4906 }
4907
4908#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4909 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4910 authmode = ssl->handshake->sni_authmode;
4911#endif
4912
4913 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4914 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004915 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004916 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004918 ssl->state++;
4919 return( 0 );
4920 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004921#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004924 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004925 /* mbedtls_ssl_read_record may have sent an alert already. We
4926 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004928 return( ret );
4929 }
4930
4931 ssl->state++;
4932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933#if defined(MBEDTLS_SSL_SRV_C)
4934#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004935 /*
4936 * Check if the client sent an empty certificate
4937 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004940 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004941 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4943 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4944 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004947
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004948 /* The client was asked for a certificate but didn't send
4949 one. The client should know what's going on, so we
4950 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004951 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004952 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004953 return( 0 );
4954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004956 }
4957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4961 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004962 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4966 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4967 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4968 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004971
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004972 /* The client was asked for a certificate but didn't send
4973 one. The client should know what's going on, so we
4974 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004975 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004976 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004977 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004978 else
4979 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004980 }
4981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4983 MBEDTLS_SSL_PROTO_TLS1_2 */
4984#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004989 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4990 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004991 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004992 }
4993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4995 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005001 }
5002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005004
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005007 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005008 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005009
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005010 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005014 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5015 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005017 }
5018
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005019 /* In case we tried to reuse a session but it failed */
5020 if( ssl->session_negotiate->peer_cert != NULL )
5021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5023 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005024 }
5025
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005026 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005028 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005031 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5032 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005033 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005034 }
5035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005037
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005038 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005039
5040 while( i < ssl->in_hslen )
5041 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005042 if ( i + 3 > ssl->in_hslen ) {
5043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5044 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5045 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5046 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5047 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 if( ssl->in_msg[i] != 0 )
5049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005051 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5052 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005054 }
5055
5056 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5057 | (unsigned int) ssl->in_msg[i + 2];
5058 i += 3;
5059
5060 if( n < 128 || i + n > ssl->in_hslen )
5061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005063 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5064 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005066 }
5067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005069 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005070 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005072 case 0: /*ok*/
5073 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5074 /* Ignore certificate with an unknown algorithm: maybe a
5075 prior certificate was already trusted. */
5076 break;
5077
5078 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5079 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5080 goto crt_parse_der_failed;
5081
5082 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5083 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5084 goto crt_parse_der_failed;
5085
5086 default:
5087 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5088 crt_parse_der_failed:
5089 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005091 return( ret );
5092 }
5093
5094 i += n;
5095 }
5096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005098
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005099 /*
5100 * On client, make sure the server cert doesn't change during renego to
5101 * avoid "triple handshake" attack: https://secure-resumption.com/
5102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005104 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005106 {
5107 if( ssl->session->peer_cert == NULL )
5108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005110 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5111 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005113 }
5114
5115 if( ssl->session->peer_cert->raw.len !=
5116 ssl->session_negotiate->peer_cert->raw.len ||
5117 memcmp( ssl->session->peer_cert->raw.p,
5118 ssl->session_negotiate->peer_cert->raw.p,
5119 ssl->session->peer_cert->raw.len ) != 0 )
5120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005122 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5123 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005124 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005125 }
5126 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005128
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005129 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005130 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005131 mbedtls_x509_crt *ca_chain;
5132 mbedtls_x509_crl *ca_crl;
5133
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005134#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005135 if( ssl->handshake->sni_ca_chain != NULL )
5136 {
5137 ca_chain = ssl->handshake->sni_ca_chain;
5138 ca_crl = ssl->handshake->sni_ca_crl;
5139 }
5140 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005141#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005142 {
5143 ca_chain = ssl->conf->ca_chain;
5144 ca_crl = ssl->conf->ca_crl;
5145 }
5146
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005147 /*
5148 * Main check: verify certificate
5149 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005150 ret = mbedtls_x509_crt_verify_with_profile(
5151 ssl->session_negotiate->peer_cert,
5152 ca_chain, ca_crl,
5153 ssl->conf->cert_profile,
5154 ssl->hostname,
5155 &ssl->session_negotiate->verify_result,
5156 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005157
5158 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005161 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005162
5163 /*
5164 * Secondary checks: always done, but change 'ret' only if it was 0
5165 */
5166
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005167#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005170
5171 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005173 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005174 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005175 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005178 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005180 }
5181 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005182#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005185 ciphersuite_info,
5186 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005187 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005190 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005192 }
5193
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005194 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5195 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5196 * with details encoded in the verification flags. All other kinds
5197 * of error codes, including those from the user provided f_vrfy
5198 * functions, are treated as fatal and lead to a failure of
5199 * ssl_parse_certificate even if verification was optional. */
5200 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5201 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5202 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5203 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005204 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005205 }
5206
5207 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5208 {
5209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5210 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5211 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005212
5213 if( ret != 0 )
5214 {
5215 /* The certificate may have been rejected for several reasons.
5216 Pick one and send the corresponding alert. Which alert to send
5217 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005218 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5219 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5220 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5221 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5222 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5223 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5224 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5225 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5226 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5227 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5228 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5229 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5230 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5231 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5232 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5233 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5234 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5235 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5236 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5237 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005238 else
5239 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5241 alert );
5242 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005243
Hanno Beckere6706e62017-05-15 16:05:15 +01005244#if defined(MBEDTLS_DEBUG_C)
5245 if( ssl->session_negotiate->verify_result != 0 )
5246 {
5247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5248 ssl->session_negotiate->verify_result ) );
5249 }
5250 else
5251 {
5252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5253 }
5254#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005255 }
5256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005258
5259 return( ret );
5260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5262 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5263 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5264 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5265 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5266 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5267 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005270{
5271 int ret;
5272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005276 ssl->out_msglen = 1;
5277 ssl->out_msg[0] = 1;
5278
Paul Bakker5121ce52009-01-03 21:22:43 +00005279 ssl->state++;
5280
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005281 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005284 return( ret );
5285 }
5286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005288
5289 return( 0 );
5290}
5291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005293{
5294 int ret;
5295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005301 return( ret );
5302 }
5303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005307 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5308 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005310 }
5311
5312 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005315 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5316 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 }
5319
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005320 /*
5321 * Switch to our negotiated transform and session parameters for inbound
5322 * data.
5323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005325 ssl->transform_in = ssl->transform_negotiate;
5326 ssl->session_in = ssl->session_negotiate;
5327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005332 ssl_dtls_replay_reset( ssl );
5333#endif
5334
5335 /* Increment epoch */
5336 if( ++ssl->in_epoch == 0 )
5337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005339 /* This is highly unlikely to happen for legitimate reasons, so
5340 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005342 }
5343 }
5344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005346 memset( ssl->in_ctr, 0, 8 );
5347
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005348 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5351 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005356 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5357 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005359 }
5360 }
5361#endif
5362
Paul Bakker5121ce52009-01-03 21:22:43 +00005363 ssl->state++;
5364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005366
5367 return( 0 );
5368}
5369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5371 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005372{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005373 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5376 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5377 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005378 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005379 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5382#if defined(MBEDTLS_SHA512_C)
5383 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005384 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5385 else
5386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387#if defined(MBEDTLS_SHA256_C)
5388 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005389 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005390 else
5391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005395 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005396 }
Paul Bakker380da532012-04-18 16:10:25 +00005397}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5402 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005403 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5404 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5407#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005408 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005411 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005414}
5415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005417 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5420 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005421 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5422 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5425#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005426 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005429 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005432}
5433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5435 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5436static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005437 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005438{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005439 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5440 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005441}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005442#endif
Paul Bakker380da532012-04-18 16:10:25 +00005443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5445#if defined(MBEDTLS_SHA256_C)
5446static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005447 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005448{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005449 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, 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_SHA512_C)
5454static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005455 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005456{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005457 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005458}
Paul Bakker769075d2012-11-24 11:26:46 +01005459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005463static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005465{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005466 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005467 mbedtls_md5_context md5;
5468 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005469
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 unsigned char padbuf[48];
5471 unsigned char md5sum[16];
5472 unsigned char sha1sum[20];
5473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005475 if( !session )
5476 session = ssl->session;
5477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005479
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005480 mbedtls_md5_init( &md5 );
5481 mbedtls_sha1_init( &sha1 );
5482
5483 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5484 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005485
5486 /*
5487 * SSLv3:
5488 * hash =
5489 * MD5( master + pad2 +
5490 * MD5( handshake + sender + master + pad1 ) )
5491 * + SHA1( master + pad2 +
5492 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005493 */
5494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005496 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5497 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005498#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005501 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5502 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005506 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005507
Paul Bakker1ef83d62012-04-11 12:09:53 +00005508 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005509
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005510 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5511 mbedtls_md5_update_ret( &md5, session->master, 48 );
5512 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5513 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005514
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005515 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5516 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5517 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5518 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005519
Paul Bakker1ef83d62012-04-11 12:09:53 +00005520 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005521
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005522 mbedtls_md5_starts_ret( &md5 );
5523 mbedtls_md5_update_ret( &md5, session->master, 48 );
5524 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5525 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5526 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005527
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005528 mbedtls_sha1_starts_ret( &sha1 );
5529 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5530 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5531 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5532 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005536 mbedtls_md5_free( &md5 );
5537 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005538
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005539 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5540 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5541 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005548static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005550{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005551 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005552 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005553 mbedtls_md5_context md5;
5554 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005555 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005558 if( !session )
5559 session = ssl->session;
5560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005563 mbedtls_md5_init( &md5 );
5564 mbedtls_sha1_init( &sha1 );
5565
5566 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5567 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005568
Paul Bakker1ef83d62012-04-11 12:09:53 +00005569 /*
5570 * TLSv1:
5571 * hash = PRF( master, finished_label,
5572 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5573 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005576 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5577 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005578#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005581 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5582 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005583#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005586 ? "client finished"
5587 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005588
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005589 mbedtls_md5_finish_ret( &md5, padbuf );
5590 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005591
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005592 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005593 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005596
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005597 mbedtls_md5_free( &md5 );
5598 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005599
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005600 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5607#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005608static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005610{
5611 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005612 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005613 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005614 unsigned char padbuf[32];
5615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005617 if( !session )
5618 session = ssl->session;
5619
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005620 mbedtls_sha256_init( &sha256 );
5621
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005623
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005624 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005625
5626 /*
5627 * TLSv1.2:
5628 * hash = PRF( master, finished_label,
5629 * Hash( handshake ) )[0.11]
5630 */
5631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632#if !defined(MBEDTLS_SHA256_ALT)
5633 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005634 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005635#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005638 ? "client finished"
5639 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005640
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005641 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005642
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005643 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005644 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005647
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005648 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005649
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005650 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005657static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005659{
5660 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005661 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005662 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005663 unsigned char padbuf[48];
5664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005666 if( !session )
5667 session = ssl->session;
5668
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005669 mbedtls_sha512_init( &sha512 );
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005672
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005673 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005674
5675 /*
5676 * TLSv1.2:
5677 * hash = PRF( master, finished_label,
5678 * Hash( handshake ) )[0.11]
5679 */
5680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005682 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5683 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005684#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005687 ? "client finished"
5688 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005689
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005690 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005691
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005692 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005693 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005696
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005697 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005698
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005699 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703#endif /* MBEDTLS_SHA512_C */
5704#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005709
5710 /*
5711 * Free our handshake params
5712 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005713 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005715 ssl->handshake = NULL;
5716
5717 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005718 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005719 */
5720 if( ssl->transform )
5721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 mbedtls_ssl_transform_free( ssl->transform );
5723 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005724 }
5725 ssl->transform = ssl->transform_negotiate;
5726 ssl->transform_negotiate = NULL;
5727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005729}
5730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005732{
5733 int resume = ssl->handshake->resume;
5734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737#if defined(MBEDTLS_SSL_RENEGOTIATION)
5738 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005741 ssl->renego_records_seen = 0;
5742 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005743#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005744
5745 /*
5746 * Free the previous session and switch in the current one
5747 */
Paul Bakker0a597072012-09-25 21:55:46 +00005748 if( ssl->session )
5749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005751 /* RFC 7366 3.1: keep the EtM state */
5752 ssl->session_negotiate->encrypt_then_mac =
5753 ssl->session->encrypt_then_mac;
5754#endif
5755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756 mbedtls_ssl_session_free( ssl->session );
5757 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005758 }
5759 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005760 ssl->session_negotiate = NULL;
5761
Paul Bakker0a597072012-09-25 21:55:46 +00005762 /*
5763 * Add cache entry
5764 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005765 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005766 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005767 resume == 0 )
5768 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005769 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005771 }
Paul Bakker0a597072012-09-25 21:55:46 +00005772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005775 ssl->handshake->flight != NULL )
5776 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005777 /* Cancel handshake timer */
5778 ssl_set_timer( ssl, 0 );
5779
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005780 /* Keep last flight around in case we need to resend it:
5781 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005783 }
5784 else
5785#endif
5786 ssl_handshake_wrapup_free_hs_transform( ssl );
5787
Paul Bakker48916f92012-09-16 19:57:18 +00005788 ssl->state++;
5789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005791}
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005794{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005795 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005798
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005799 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01005800
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005801 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005802
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005803 /*
5804 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5805 * may define some other value. Currently (early 2016), no defined
5806 * ciphersuite does this (and this is unlikely to change as activity has
5807 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005812 ssl->verify_data_len = hash_len;
5813 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005814#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005815
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5818 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005819
5820 /*
5821 * In case of session resuming, invert the client and server
5822 * ChangeCipherSpec messages order.
5823 */
Paul Bakker0a597072012-09-25 21:55:46 +00005824 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005827 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005831 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 }
5835 else
5836 ssl->state++;
5837
Paul Bakker48916f92012-09-16 19:57:18 +00005838 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005839 * Switch to our negotiated transform and session parameters for outbound
5840 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005846 {
5847 unsigned char i;
5848
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005849 /* Remember current epoch settings for resending */
5850 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01005851 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005852
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005853 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01005854 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005855
5856 /* Increment epoch */
5857 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01005858 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005859 break;
5860
5861 /* The loop goes to its end iff the counter is wrapping */
5862 if( i == 0 )
5863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5865 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005866 }
5867 }
5868 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01005870 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005871
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005872 ssl->transform_out = ssl->transform_negotiate;
5873 ssl->session_out = ssl->session_negotiate;
5874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5876 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5881 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005882 }
5883 }
5884#endif
5885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005887 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005889#endif
5890
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005891 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005892 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005894 return( ret );
5895 }
5896
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005897#if defined(MBEDTLS_SSL_PROTO_DTLS)
5898 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5899 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5900 {
5901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5902 return( ret );
5903 }
5904#endif
5905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005907
5908 return( 0 );
5909}
5910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005912#define SSL_MAX_HASH_LEN 36
5913#else
5914#define SSL_MAX_HASH_LEN 12
5915#endif
5916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005918{
Paul Bakker23986e52011-04-24 08:57:21 +00005919 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005920 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005921 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005924
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005925 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005930 return( ret );
5931 }
5932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005936 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5937 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939 }
5940
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005941 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942#if defined(MBEDTLS_SSL_PROTO_SSL3)
5943 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005944 hash_len = 36;
5945 else
5946#endif
5947 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5950 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005953 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005956 }
5957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005959 buf, hash_len ) != 0 )
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 defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005968 ssl->verify_data_len = hash_len;
5969 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005970#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005971
Paul Bakker0a597072012-09-25 21:55:46 +00005972 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005981#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005982 }
5983 else
5984 ssl->state++;
5985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005989#endif
5990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005992
5993 return( 0 );
5994}
5995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005997{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6001 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6002 mbedtls_md5_init( &handshake->fin_md5 );
6003 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006004 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6005 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6008#if defined(MBEDTLS_SHA256_C)
6009 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006010 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012#if defined(MBEDTLS_SHA512_C)
6013 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006014 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006017
6018 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006019
6020#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6021 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6022 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6023#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#if defined(MBEDTLS_DHM_C)
6026 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028#if defined(MBEDTLS_ECDH_C)
6029 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006030#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006031#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006032 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006033#if defined(MBEDTLS_SSL_CLI_C)
6034 handshake->ecjpake_cache = NULL;
6035 handshake->ecjpake_cache_len = 0;
6036#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006037#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006038
6039#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6040 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6041#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006042}
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6049 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 mbedtls_md_init( &transform->md_ctx_enc );
6052 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006053}
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006058}
6059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006061{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006062 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006063 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006065 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006067 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006068 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006069
6070 /*
6071 * Either the pointers are now NULL or cleared properly and can be freed.
6072 * Now allocate missing structures.
6073 */
6074 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006075 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006076 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006077 }
Paul Bakker48916f92012-09-16 19:57:18 +00006078
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006079 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006080 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006081 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006082 }
Paul Bakker48916f92012-09-16 19:57:18 +00006083
Paul Bakker82788fb2014-10-20 13:59:19 +02006084 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006085 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006086 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006087 }
Paul Bakker48916f92012-09-16 19:57:18 +00006088
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006089 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006090 if( ssl->handshake == NULL ||
6091 ssl->transform_negotiate == NULL ||
6092 ssl->session_negotiate == NULL )
6093 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 mbedtls_free( ssl->handshake );
6097 mbedtls_free( ssl->transform_negotiate );
6098 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006099
6100 ssl->handshake = NULL;
6101 ssl->transform_negotiate = NULL;
6102 ssl->session_negotiate = NULL;
6103
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006104 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006105 }
6106
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006107 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006109 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006110 ssl_handshake_params_init( ssl->handshake );
6111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006113 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6114 {
6115 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006116
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6118 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6119 else
6120 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006121
6122 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006123 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006124#endif
6125
Paul Bakker48916f92012-09-16 19:57:18 +00006126 return( 0 );
6127}
6128
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006129#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006130/* Dummy cookie callbacks for defaults */
6131static int ssl_cookie_write_dummy( void *ctx,
6132 unsigned char **p, unsigned char *end,
6133 const unsigned char *cli_id, size_t cli_id_len )
6134{
6135 ((void) ctx);
6136 ((void) p);
6137 ((void) end);
6138 ((void) cli_id);
6139 ((void) cli_id_len);
6140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006142}
6143
6144static int ssl_cookie_check_dummy( void *ctx,
6145 const unsigned char *cookie, size_t cookie_len,
6146 const unsigned char *cli_id, size_t cli_id_len )
6147{
6148 ((void) ctx);
6149 ((void) cookie);
6150 ((void) cookie_len);
6151 ((void) cli_id);
6152 ((void) cli_id_len);
6153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006155}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006156#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006157
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006158/* Once ssl->out_hdr as the address of the beginning of the
6159 * next outgoing record is set, deduce the other pointers.
6160 *
6161 * Note: For TLS, we save the implicit record sequence number
6162 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6163 * and the caller has to make sure there's space for this.
6164 */
6165
6166static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6167 mbedtls_ssl_transform *transform )
6168{
6169#if defined(MBEDTLS_SSL_PROTO_DTLS)
6170 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6171 {
6172 ssl->out_ctr = ssl->out_hdr + 3;
6173 ssl->out_len = ssl->out_hdr + 11;
6174 ssl->out_iv = ssl->out_hdr + 13;
6175 }
6176 else
6177#endif
6178 {
6179 ssl->out_ctr = ssl->out_hdr - 8;
6180 ssl->out_len = ssl->out_hdr + 3;
6181 ssl->out_iv = ssl->out_hdr + 5;
6182 }
6183
6184 /* Adjust out_msg to make space for explicit IV, if used. */
6185 if( transform != NULL &&
6186 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6187 {
6188 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6189 }
6190 else
6191 ssl->out_msg = ssl->out_iv;
6192}
6193
6194/* Once ssl->in_hdr as the address of the beginning of the
6195 * next incoming record is set, deduce the other pointers.
6196 *
6197 * Note: For TLS, we save the implicit record sequence number
6198 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6199 * and the caller has to make sure there's space for this.
6200 */
6201
6202static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6203 mbedtls_ssl_transform *transform )
6204{
6205#if defined(MBEDTLS_SSL_PROTO_DTLS)
6206 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6207 {
6208 ssl->in_ctr = ssl->in_hdr + 3;
6209 ssl->in_len = ssl->in_hdr + 11;
6210 ssl->in_iv = ssl->in_hdr + 13;
6211 }
6212 else
6213#endif
6214 {
6215 ssl->in_ctr = ssl->in_hdr - 8;
6216 ssl->in_len = ssl->in_hdr + 3;
6217 ssl->in_iv = ssl->in_hdr + 5;
6218 }
6219
6220 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6221 if( transform != NULL &&
6222 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6223 {
6224 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6225 }
6226 else
6227 ssl->in_msg = ssl->in_iv;
6228}
6229
Paul Bakker5121ce52009-01-03 21:22:43 +00006230/*
6231 * Initialize an SSL context
6232 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006233void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6234{
6235 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6236}
6237
6238/*
6239 * Setup an SSL context
6240 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006241
6242static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6243{
6244 /* Set the incoming and outgoing record pointers. */
6245#if defined(MBEDTLS_SSL_PROTO_DTLS)
6246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6247 {
6248 ssl->out_hdr = ssl->out_buf;
6249 ssl->in_hdr = ssl->in_buf;
6250 }
6251 else
6252#endif /* MBEDTLS_SSL_PROTO_DTLS */
6253 {
6254 ssl->out_hdr = ssl->out_buf + 8;
6255 ssl->in_hdr = ssl->in_buf + 8;
6256 }
6257
6258 /* Derive other internal pointers. */
6259 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6260 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6261}
6262
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006263int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006264 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006265{
Paul Bakker48916f92012-09-16 19:57:18 +00006266 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006267
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006268 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006269
6270 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006271 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006272 */
Angus Grattond8213d02016-05-25 20:56:48 +10006273 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6274 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 {
Angus Grattond8213d02016-05-25 20:56:48 +10006276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6277 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6278 }
6279
6280 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6281 if( ssl->out_buf == NULL )
6282 {
6283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006285 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006286 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006287 }
6288
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006289 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006290
Paul Bakker48916f92012-09-16 19:57:18 +00006291 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6292 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006293
6294 return( 0 );
6295}
6296
6297/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006298 * Reset an initialized and used SSL context for re-use while retaining
6299 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006300 *
6301 * If partial is non-zero, keep data in the input buffer and client ID.
6302 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006303 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006304static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006305{
Paul Bakker48916f92012-09-16 19:57:18 +00006306 int ret;
6307
Hanno Becker7e772132018-08-10 12:38:21 +01006308#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6309 !defined(MBEDTLS_SSL_SRV_C)
6310 ((void) partial);
6311#endif
6312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006314
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006315 /* Cancel any possibly running timer */
6316 ssl_set_timer( ssl, 0 );
6317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#if defined(MBEDTLS_SSL_RENEGOTIATION)
6319 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006320 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006321
6322 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6324 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006327
Paul Bakker7eb013f2011-10-06 12:37:39 +00006328 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006329 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006330
6331 ssl->in_msgtype = 0;
6332 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006334 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006335 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006338 ssl_dtls_replay_reset( ssl );
6339#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006340
6341 ssl->in_hslen = 0;
6342 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006343
6344 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006345
6346 ssl->out_msgtype = 0;
6347 ssl->out_msglen = 0;
6348 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6350 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006351 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006352#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006353
Hanno Becker19859472018-08-06 09:40:20 +01006354 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6355
Paul Bakker48916f92012-09-16 19:57:18 +00006356 ssl->transform_in = NULL;
6357 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006358
Hanno Becker78640902018-08-13 16:35:15 +01006359 ssl->session_in = NULL;
6360 ssl->session_out = NULL;
6361
Angus Grattond8213d02016-05-25 20:56:48 +10006362 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006363
6364#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006365 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006366#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6367 {
6368 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006369 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006370 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6373 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6376 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6379 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006380 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006381 }
6382#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006383
Paul Bakker48916f92012-09-16 19:57:18 +00006384 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 mbedtls_ssl_transform_free( ssl->transform );
6387 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006388 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006389 }
Paul Bakker48916f92012-09-16 19:57:18 +00006390
Paul Bakkerc0463502013-02-14 11:19:38 +01006391 if( ssl->session )
6392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 mbedtls_ssl_session_free( ssl->session );
6394 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006395 ssl->session = NULL;
6396 }
6397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006399 ssl->alpn_chosen = NULL;
6400#endif
6401
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006402#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006403#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006404 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006405#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006406 {
6407 mbedtls_free( ssl->cli_id );
6408 ssl->cli_id = NULL;
6409 ssl->cli_id_len = 0;
6410 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006411#endif
6412
Paul Bakker48916f92012-09-16 19:57:18 +00006413 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6414 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006415
6416 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006417}
6418
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006419/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006420 * Reset an initialized and used SSL context for re-use while retaining
6421 * all application-set variables, function pointers and data.
6422 */
6423int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6424{
6425 return( ssl_session_reset_int( ssl, 0 ) );
6426}
6427
6428/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006429 * SSL set accessors
6430 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006431void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006433 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006434}
6435
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006436void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006437{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006438 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006439}
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006442void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006444 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006445}
6446#endif
6447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006449void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006451 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006452}
6453#endif
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006456
6457void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6458 unsigned allow_packing )
6459{
6460 ssl->disable_datagram_packing = !allow_packing;
6461}
6462
6463void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6464 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006465{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006466 conf->hs_timeout_min = min;
6467 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006468}
6469#endif
6470
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006471void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006472{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006473 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006474}
6475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006477void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006478 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006479 void *p_vrfy )
6480{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006481 conf->f_vrfy = f_vrfy;
6482 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006485
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006486void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006487 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006488 void *p_rng )
6489{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006490 conf->f_rng = f_rng;
6491 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006492}
6493
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006494void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006495 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 void *p_dbg )
6497{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006498 conf->f_dbg = f_dbg;
6499 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006500}
6501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006503 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006504 mbedtls_ssl_send_t *f_send,
6505 mbedtls_ssl_recv_t *f_recv,
6506 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006507{
6508 ssl->p_bio = p_bio;
6509 ssl->f_send = f_send;
6510 ssl->f_recv = f_recv;
6511 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006512}
6513
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006514#if defined(MBEDTLS_SSL_PROTO_DTLS)
6515void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6516{
6517 ssl->mtu = mtu;
6518}
6519#endif
6520
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006521void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006522{
6523 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006524}
6525
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006526void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6527 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006528 mbedtls_ssl_set_timer_t *f_set_timer,
6529 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006530{
6531 ssl->p_timer = p_timer;
6532 ssl->f_set_timer = f_set_timer;
6533 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006534
6535 /* Make sure we start with no timer running */
6536 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006537}
6538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006540void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006541 void *p_cache,
6542 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6543 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006544{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006545 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006546 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006547 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551#if defined(MBEDTLS_SSL_CLI_C)
6552int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006554 int ret;
6555
6556 if( ssl == NULL ||
6557 session == NULL ||
6558 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006559 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006562 }
6563
6564 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6565 return( ret );
6566
Paul Bakker0a597072012-09-25 21:55:46 +00006567 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006568
6569 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006572
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006573void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006574 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006576 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6577 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6578 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6579 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006580}
6581
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006582void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006583 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006584 int major, int minor )
6585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006587 return;
6588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006590 return;
6591
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006592 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006593}
6594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006596void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006597 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006598{
6599 conf->cert_profile = profile;
6600}
6601
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006602/* Append a new keycert entry to a (possibly empty) list */
6603static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6604 mbedtls_x509_crt *cert,
6605 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006606{
niisato8ee24222018-06-25 19:05:48 +09006607 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006608
niisato8ee24222018-06-25 19:05:48 +09006609 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6610 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006611 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006612
niisato8ee24222018-06-25 19:05:48 +09006613 new_cert->cert = cert;
6614 new_cert->key = key;
6615 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006616
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006617 /* Update head is the list was null, else add to the end */
6618 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006619 {
niisato8ee24222018-06-25 19:05:48 +09006620 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006621 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006622 else
6623 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006624 mbedtls_ssl_key_cert *cur = *head;
6625 while( cur->next != NULL )
6626 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006627 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006628 }
6629
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006630 return( 0 );
6631}
6632
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006633int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006634 mbedtls_x509_crt *own_cert,
6635 mbedtls_pk_context *pk_key )
6636{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006637 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006638}
6639
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006640void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006641 mbedtls_x509_crt *ca_chain,
6642 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006643{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006644 conf->ca_chain = ca_chain;
6645 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006648
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006649#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6650int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6651 mbedtls_x509_crt *own_cert,
6652 mbedtls_pk_context *pk_key )
6653{
6654 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6655 own_cert, pk_key ) );
6656}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006657
6658void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6659 mbedtls_x509_crt *ca_chain,
6660 mbedtls_x509_crl *ca_crl )
6661{
6662 ssl->handshake->sni_ca_chain = ca_chain;
6663 ssl->handshake->sni_ca_crl = ca_crl;
6664}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006665
6666void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6667 int authmode )
6668{
6669 ssl->handshake->sni_authmode = authmode;
6670}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006671#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6672
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006673#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006674/*
6675 * Set EC J-PAKE password for current handshake
6676 */
6677int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6678 const unsigned char *pw,
6679 size_t pw_len )
6680{
6681 mbedtls_ecjpake_role role;
6682
Janos Follath8eb64132016-06-03 15:40:57 +01006683 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6685
6686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6687 role = MBEDTLS_ECJPAKE_SERVER;
6688 else
6689 role = MBEDTLS_ECJPAKE_CLIENT;
6690
6691 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6692 role,
6693 MBEDTLS_MD_SHA256,
6694 MBEDTLS_ECP_DP_SECP256R1,
6695 pw, pw_len ) );
6696}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006697#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006700int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006701 const unsigned char *psk, size_t psk_len,
6702 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006703{
Paul Bakker6db455e2013-09-18 17:29:31 +02006704 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6708 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006709
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006710 /* Identity len will be encoded on two bytes */
6711 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006712 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006713 {
6714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6715 }
6716
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006717 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006718 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006719 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006720
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006721 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006722 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006723 conf->psk_len = 0;
6724 }
6725 if( conf->psk_identity != NULL )
6726 {
6727 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006728 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006729 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006730 }
6731
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006732 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6733 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006734 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006735 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006736 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006737 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006738 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006739 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006740 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006741
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006742 conf->psk_len = psk_len;
6743 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006744
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006745 memcpy( conf->psk, psk, conf->psk_len );
6746 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006747
6748 return( 0 );
6749}
6750
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006751int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6752 const unsigned char *psk, size_t psk_len )
6753{
6754 if( psk == NULL || ssl->handshake == NULL )
6755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6756
6757 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6759
6760 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006761 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006762 mbedtls_platform_zeroize( ssl->handshake->psk,
6763 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006764 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006765 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006766 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006767
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006768 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006769 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006770
6771 ssl->handshake->psk_len = psk_len;
6772 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6773
6774 return( 0 );
6775}
6776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006777void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006779 size_t),
6780 void *p_psk )
6781{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006782 conf->f_psk = f_psk;
6783 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006786
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006787#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006788
6789#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006790int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006791{
6792 int ret;
6793
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006794 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6795 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6796 {
6797 mbedtls_mpi_free( &conf->dhm_P );
6798 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006799 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006800 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006801
6802 return( 0 );
6803}
Hanno Becker470a8c42017-10-04 15:28:46 +01006804#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006805
Hanno Beckera90658f2017-10-04 15:29:08 +01006806int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6807 const unsigned char *dhm_P, size_t P_len,
6808 const unsigned char *dhm_G, size_t G_len )
6809{
6810 int ret;
6811
6812 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6813 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6814 {
6815 mbedtls_mpi_free( &conf->dhm_P );
6816 mbedtls_mpi_free( &conf->dhm_G );
6817 return( ret );
6818 }
6819
6820 return( 0 );
6821}
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006823int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006824{
6825 int ret;
6826
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006827 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6828 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6829 {
6830 mbedtls_mpi_free( &conf->dhm_P );
6831 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006832 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006833 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006834
6835 return( 0 );
6836}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006837#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006838
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006839#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6840/*
6841 * Set the minimum length for Diffie-Hellman parameters
6842 */
6843void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6844 unsigned int bitlen )
6845{
6846 conf->dhm_min_bitlen = bitlen;
6847}
6848#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6849
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006850#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006851/*
6852 * Set allowed/preferred hashes for handshake signatures
6853 */
6854void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6855 const int *hashes )
6856{
6857 conf->sig_hashes = hashes;
6858}
Hanno Becker947194e2017-04-07 13:25:49 +01006859#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006860
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006861#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006862/*
6863 * Set the allowed elliptic curves
6864 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006865void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006866 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006868 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006869}
Hanno Becker947194e2017-04-07 13:25:49 +01006870#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006871
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006872#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006874{
Hanno Becker947194e2017-04-07 13:25:49 +01006875 /* Initialize to suppress unnecessary compiler warning */
6876 size_t hostname_len = 0;
6877
6878 /* Check if new hostname is valid before
6879 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006880 if( hostname != NULL )
6881 {
6882 hostname_len = strlen( hostname );
6883
6884 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6886 }
6887
6888 /* Now it's clear that we will overwrite the old hostname,
6889 * so we can free it safely */
6890
6891 if( ssl->hostname != NULL )
6892 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006893 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006894 mbedtls_free( ssl->hostname );
6895 }
6896
6897 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006898
Paul Bakker5121ce52009-01-03 21:22:43 +00006899 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006900 {
6901 ssl->hostname = NULL;
6902 }
6903 else
6904 {
6905 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006906 if( ssl->hostname == NULL )
6907 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006908
Hanno Becker947194e2017-04-07 13:25:49 +01006909 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006910
Hanno Becker947194e2017-04-07 13:25:49 +01006911 ssl->hostname[hostname_len] = '\0';
6912 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006913
6914 return( 0 );
6915}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006916#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006917
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006918#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006919void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006921 const unsigned char *, size_t),
6922 void *p_sni )
6923{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006924 conf->f_sni = f_sni;
6925 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006930int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006931{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006932 size_t cur_len, tot_len;
6933 const char **p;
6934
6935 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006936 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6937 * MUST NOT be truncated."
6938 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006939 */
6940 tot_len = 0;
6941 for( p = protos; *p != NULL; p++ )
6942 {
6943 cur_len = strlen( *p );
6944 tot_len += cur_len;
6945
6946 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006948 }
6949
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006950 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006951
6952 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006953}
6954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006956{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006957 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006960
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006961void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006962{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006963 conf->max_major_ver = major;
6964 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006965}
6966
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006967void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006968{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006969 conf->min_major_ver = major;
6970 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006971}
6972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006974void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006975{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006976 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006977}
6978#endif
6979
Janos Follath088ce432017-04-10 12:42:31 +01006980#if defined(MBEDTLS_SSL_SRV_C)
6981void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6982 char cert_req_ca_list )
6983{
6984 conf->cert_req_ca_list = cert_req_ca_list;
6985}
6986#endif
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006989void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006990{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006991 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006992}
6993#endif
6994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006996void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006997{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006998 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006999}
7000#endif
7001
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007002#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007003void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007004{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007005 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007006}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007007#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007010int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007011{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007013 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007016 }
7017
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007018 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007019
7020 return( 0 );
7021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007025void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007026{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007027 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007028}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007032void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007033{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007034 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007035}
7036#endif
7037
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007038void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007039{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007040 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007041}
7042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007044void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007045{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007046 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007047}
7048
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007049void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007050{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007051 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007052}
7053
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007054void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007055 const unsigned char period[8] )
7056{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007057 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007062#if defined(MBEDTLS_SSL_CLI_C)
7063void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007064{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007065 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007066}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007067#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007068
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007069#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007070void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7071 mbedtls_ssl_ticket_write_t *f_ticket_write,
7072 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7073 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007074{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007075 conf->f_ticket_write = f_ticket_write;
7076 conf->f_ticket_parse = f_ticket_parse;
7077 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007078}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007081
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007082#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7083void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7084 mbedtls_ssl_export_keys_t *f_export_keys,
7085 void *p_export_keys )
7086{
7087 conf->f_export_keys = f_export_keys;
7088 conf->p_export_keys = p_export_keys;
7089}
7090#endif
7091
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007092#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007093void mbedtls_ssl_conf_async_private_cb(
7094 mbedtls_ssl_config *conf,
7095 mbedtls_ssl_async_sign_t *f_async_sign,
7096 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7097 mbedtls_ssl_async_resume_t *f_async_resume,
7098 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007099 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007100{
7101 conf->f_async_sign_start = f_async_sign;
7102 conf->f_async_decrypt_start = f_async_decrypt;
7103 conf->f_async_resume = f_async_resume;
7104 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007105 conf->p_async_config_data = async_config_data;
7106}
7107
Gilles Peskine8f97af72018-04-26 11:46:10 +02007108void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7109{
7110 return( conf->p_async_config_data );
7111}
7112
Gilles Peskine1febfef2018-04-30 11:54:39 +02007113void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007114{
7115 if( ssl->handshake == NULL )
7116 return( NULL );
7117 else
7118 return( ssl->handshake->user_async_ctx );
7119}
7120
Gilles Peskine1febfef2018-04-30 11:54:39 +02007121void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007122 void *ctx )
7123{
7124 if( ssl->handshake != NULL )
7125 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007126}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007127#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007128
Paul Bakker5121ce52009-01-03 21:22:43 +00007129/*
7130 * SSL get accessors
7131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007133{
7134 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7135}
7136
Hanno Becker8b170a02017-10-10 11:51:19 +01007137int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7138{
7139 /*
7140 * Case A: We're currently holding back
7141 * a message for further processing.
7142 */
7143
7144 if( ssl->keep_current_message == 1 )
7145 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007147 return( 1 );
7148 }
7149
7150 /*
7151 * Case B: Further records are pending in the current datagram.
7152 */
7153
7154#if defined(MBEDTLS_SSL_PROTO_DTLS)
7155 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7156 ssl->in_left > ssl->next_record_offset )
7157 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007159 return( 1 );
7160 }
7161#endif /* MBEDTLS_SSL_PROTO_DTLS */
7162
7163 /*
7164 * Case C: A handshake message is being processed.
7165 */
7166
Hanno Becker8b170a02017-10-10 11:51:19 +01007167 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7168 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007170 return( 1 );
7171 }
7172
7173 /*
7174 * Case D: An application data message is being processed
7175 */
7176 if( ssl->in_offt != NULL )
7177 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007179 return( 1 );
7180 }
7181
7182 /*
7183 * In all other cases, the rest of the message can be dropped.
7184 * As in ssl_read_record_layer, this needs to be adapted if
7185 * we implement support for multiple alerts in single records.
7186 */
7187
7188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7189 return( 0 );
7190}
7191
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007192uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007193{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007194 if( ssl->session != NULL )
7195 return( ssl->session->verify_result );
7196
7197 if( ssl->session_negotiate != NULL )
7198 return( ssl->session_negotiate->verify_result );
7199
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007200 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007201}
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007204{
Paul Bakker926c8e42013-03-06 10:23:34 +01007205 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007206 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007209}
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007215 {
7216 switch( ssl->minor_ver )
7217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007219 return( "DTLSv1.0" );
7220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007222 return( "DTLSv1.2" );
7223
7224 default:
7225 return( "unknown (DTLS)" );
7226 }
7227 }
7228#endif
7229
Paul Bakker43ca69c2011-01-15 17:35:19 +00007230 switch( ssl->minor_ver )
7231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007233 return( "SSLv3.0" );
7234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007236 return( "TLSv1.0" );
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007239 return( "TLSv1.1" );
7240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007242 return( "TLSv1.2" );
7243
Paul Bakker43ca69c2011-01-15 17:35:19 +00007244 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007245 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007246 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007247}
7248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007250{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007251 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007253 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007254
Hanno Becker78640902018-08-13 16:35:15 +01007255 if( transform == NULL )
7256 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_ZLIB_SUPPORT)
7259 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7260 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007261#endif
7262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 case MBEDTLS_MODE_GCM:
7266 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007267 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007269 transform_expansion = transform->minlen;
7270 break;
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007273
7274 block_size = mbedtls_cipher_get_block_size(
7275 &transform->cipher_ctx_enc );
7276
7277#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7278 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7279 {
7280 /* Expansion due to addition of
7281 * - MAC
7282 * - CBC padding (theoretically up to 256 bytes, but
7283 * we never use more than block_size)
7284 * - explicit IV
7285 */
7286 transform_expansion = transform->maclen + 2 * block_size;
7287 }
7288 else
7289#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7290 {
7291 /* No explicit IV prior to TLS 1.1. */
7292 transform_expansion = transform->maclen + block_size;
7293 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007294 break;
7295
7296 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007299 }
7300
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007301 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007302}
7303
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007304#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7305size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7306{
7307 size_t max_len;
7308
7309 /*
7310 * Assume mfl_code is correct since it was checked when set
7311 */
Angus Grattond8213d02016-05-25 20:56:48 +10007312 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007313
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007314 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007315 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007316 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007317 {
Angus Grattond8213d02016-05-25 20:56:48 +10007318 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007319 }
7320
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007321 /* During a handshake, use the value being negotiated */
7322 if( ssl->session_negotiate != NULL &&
7323 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7324 {
7325 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7326 }
7327
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007328 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007329}
7330#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7331
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007332#if defined(MBEDTLS_SSL_PROTO_DTLS)
7333static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7334{
7335 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7336 return( ssl->mtu );
7337
7338 if( ssl->mtu == 0 )
7339 return( ssl->handshake->mtu );
7340
7341 return( ssl->mtu < ssl->handshake->mtu ?
7342 ssl->mtu : ssl->handshake->mtu );
7343}
7344#endif /* MBEDTLS_SSL_PROTO_DTLS */
7345
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007346int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7347{
7348 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7349
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007350#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7351 !defined(MBEDTLS_SSL_PROTO_DTLS)
7352 (void) ssl;
7353#endif
7354
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007355#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7356 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7357
7358 if( max_len > mfl )
7359 max_len = mfl;
7360#endif
7361
7362#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007363 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007364 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007365 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007366 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7367 const size_t overhead = (size_t) ret;
7368
7369 if( ret < 0 )
7370 return( ret );
7371
7372 if( mtu <= overhead )
7373 {
7374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7375 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7376 }
7377
7378 if( max_len > mtu - overhead )
7379 max_len = mtu - overhead;
7380 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007381#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007382
Hanno Becker0defedb2018-08-10 12:35:02 +01007383#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7384 !defined(MBEDTLS_SSL_PROTO_DTLS)
7385 ((void) ssl);
7386#endif
7387
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007388 return( (int) max_len );
7389}
7390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391#if defined(MBEDTLS_X509_CRT_PARSE_C)
7392const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007393{
7394 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007395 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007396
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007397 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#if defined(MBEDTLS_SSL_CLI_C)
7402int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007403{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007404 if( ssl == NULL ||
7405 dst == NULL ||
7406 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007407 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007410 }
7411
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007412 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007415
Paul Bakker5121ce52009-01-03 21:22:43 +00007416/*
Paul Bakker1961b702013-01-25 14:49:24 +01007417 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007422
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007423 if( ssl == NULL || ssl->conf == NULL )
7424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007427 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007433#endif
7434
Paul Bakker1961b702013-01-25 14:49:24 +01007435 return( ret );
7436}
7437
7438/*
7439 * Perform the SSL handshake
7440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007442{
7443 int ret = 0;
7444
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007445 if( ssl == NULL || ssl->conf == NULL )
7446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007453
7454 if( ret != 0 )
7455 break;
7456 }
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007459
7460 return( ret );
7461}
7462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#if defined(MBEDTLS_SSL_RENEGOTIATION)
7464#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007465/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007466 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007469{
7470 int ret;
7471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007473
7474 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7476 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007477
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007478 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007479 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007481 return( ret );
7482 }
7483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007485
7486 return( 0 );
7487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007489
7490/*
7491 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 * - any side: calling mbedtls_ssl_renegotiate(),
7493 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7494 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007495 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007496 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007500{
7501 int ret;
7502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007504
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007505 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7506 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007507
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007508 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7509 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007513 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007514 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007515 ssl->handshake->out_msg_seq = 1;
7516 else
7517 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007518 }
7519#endif
7520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7522 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007527 return( ret );
7528 }
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007531
7532 return( 0 );
7533}
7534
7535/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007536 * Renegotiate current connection on client,
7537 * or request renegotiation on server
7538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007540{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007542
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007543 if( ssl == NULL || ssl->conf == NULL )
7544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007547 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007548 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007554
7555 /* Did we already try/start sending HelloRequest? */
7556 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007558
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007559 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007564 /*
7565 * On client, either start the renegotiation process or,
7566 * if already in progress, continue the handshake
7567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007572
7573 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007576 return( ret );
7577 }
7578 }
7579 else
7580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007584 return( ret );
7585 }
7586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007588
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007589 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007590}
7591
7592/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007593 * Check record counters and renegotiate if they're above the limit.
7594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007596{
Andres AG2196c7f2016-12-15 17:01:16 +00007597 size_t ep_len = ssl_ep_len( ssl );
7598 int in_ctr_cmp;
7599 int out_ctr_cmp;
7600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7602 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007603 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007604 {
7605 return( 0 );
7606 }
7607
Andres AG2196c7f2016-12-15 17:01:16 +00007608 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7609 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007610 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007611 ssl->conf->renego_period + ep_len, 8 - ep_len );
7612
7613 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007614 {
7615 return( 0 );
7616 }
7617
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007622
7623/*
7624 * Receive application data decrypted from the SSL layer
7625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007627{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007628 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007629 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007630
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007631 if( ssl == NULL || ssl->conf == NULL )
7632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007637 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640 return( ret );
7641
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007642 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007644 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007645 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007646 return( ret );
7647 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007648 }
7649#endif
7650
Hanno Becker4a810fb2017-05-24 16:27:30 +01007651 /*
7652 * Check if renegotiation is necessary and/or handshake is
7653 * in process. If yes, perform/continue, and fall through
7654 * if an unexpected packet is received while the client
7655 * is waiting for the ServerHello.
7656 *
7657 * (There is no equivalent to the last condition on
7658 * the server-side as it is not treated as within
7659 * a handshake while waiting for the ClientHello
7660 * after a renegotiation request.)
7661 */
7662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007664 ret = ssl_check_ctr_renegotiate( ssl );
7665 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7666 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007669 return( ret );
7670 }
7671#endif
7672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007676 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7677 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007680 return( ret );
7681 }
7682 }
7683
Hanno Beckere41158b2017-10-23 13:30:32 +01007684 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007685 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007686 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007687 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007688 if( ssl->f_get_timer != NULL &&
7689 ssl->f_get_timer( ssl->p_timer ) == -1 )
7690 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007691 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007692 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007693
Hanno Becker4a810fb2017-05-24 16:27:30 +01007694 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007695 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007696 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7697 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007698
Hanno Becker4a810fb2017-05-24 16:27:30 +01007699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7700 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007701 }
7702
7703 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007705 {
7706 /*
7707 * OpenSSL sends empty messages to randomize the IV
7708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007712 return( 0 );
7713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 return( ret );
7716 }
7717 }
7718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007722
Hanno Becker4a810fb2017-05-24 16:27:30 +01007723 /*
7724 * - For client-side, expect SERVER_HELLO_REQUEST.
7725 * - For server-side, expect CLIENT_HELLO.
7726 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7727 */
7728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007730 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007732 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007735
7736 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007739 {
7740 continue;
7741 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007744 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007745#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007746
Hanno Becker4a810fb2017-05-24 16:27:30 +01007747#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007748 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007752
7753 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007756 {
7757 continue;
7758 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007761 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007762#endif /* MBEDTLS_SSL_SRV_C */
7763
Hanno Becker21df7f92017-10-17 11:03:26 +01007764#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007765 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007766 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7767 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7768 ssl->conf->allow_legacy_renegotiation ==
7769 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7770 {
7771 /*
7772 * Accept renegotiation request
7773 */
Paul Bakker48916f92012-09-16 19:57:18 +00007774
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007775 /* DTLS clients need to know renego is server-initiated */
7776#if defined(MBEDTLS_SSL_PROTO_DTLS)
7777 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7778 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7779 {
7780 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7781 }
7782#endif
7783 ret = ssl_start_renegotiation( ssl );
7784 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7785 ret != 0 )
7786 {
7787 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7788 return( ret );
7789 }
7790 }
7791 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007792#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007793 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007794 /*
7795 * Refuse renegotiation
7796 */
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800#if defined(MBEDTLS_SSL_PROTO_SSL3)
7801 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007802 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007803 /* SSLv3 does not have a "no_renegotiation" warning, so
7804 we send a fatal alert and abort the connection. */
7805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7806 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7807 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007808 }
7809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7811#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7812 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7813 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7816 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7817 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007818 {
7819 return( ret );
7820 }
Paul Bakker48916f92012-09-16 19:57:18 +00007821 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7824 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7827 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007828 }
Paul Bakker48916f92012-09-16 19:57:18 +00007829 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007830
Hanno Becker90333da2017-10-10 11:27:13 +01007831 /* At this point, we don't know whether the renegotiation has been
7832 * completed or not. The cases to consider are the following:
7833 * 1) The renegotiation is complete. In this case, no new record
7834 * has been read yet.
7835 * 2) The renegotiation is incomplete because the client received
7836 * an application data record while awaiting the ServerHello.
7837 * 3) The renegotiation is incomplete because the client received
7838 * a non-handshake, non-application data message while awaiting
7839 * the ServerHello.
7840 * In each of these case, looping will be the proper action:
7841 * - For 1), the next iteration will read a new record and check
7842 * if it's application data.
7843 * - For 2), the loop condition isn't satisfied as application data
7844 * is present, hence continue is the same as break
7845 * - For 3), the loop condition is satisfied and read_record
7846 * will re-deliver the message that was held back by the client
7847 * when expecting the ServerHello.
7848 */
7849 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007850 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007851#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007853 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007854 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007855 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007856 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007859 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007861 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007862 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007863 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7867 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007870 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007871 }
7872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7876 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007877 }
7878
7879 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007880
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007881 /* We're going to return something now, cancel timer,
7882 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007884 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007885
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007886#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007887 /* If we requested renego but received AppData, resend HelloRequest.
7888 * Do it now, after setting in_offt, to avoid taking this branch
7889 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007891 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007893 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007894 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007897 return( ret );
7898 }
7899 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007901#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007902 }
7903
7904 n = ( len < ssl->in_msglen )
7905 ? len : ssl->in_msglen;
7906
7907 memcpy( buf, ssl->in_offt, n );
7908 ssl->in_msglen -= n;
7909
7910 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007911 {
7912 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007913 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007914 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007916 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007917 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007918 /* more data available */
7919 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007923
Paul Bakker23986e52011-04-24 08:57:21 +00007924 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007925}
7926
7927/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007928 * Send application data to be encrypted by the SSL layer, taking care of max
7929 * fragment length and buffer size.
7930 *
7931 * According to RFC 5246 Section 6.2.1:
7932 *
7933 * Zero-length fragments of Application data MAY be sent as they are
7934 * potentially useful as a traffic analysis countermeasure.
7935 *
7936 * Therefore, it is possible that the input message length is 0 and the
7937 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007938 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007939static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007940 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007941{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007942 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7943 const size_t max_len = (size_t) ret;
7944
7945 if( ret < 0 )
7946 {
7947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7948 return( ret );
7949 }
7950
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007951 if( len > max_len )
7952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007954 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007957 "maximum fragment length: %d > %d",
7958 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007960 }
7961 else
7962#endif
7963 len = max_len;
7964 }
Paul Bakker887bd502011-06-08 13:10:54 +00007965
Paul Bakker5121ce52009-01-03 21:22:43 +00007966 if( ssl->out_left != 0 )
7967 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007968 /*
7969 * The user has previously tried to send the data and
7970 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7971 * written. In this case, we expect the high-level write function
7972 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007977 return( ret );
7978 }
7979 }
Paul Bakker887bd502011-06-08 13:10:54 +00007980 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007981 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007982 /*
7983 * The user is trying to send a message the first time, so we need to
7984 * copy the data into the internal buffers and setup the data structure
7985 * to keep track of partial writes
7986 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007987 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007989 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007990
Hanno Becker67bc7c32018-08-06 11:33:50 +01007991 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007994 return( ret );
7995 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007996 }
7997
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007998 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007999}
8000
8001/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008002 * Write application data, doing 1/n-1 splitting if necessary.
8003 *
8004 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008005 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008006 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008009static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008010 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008011{
8012 int ret;
8013
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008014 if( ssl->conf->cbc_record_splitting ==
8015 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008016 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8018 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8019 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008020 {
8021 return( ssl_write_real( ssl, buf, len ) );
8022 }
8023
8024 if( ssl->split_done == 0 )
8025 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008026 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008027 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008028 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008029 }
8030
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008031 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8032 return( ret );
8033 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008034
8035 return( ret + 1 );
8036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008038
8039/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008040 * Write application data (public-facing wrapper)
8041 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008042int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008043{
8044 int ret;
8045
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008047
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008048 if( ssl == NULL || ssl->conf == NULL )
8049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8050
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008051#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008052 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8053 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008054 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008055 return( ret );
8056 }
8057#endif
8058
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008059 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008060 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008061 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008062 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008064 return( ret );
8065 }
8066 }
8067
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008068#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008069 ret = ssl_write_split( ssl, buf, len );
8070#else
8071 ret = ssl_write_real( ssl, buf, len );
8072#endif
8073
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008075
8076 return( ret );
8077}
8078
8079/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008080 * Notify the peer that the connection is being closed
8081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008083{
8084 int ret;
8085
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008086 if( ssl == NULL || ssl->conf == NULL )
8087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008090
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008091 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8097 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8098 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008101 return( ret );
8102 }
8103 }
8104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008106
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008107 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008108}
8109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008111{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008112 if( transform == NULL )
8113 return;
8114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008116 deflateEnd( &transform->ctx_deflate );
8117 inflateEnd( &transform->ctx_inflate );
8118#endif
8119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8121 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 mbedtls_md_free( &transform->md_ctx_enc );
8124 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008125
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008126 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008127}
8128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129#if defined(MBEDTLS_X509_CRT_PARSE_C)
8130static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008133
8134 while( cur != NULL )
8135 {
8136 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008138 cur = next;
8139 }
8140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008142
Gilles Peskine9b562d52018-04-25 20:32:43 +02008143void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008144{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008145 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8146
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147 if( handshake == NULL )
8148 return;
8149
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008150#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8151 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8152 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008153 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008154 handshake->async_in_progress = 0;
8155 }
8156#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8157
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008158#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8159 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8160 mbedtls_md5_free( &handshake->fin_md5 );
8161 mbedtls_sha1_free( &handshake->fin_sha1 );
8162#endif
8163#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8164#if defined(MBEDTLS_SHA256_C)
8165 mbedtls_sha256_free( &handshake->fin_sha256 );
8166#endif
8167#if defined(MBEDTLS_SHA512_C)
8168 mbedtls_sha512_free( &handshake->fin_sha512 );
8169#endif
8170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008172#if defined(MBEDTLS_DHM_C)
8173 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_ECDH_C)
8176 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008177#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008178#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008179 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008180#if defined(MBEDTLS_SSL_CLI_C)
8181 mbedtls_free( handshake->ecjpake_cache );
8182 handshake->ecjpake_cache = NULL;
8183 handshake->ecjpake_cache_len = 0;
8184#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008185#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008186
Janos Follath4ae5c292016-02-10 11:27:43 +00008187#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8188 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008189 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008191#endif
8192
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008193#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8194 if( handshake->psk != NULL )
8195 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008196 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008197 mbedtls_free( handshake->psk );
8198 }
8199#endif
8200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8202 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008203 /*
8204 * Free only the linked list wrapper, not the keys themselves
8205 * since the belong to the SNI callback
8206 */
8207 if( handshake->sni_key_cert != NULL )
8208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008210
8211 while( cur != NULL )
8212 {
8213 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008215 cur = next;
8216 }
8217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#if defined(MBEDTLS_SSL_PROTO_DTLS)
8221 mbedtls_free( handshake->verify_cookie );
8222 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008223 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008224#endif
8225
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008226 mbedtls_platform_zeroize( handshake,
8227 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008228}
8229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008231{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008232 if( session == NULL )
8233 return;
8234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008236 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 mbedtls_x509_crt_free( session->peer_cert );
8239 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008240 }
Paul Bakkered27a042013-04-18 22:46:23 +02008241#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008242
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008243#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008245#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008246
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008247 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008248}
8249
Paul Bakker5121ce52009-01-03 21:22:43 +00008250/*
8251 * Free an SSL context
8252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008254{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008255 if( ssl == NULL )
8256 return;
8257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008259
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008260 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008261 {
Angus Grattond8213d02016-05-25 20:56:48 +10008262 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008264 }
8265
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008266 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008267 {
Angus Grattond8213d02016-05-25 20:56:48 +10008268 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008270 }
8271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008273 if( ssl->compress_buf != NULL )
8274 {
Angus Grattond8213d02016-05-25 20:56:48 +10008275 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008277 }
8278#endif
8279
Paul Bakker48916f92012-09-16 19:57:18 +00008280 if( ssl->transform )
8281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 mbedtls_ssl_transform_free( ssl->transform );
8283 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008284 }
8285
8286 if( ssl->handshake )
8287 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008288 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8290 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 mbedtls_free( ssl->handshake );
8293 mbedtls_free( ssl->transform_negotiate );
8294 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008295 }
8296
Paul Bakkerc0463502013-02-14 11:19:38 +01008297 if( ssl->session )
8298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 mbedtls_ssl_session_free( ssl->session );
8300 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008301 }
8302
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008303#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008304 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008305 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008306 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008308 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008309#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8312 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8315 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008316 }
8317#endif
8318
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008319#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008321#endif
8322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008324
Paul Bakker86f04f42013-02-14 11:20:09 +01008325 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008326 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008327}
8328
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008329/*
8330 * Initialze mbedtls_ssl_config
8331 */
8332void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8333{
8334 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8335}
8336
Simon Butcherc97b6972015-12-27 23:48:17 +00008337#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008338static int ssl_preset_default_hashes[] = {
8339#if defined(MBEDTLS_SHA512_C)
8340 MBEDTLS_MD_SHA512,
8341 MBEDTLS_MD_SHA384,
8342#endif
8343#if defined(MBEDTLS_SHA256_C)
8344 MBEDTLS_MD_SHA256,
8345 MBEDTLS_MD_SHA224,
8346#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008347#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008348 MBEDTLS_MD_SHA1,
8349#endif
8350 MBEDTLS_MD_NONE
8351};
Simon Butcherc97b6972015-12-27 23:48:17 +00008352#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008353
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008354static int ssl_preset_suiteb_ciphersuites[] = {
8355 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8356 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8357 0
8358};
8359
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008360#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008361static int ssl_preset_suiteb_hashes[] = {
8362 MBEDTLS_MD_SHA256,
8363 MBEDTLS_MD_SHA384,
8364 MBEDTLS_MD_NONE
8365};
8366#endif
8367
8368#if defined(MBEDTLS_ECP_C)
8369static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8370 MBEDTLS_ECP_DP_SECP256R1,
8371 MBEDTLS_ECP_DP_SECP384R1,
8372 MBEDTLS_ECP_DP_NONE
8373};
8374#endif
8375
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008376/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008377 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008378 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008379int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008380 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008381{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008382#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008383 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008384#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008385
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008386 /* Use the functions here so that they are covered in tests,
8387 * but otherwise access member directly for efficiency */
8388 mbedtls_ssl_conf_endpoint( conf, endpoint );
8389 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008390
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008391 /*
8392 * Things that are common to all presets
8393 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008394#if defined(MBEDTLS_SSL_CLI_C)
8395 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8396 {
8397 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8398#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8399 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8400#endif
8401 }
8402#endif
8403
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008404#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008405 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008406#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008407
8408#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8409 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8410#endif
8411
8412#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8413 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8414#endif
8415
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008416#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8417 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8418#endif
8419
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008420#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008421 conf->f_cookie_write = ssl_cookie_write_dummy;
8422 conf->f_cookie_check = ssl_cookie_check_dummy;
8423#endif
8424
8425#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8426 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8427#endif
8428
Janos Follath088ce432017-04-10 12:42:31 +01008429#if defined(MBEDTLS_SSL_SRV_C)
8430 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8431#endif
8432
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008433#if defined(MBEDTLS_SSL_PROTO_DTLS)
8434 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8435 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8436#endif
8437
8438#if defined(MBEDTLS_SSL_RENEGOTIATION)
8439 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008440 memset( conf->renego_period, 0x00, 2 );
8441 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008442#endif
8443
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008444#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8445 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8446 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008447 const unsigned char dhm_p[] =
8448 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8449 const unsigned char dhm_g[] =
8450 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8451
Hanno Beckera90658f2017-10-04 15:29:08 +01008452 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8453 dhm_p, sizeof( dhm_p ),
8454 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008455 {
8456 return( ret );
8457 }
8458 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008459#endif
8460
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008461 /*
8462 * Preset-specific defaults
8463 */
8464 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008465 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008466 /*
8467 * NSA Suite B
8468 */
8469 case MBEDTLS_SSL_PRESET_SUITEB:
8470 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8471 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8472 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8473 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8474
8475 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8476 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8477 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8478 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8479 ssl_preset_suiteb_ciphersuites;
8480
8481#if defined(MBEDTLS_X509_CRT_PARSE_C)
8482 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008483#endif
8484
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008485#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008486 conf->sig_hashes = ssl_preset_suiteb_hashes;
8487#endif
8488
8489#if defined(MBEDTLS_ECP_C)
8490 conf->curve_list = ssl_preset_suiteb_curves;
8491#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008492 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008493
8494 /*
8495 * Default
8496 */
8497 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008498 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8499 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8500 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8501 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8502 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8503 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8504 MBEDTLS_SSL_MIN_MINOR_VERSION :
8505 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008506 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8507 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8508
8509#if defined(MBEDTLS_SSL_PROTO_DTLS)
8510 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8511 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8512#endif
8513
8514 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8515 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8516 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8517 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8518 mbedtls_ssl_list_ciphersuites();
8519
8520#if defined(MBEDTLS_X509_CRT_PARSE_C)
8521 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8522#endif
8523
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008524#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008525 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008526#endif
8527
8528#if defined(MBEDTLS_ECP_C)
8529 conf->curve_list = mbedtls_ecp_grp_id_list();
8530#endif
8531
8532#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8533 conf->dhm_min_bitlen = 1024;
8534#endif
8535 }
8536
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008537 return( 0 );
8538}
8539
8540/*
8541 * Free mbedtls_ssl_config
8542 */
8543void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8544{
8545#if defined(MBEDTLS_DHM_C)
8546 mbedtls_mpi_free( &conf->dhm_P );
8547 mbedtls_mpi_free( &conf->dhm_G );
8548#endif
8549
8550#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8551 if( conf->psk != NULL )
8552 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008553 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008554 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008555 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008556 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008557 }
8558
8559 if( conf->psk_identity != NULL )
8560 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008561 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008562 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008563 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008564 conf->psk_identity_len = 0;
8565 }
8566#endif
8567
8568#if defined(MBEDTLS_X509_CRT_PARSE_C)
8569 ssl_key_cert_free( conf->key_cert );
8570#endif
8571
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008572 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008573}
8574
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008575#if defined(MBEDTLS_PK_C) && \
8576 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008577/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#if defined(MBEDTLS_RSA_C)
8583 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8584 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586#if defined(MBEDTLS_ECDSA_C)
8587 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8588 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008591}
8592
Hanno Becker7e5437a2017-04-28 17:15:26 +01008593unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8594{
8595 switch( type ) {
8596 case MBEDTLS_PK_RSA:
8597 return( MBEDTLS_SSL_SIG_RSA );
8598 case MBEDTLS_PK_ECDSA:
8599 case MBEDTLS_PK_ECKEY:
8600 return( MBEDTLS_SSL_SIG_ECDSA );
8601 default:
8602 return( MBEDTLS_SSL_SIG_ANON );
8603 }
8604}
8605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008607{
8608 switch( sig )
8609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#if defined(MBEDTLS_RSA_C)
8611 case MBEDTLS_SSL_SIG_RSA:
8612 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614#if defined(MBEDTLS_ECDSA_C)
8615 case MBEDTLS_SSL_SIG_ECDSA:
8616 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008617#endif
8618 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008620 }
8621}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008622#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008623
Hanno Becker7e5437a2017-04-28 17:15:26 +01008624#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8625 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8626
8627/* Find an entry in a signature-hash set matching a given hash algorithm. */
8628mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8629 mbedtls_pk_type_t sig_alg )
8630{
8631 switch( sig_alg )
8632 {
8633 case MBEDTLS_PK_RSA:
8634 return( set->rsa );
8635 case MBEDTLS_PK_ECDSA:
8636 return( set->ecdsa );
8637 default:
8638 return( MBEDTLS_MD_NONE );
8639 }
8640}
8641
8642/* Add a signature-hash-pair to a signature-hash set */
8643void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8644 mbedtls_pk_type_t sig_alg,
8645 mbedtls_md_type_t md_alg )
8646{
8647 switch( sig_alg )
8648 {
8649 case MBEDTLS_PK_RSA:
8650 if( set->rsa == MBEDTLS_MD_NONE )
8651 set->rsa = md_alg;
8652 break;
8653
8654 case MBEDTLS_PK_ECDSA:
8655 if( set->ecdsa == MBEDTLS_MD_NONE )
8656 set->ecdsa = md_alg;
8657 break;
8658
8659 default:
8660 break;
8661 }
8662}
8663
8664/* Allow exactly one hash algorithm for each signature. */
8665void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8666 mbedtls_md_type_t md_alg )
8667{
8668 set->rsa = md_alg;
8669 set->ecdsa = md_alg;
8670}
8671
8672#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8673 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8674
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008675/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008676 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008679{
8680 switch( hash )
8681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#if defined(MBEDTLS_MD5_C)
8683 case MBEDTLS_SSL_HASH_MD5:
8684 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#if defined(MBEDTLS_SHA1_C)
8687 case MBEDTLS_SSL_HASH_SHA1:
8688 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690#if defined(MBEDTLS_SHA256_C)
8691 case MBEDTLS_SSL_HASH_SHA224:
8692 return( MBEDTLS_MD_SHA224 );
8693 case MBEDTLS_SSL_HASH_SHA256:
8694 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008695#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696#if defined(MBEDTLS_SHA512_C)
8697 case MBEDTLS_SSL_HASH_SHA384:
8698 return( MBEDTLS_MD_SHA384 );
8699 case MBEDTLS_SSL_HASH_SHA512:
8700 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008701#endif
8702 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008704 }
8705}
8706
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008707/*
8708 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8709 */
8710unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8711{
8712 switch( md )
8713 {
8714#if defined(MBEDTLS_MD5_C)
8715 case MBEDTLS_MD_MD5:
8716 return( MBEDTLS_SSL_HASH_MD5 );
8717#endif
8718#if defined(MBEDTLS_SHA1_C)
8719 case MBEDTLS_MD_SHA1:
8720 return( MBEDTLS_SSL_HASH_SHA1 );
8721#endif
8722#if defined(MBEDTLS_SHA256_C)
8723 case MBEDTLS_MD_SHA224:
8724 return( MBEDTLS_SSL_HASH_SHA224 );
8725 case MBEDTLS_MD_SHA256:
8726 return( MBEDTLS_SSL_HASH_SHA256 );
8727#endif
8728#if defined(MBEDTLS_SHA512_C)
8729 case MBEDTLS_MD_SHA384:
8730 return( MBEDTLS_SSL_HASH_SHA384 );
8731 case MBEDTLS_MD_SHA512:
8732 return( MBEDTLS_SSL_HASH_SHA512 );
8733#endif
8734 default:
8735 return( MBEDTLS_SSL_HASH_NONE );
8736 }
8737}
8738
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008739#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008740/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008741 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008742 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008743 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008744int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008747
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008748 if( ssl->conf->curve_list == NULL )
8749 return( -1 );
8750
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008751 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008752 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008753 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008754
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008755 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008756}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008757#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008758
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008759#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008760/*
8761 * Check if a hash proposed by the peer is in our list.
8762 * Return 0 if we're willing to use it, -1 otherwise.
8763 */
8764int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8765 mbedtls_md_type_t md )
8766{
8767 const int *cur;
8768
8769 if( ssl->conf->sig_hashes == NULL )
8770 return( -1 );
8771
8772 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8773 if( *cur == (int) md )
8774 return( 0 );
8775
8776 return( -1 );
8777}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008778#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#if defined(MBEDTLS_X509_CRT_PARSE_C)
8781int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8782 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008783 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008784 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008785{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008786 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008788 int usage = 0;
8789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008791 const char *ext_oid;
8792 size_t ext_len;
8793#endif
8794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8796 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008797 ((void) cert);
8798 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008799 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008800#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8803 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008804 {
8805 /* Server part of the key exchange */
8806 switch( ciphersuite->key_exchange )
8807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808 case MBEDTLS_KEY_EXCHANGE_RSA:
8809 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008810 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008811 break;
8812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8814 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8815 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8816 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008817 break;
8818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8820 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008821 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008822 break;
8823
8824 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 case MBEDTLS_KEY_EXCHANGE_NONE:
8826 case MBEDTLS_KEY_EXCHANGE_PSK:
8827 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8828 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008829 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008830 usage = 0;
8831 }
8832 }
8833 else
8834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8836 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008837 }
8838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008840 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008841 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008842 ret = -1;
8843 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008844#else
8845 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8849 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8852 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008853 }
8854 else
8855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8857 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008858 }
8859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008861 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008862 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008863 ret = -1;
8864 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008866
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008867 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008868}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008870
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008871/*
8872 * Convert version numbers to/from wire format
8873 * and, for DTLS, to/from TLS equivalent.
8874 *
8875 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008876 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008877 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8878 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008881 unsigned char ver[2] )
8882{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008883#if defined(MBEDTLS_SSL_PROTO_DTLS)
8884 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008887 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8888
8889 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8890 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8891 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008892 else
8893#else
8894 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008895#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008896 {
8897 ver[0] = (unsigned char) major;
8898 ver[1] = (unsigned char) minor;
8899 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008900}
8901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008902void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008903 const unsigned char ver[2] )
8904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905#if defined(MBEDTLS_SSL_PROTO_DTLS)
8906 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008907 {
8908 *major = 255 - ver[0] + 2;
8909 *minor = 255 - ver[1] + 1;
8910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008912 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8913 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008914 else
8915#else
8916 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008917#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008918 {
8919 *major = ver[0];
8920 *minor = ver[1];
8921 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008922}
8923
Simon Butcher99000142016-10-13 17:21:01 +01008924int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8925{
8926#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8927 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8928 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8929
8930 switch( md )
8931 {
8932#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8933#if defined(MBEDTLS_MD5_C)
8934 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008935 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008936#endif
8937#if defined(MBEDTLS_SHA1_C)
8938 case MBEDTLS_SSL_HASH_SHA1:
8939 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8940 break;
8941#endif
8942#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8943#if defined(MBEDTLS_SHA512_C)
8944 case MBEDTLS_SSL_HASH_SHA384:
8945 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8946 break;
8947#endif
8948#if defined(MBEDTLS_SHA256_C)
8949 case MBEDTLS_SSL_HASH_SHA256:
8950 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8951 break;
8952#endif
8953 default:
8954 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8955 }
8956
8957 return 0;
8958#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8959 (void) ssl;
8960 (void) md;
8961
8962 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8963#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8964}
8965
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008966#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8967 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8968int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8969 unsigned char *output,
8970 unsigned char *data, size_t data_len )
8971{
8972 int ret = 0;
8973 mbedtls_md5_context mbedtls_md5;
8974 mbedtls_sha1_context mbedtls_sha1;
8975
8976 mbedtls_md5_init( &mbedtls_md5 );
8977 mbedtls_sha1_init( &mbedtls_sha1 );
8978
8979 /*
8980 * digitally-signed struct {
8981 * opaque md5_hash[16];
8982 * opaque sha_hash[20];
8983 * };
8984 *
8985 * md5_hash
8986 * MD5(ClientHello.random + ServerHello.random
8987 * + ServerParams);
8988 * sha_hash
8989 * SHA(ClientHello.random + ServerHello.random
8990 * + ServerParams);
8991 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008992 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008993 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008995 goto exit;
8996 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008997 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008998 ssl->handshake->randbytes, 64 ) ) != 0 )
8999 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009001 goto exit;
9002 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009003 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009004 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009006 goto exit;
9007 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009008 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009009 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009011 goto exit;
9012 }
9013
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009014 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009015 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009017 goto exit;
9018 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009019 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009020 ssl->handshake->randbytes, 64 ) ) != 0 )
9021 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009023 goto exit;
9024 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009025 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009026 data_len ) ) != 0 )
9027 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009029 goto exit;
9030 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009031 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009032 output + 16 ) ) != 0 )
9033 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009035 goto exit;
9036 }
9037
9038exit:
9039 mbedtls_md5_free( &mbedtls_md5 );
9040 mbedtls_sha1_free( &mbedtls_sha1 );
9041
9042 if( ret != 0 )
9043 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9044 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9045
9046 return( ret );
9047
9048}
9049#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9050 MBEDTLS_SSL_PROTO_TLS1_1 */
9051
9052#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9053 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9054int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009055 unsigned char *hash, size_t *hashlen,
9056 unsigned char *data, size_t data_len,
9057 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009058{
9059 int ret = 0;
9060 mbedtls_md_context_t ctx;
9061 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009062 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009063
9064 mbedtls_md_init( &ctx );
9065
9066 /*
9067 * digitally-signed struct {
9068 * opaque client_random[32];
9069 * opaque server_random[32];
9070 * ServerDHParams params;
9071 * };
9072 */
9073 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9074 {
9075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9076 goto exit;
9077 }
9078 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9079 {
9080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9081 goto exit;
9082 }
9083 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9084 {
9085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9086 goto exit;
9087 }
9088 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9089 {
9090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9091 goto exit;
9092 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009093 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009094 {
9095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9096 goto exit;
9097 }
9098
9099exit:
9100 mbedtls_md_free( &ctx );
9101
9102 if( ret != 0 )
9103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9104 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9105
9106 return( ret );
9107}
9108#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9109 MBEDTLS_SSL_PROTO_TLS1_2 */
9110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_SSL_TLS_C */