blob: e54bb0e503d0aaf122f5bf9da237d189be5b9906 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Becker2a43f6f2018-08-10 11:12:52 +010057static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
58
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010059/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020063 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065#else
66 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067#endif
68 return( 0 );
69}
70
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020071/*
72 * Start a timer.
73 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020077 if( ssl->f_set_timer == NULL )
78 return;
79
80 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
81 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082}
83
84/*
85 * Return -1 is timer is expired, 0 if it isn't.
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020089 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020090 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020091
92 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020093 {
94 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020096 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020097
98 return( 0 );
99}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100101static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
102 mbedtls_ssl_transform *transform );
103static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
104 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100105
106#define SSL_DONT_FORCE_FLUSH 0
107#define SSL_FORCE_FLUSH 1
108
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100110
Hanno Beckera67dee22018-08-22 10:05:20 +0100111static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100112static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100113{
Hanno Becker11682cc2018-08-22 14:41:02 +0100114 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100115
116 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100117 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100118
119 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
120}
121
Hanno Becker67bc7c32018-08-06 11:33:50 +0100122static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
123{
Hanno Becker11682cc2018-08-22 14:41:02 +0100124 size_t const bytes_written = ssl->out_left;
125 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100126
127 /* Double-check that the write-index hasn't gone
128 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100129 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100130 {
131 /* Should never happen... */
132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
133 }
134
135 return( (int) ( mtu - bytes_written ) );
136}
137
138static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
139{
140 int ret;
141 size_t remaining, expansion;
142 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
143
144#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
145 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
146
147 if( max_len > mfl )
148 max_len = mfl;
149#endif
150
151 ret = ssl_get_remaining_space_in_datagram( ssl );
152 if( ret < 0 )
153 return( ret );
154 remaining = (size_t) ret;
155
156 ret = mbedtls_ssl_get_record_expansion( ssl );
157 if( ret < 0 )
158 return( ret );
159 expansion = (size_t) ret;
160
161 if( remaining <= expansion )
162 return( 0 );
163
164 remaining -= expansion;
165 if( remaining >= max_len )
166 remaining = max_len;
167
168 return( (int) remaining );
169}
170
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200171/*
172 * Double the retransmit timeout value, within the allowed range,
173 * returning -1 if the maximum value has already been reached.
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200176{
177 uint32_t new_timeout;
178
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200179 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200180 return( -1 );
181
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200182 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
183 * in the following way: after the initial transmission and a first
184 * retransmission, back off to a temporary estimated MTU of 508 bytes.
185 * This value is guaranteed to be deliverable (if not guaranteed to be
186 * delivered) of any compliant IPv4 (and IPv6) network, and should work
187 * on most non-IP stacks too. */
188 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
189 ssl->handshake->mtu = 508;
190
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200191 new_timeout = 2 * ssl->handshake->retransmit_timeout;
192
193 /* Avoid arithmetic overflow and range overflow */
194 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200195 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200196 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200197 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200198 }
199
200 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202 ssl->handshake->retransmit_timeout ) );
203
204 return( 0 );
205}
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200208{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200209 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211 ssl->handshake->retransmit_timeout ) );
212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200216/*
217 * Convert max_fragment_length codes to length.
218 * RFC 6066 says:
219 * enum{
220 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
221 * } MaxFragmentLength;
222 * and we add 0 -> extension unused
223 */
Angus Grattond8213d02016-05-25 20:56:48 +1000224static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200225{
Angus Grattond8213d02016-05-25 20:56:48 +1000226 switch( mfl )
227 {
228 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
229 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
230 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
231 return 512;
232 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
233 return 1024;
234 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
235 return 2048;
236 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
237 return 4096;
238 default:
239 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
240 }
241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200243
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200244#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_ssl_session_free( dst );
248 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200251 if( src->peer_cert != NULL )
252 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200253 int ret;
254
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200255 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200256 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200257 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200262 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265 dst->peer_cert = NULL;
266 return( ret );
267 }
268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200270
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200271#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200272 if( src->ticket != NULL )
273 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200274 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200275 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200276 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200277
278 memcpy( dst->ticket, src->ticket, src->ticket_len );
279 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200280#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200281
282 return( 0 );
283}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200284#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
287int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200288 const unsigned char *key_enc, const unsigned char *key_dec,
289 size_t keylen,
290 const unsigned char *iv_enc, const unsigned char *iv_dec,
291 size_t ivlen,
292 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200293 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
295int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
296int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
297int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
298int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
299#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000300
Paul Bakker5121ce52009-01-03 21:22:43 +0000301/*
302 * Key material generation
303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200305static int ssl3_prf( const unsigned char *secret, size_t slen,
306 const char *label,
307 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000308 unsigned char *dstbuf, size_t dlen )
309{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100310 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000311 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200312 mbedtls_md5_context md5;
313 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000314 unsigned char padding[16];
315 unsigned char sha1sum[20];
316 ((void)label);
317
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200318 mbedtls_md5_init( &md5 );
319 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200320
Paul Bakker5f70b252012-09-13 14:23:06 +0000321 /*
322 * SSLv3:
323 * block =
324 * MD5( secret + SHA1( 'A' + secret + random ) ) +
325 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
326 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
327 * ...
328 */
329 for( i = 0; i < dlen / 16; i++ )
330 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200331 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000332
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100333 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100334 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100335 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100336 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100337 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100338 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100339 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100340 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100342 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000343
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100344 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100345 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100346 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100347 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100348 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100349 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100350 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100351 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000352 }
353
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100354exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200355 mbedtls_md5_free( &md5 );
356 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000357
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500358 mbedtls_platform_zeroize( padding, sizeof( padding ) );
359 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000360
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100361 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200366static int tls1_prf( const unsigned char *secret, size_t slen,
367 const char *label,
368 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000369 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000370{
Paul Bakker23986e52011-04-24 08:57:21 +0000371 size_t nb, hs;
372 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200373 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 unsigned char tmp[128];
375 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 const mbedtls_md_info_t *md_info;
377 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100378 int ret;
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 hs = ( slen + 1 ) / 2;
386 S1 = secret;
387 S2 = secret + slen - hs;
388
389 nb = strlen( label );
390 memcpy( tmp + 20, label, nb );
391 memcpy( tmp + 20 + nb, random, rlen );
392 nb += rlen;
393
394 /*
395 * First compute P_md5(secret,label+random)[0..dlen]
396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100401 return( ret );
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
404 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
405 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407 for( i = 0; i < dlen; i += 16 )
408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_reset ( &md_ctx );
410 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
411 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_md_hmac_reset ( &md_ctx );
414 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
415 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
418
419 for( j = 0; j < k; j++ )
420 dstbuf[i + j] = h_i[j];
421 }
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100424
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 /*
426 * XOR out with P_sha1(secret,label+random)[0..dlen]
427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 return( ret );
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
435 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
436 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 for( i = 0; i < dlen; i += 20 )
439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_reset ( &md_ctx );
441 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
442 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_md_hmac_reset ( &md_ctx );
445 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
446 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
449
450 for( j = 0; j < k; j++ )
451 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
452 }
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500456 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
457 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 return( 0 );
460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
464static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100465 const unsigned char *secret, size_t slen,
466 const char *label,
467 const unsigned char *random, size_t rlen,
468 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000469{
470 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100471 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000472 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
474 const mbedtls_md_info_t *md_info;
475 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100476 int ret;
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100484
485 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487
488 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100489 memcpy( tmp + md_len, label, nb );
490 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000491 nb += rlen;
492
493 /*
494 * Compute P_<hash>(secret, label + random)[0..dlen]
495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497 return( ret );
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
500 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
501 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100502
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100503 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_md_hmac_reset ( &md_ctx );
506 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
507 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_md_hmac_reset ( &md_ctx );
510 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
511 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000512
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100513 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000514
515 for( j = 0; j < k; j++ )
516 dstbuf[i + j] = h_i[j];
517 }
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100520
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500521 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
522 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523
524 return( 0 );
525}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100528static int tls_prf_sha256( const unsigned char *secret, size_t slen,
529 const char *label,
530 const unsigned char *random, size_t rlen,
531 unsigned char *dstbuf, size_t dlen )
532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100534 label, random, rlen, dstbuf, dlen ) );
535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200539static int tls_prf_sha384( const unsigned char *secret, size_t slen,
540 const char *label,
541 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000542 unsigned char *dstbuf, size_t dlen )
543{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100545 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#endif /* MBEDTLS_SHA512_C */
548#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
553 defined(MBEDTLS_SSL_PROTO_TLS1_1)
554static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200555#endif
Paul Bakker380da532012-04-18 16:10:25 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#if defined(MBEDTLS_SSL_PROTO_SSL3)
558static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
559static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200560#endif
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
563static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
564static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200565#endif
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
568#if defined(MBEDTLS_SHA256_C)
569static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
570static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
571static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200572#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_SHA512_C)
575static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
576static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
577static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200583 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 unsigned char keyblk[256];
586 unsigned char *key1;
587 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100588 unsigned char *mac_enc;
589 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000590 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200591 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 const mbedtls_cipher_info_t *cipher_info;
593 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_ssl_session *session = ssl->session_negotiate;
596 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
597 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100602 if( cipher_info == NULL )
603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100605 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100607 }
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100610 if( md_info == NULL )
611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100613 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100615 }
616
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000618 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_SSL_PROTO_SSL3)
621 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000622 {
Paul Bakker48916f92012-09-16 19:57:18 +0000623 handshake->tls_prf = ssl3_prf;
624 handshake->calc_verify = ssl_calc_verify_ssl;
625 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000626 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200627 else
628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
630 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 {
Paul Bakker48916f92012-09-16 19:57:18 +0000632 handshake->tls_prf = tls1_prf;
633 handshake->calc_verify = ssl_calc_verify_tls;
634 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000635 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200636 else
637#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
639#if defined(MBEDTLS_SHA512_C)
640 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
641 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000642 {
Paul Bakker48916f92012-09-16 19:57:18 +0000643 handshake->tls_prf = tls_prf_sha384;
644 handshake->calc_verify = ssl_calc_verify_tls_sha384;
645 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200648#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SHA256_C)
650 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651 {
Paul Bakker48916f92012-09-16 19:57:18 +0000652 handshake->tls_prf = tls_prf_sha256;
653 handshake->calc_verify = ssl_calc_verify_tls_sha256;
654 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000655 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200656 else
657#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200662 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000663
664 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 * SSLv3:
666 * master =
667 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
668 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
669 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200670 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200671 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 * master = PRF( premaster, "master secret", randbytes )[0..47]
673 */
Paul Bakker0a597072012-09-25 21:55:46 +0000674 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000677 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
680 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200681 {
682 unsigned char session_hash[48];
683 size_t hash_len;
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200686
687 ssl->handshake->calc_verify( ssl, session_hash );
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
690 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200693 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200695 {
696 hash_len = 48;
697 }
698 else
699#endif
700 hash_len = 32;
701 }
702 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200704 hash_len = 36;
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200707
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100708 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
709 "extended master secret",
710 session_hash, hash_len,
711 session->master, 48 );
712 if( ret != 0 )
713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100715 return( ret );
716 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200717
718 }
719 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200720#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100721 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
722 "master secret",
723 handshake->randbytes, 64,
724 session->master, 48 );
725 if( ret != 0 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100728 return( ret );
729 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200730
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500731 mbedtls_platform_zeroize( handshake->premaster,
732 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 }
734 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
737 /*
738 * Swap the client and server random values.
739 */
Paul Bakker48916f92012-09-16 19:57:18 +0000740 memcpy( tmp, handshake->randbytes, 64 );
741 memcpy( handshake->randbytes, tmp + 32, 32 );
742 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
745 /*
746 * SSLv3:
747 * key block =
748 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
749 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
750 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
751 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
752 * ...
753 *
754 * TLSv1:
755 * key block = PRF( master, "key expansion", randbytes )
756 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100757 ret = handshake->tls_prf( session->master, 48, "key expansion",
758 handshake->randbytes, 64, keyblk, 256 );
759 if( ret != 0 )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100762 return( ret );
763 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
766 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
767 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
768 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
769 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500771 mbedtls_platform_zeroize( handshake->randbytes,
772 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774 /*
775 * Determine the appropriate key, IV and MAC length.
776 */
Paul Bakker68884e32013-01-07 18:20:04 +0100777
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200778 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200781 cipher_info->mode == MBEDTLS_MODE_CCM ||
782 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000783 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200784 size_t taglen, explicit_ivlen;
785
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200786 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000787 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200788
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200789 /* All modes haves 96-bit IVs;
790 * GCM and CCM has 4 implicit and 8 explicit bytes
791 * ChachaPoly has all 12 bytes implicit
792 */
Paul Bakker68884e32013-01-07 18:20:04 +0100793 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200794 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
795 transform->fixed_ivlen = 12;
796 else
797 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200798
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200799 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
800 taglen = transform->ciphersuite_info->flags &
801 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
802
803
804 /* Minimum length of encrypted record */
805 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
806 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100807 }
808 else
809 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200810 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
812 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200815 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100816 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200818 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000819 mac_key_len = mbedtls_md_get_size( md_info );
820 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200823 /*
824 * If HMAC is to be truncated, we shall keep the leftmost bytes,
825 * (rfc 6066 page 13 or rfc 2104 section 4),
826 * so we only need to adjust the length here.
827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000831
832#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
833 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000834 * HMAC implementation which also truncates the key
835 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000836 mac_key_len = transform->maclen;
837#endif
838 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200840
841 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100842 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200844 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200846 transform->minlen = transform->maclen;
847 else
Paul Bakker68884e32013-01-07 18:20:04 +0100848 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200849 /*
850 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100851 * 1. if EtM is in use: one block plus MAC
852 * otherwise: * first multiple of blocklen greater than maclen
853 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
856 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100857 {
858 transform->minlen = transform->maclen
859 + cipher_info->block_size;
860 }
861 else
862#endif
863 {
864 transform->minlen = transform->maclen
865 + cipher_info->block_size
866 - transform->maclen % cipher_info->block_size;
867 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
870 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
871 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200872 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100873 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
876 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
877 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200878 {
879 transform->minlen += transform->ivlen;
880 }
881 else
882#endif
883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200886 }
Paul Bakker68884e32013-01-07 18:20:04 +0100887 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 }
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000891 transform->keylen, transform->minlen, transform->ivlen,
892 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
894 /*
895 * Finally setup the cipher contexts, IVs and MAC secrets.
896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200898 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000900 key1 = keyblk + mac_key_len * 2;
901 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Paul Bakker68884e32013-01-07 18:20:04 +0100903 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000904 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000906 /*
907 * This is not used in TLS v1.1.
908 */
Paul Bakker48916f92012-09-16 19:57:18 +0000909 iv_copy_len = ( transform->fixed_ivlen ) ?
910 transform->fixed_ivlen : transform->ivlen;
911 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
912 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000913 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 }
915 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#endif /* MBEDTLS_SSL_CLI_C */
917#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200918 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000920 key1 = keyblk + mac_key_len * 2 + transform->keylen;
921 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Hanno Becker81c7b182017-11-09 18:39:33 +0000923 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100924 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000926 /*
927 * This is not used in TLS v1.1.
928 */
Paul Bakker48916f92012-09-16 19:57:18 +0000929 iv_copy_len = ( transform->fixed_ivlen ) ?
930 transform->fixed_ivlen : transform->ivlen;
931 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
932 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000933 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100935 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100940 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_PROTO_SSL3)
943 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100944 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000945 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
948 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100949 }
950
Hanno Becker81c7b182017-11-09 18:39:33 +0000951 memcpy( transform->mac_enc, mac_enc, mac_key_len );
952 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100953 }
954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
956#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
957 defined(MBEDTLS_SSL_PROTO_TLS1_2)
958 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100959 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100960 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
961 For AEAD-based ciphersuites, there is nothing to do here. */
962 if( mac_key_len != 0 )
963 {
964 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
965 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
966 }
Paul Bakker68884e32013-01-07 18:20:04 +0100967 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200968 else
969#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200973 }
Paul Bakker68884e32013-01-07 18:20:04 +0100974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
976 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000977 {
978 int ret = 0;
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100983 transform->iv_enc, transform->iv_dec,
984 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100985 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000986 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
989 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000990 }
991 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000993
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200994#if defined(MBEDTLS_SSL_EXPORT_KEYS)
995 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100996 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200997 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
998 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001000 iv_copy_len );
1001 }
1002#endif
1003
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001004 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001005 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001008 return( ret );
1009 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001010
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001011 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001012 cipher_info ) ) != 0 )
1013 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001015 return( ret );
1016 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001019 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001023 return( ret );
1024 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001027 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001031 return( ret );
1032 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_CIPHER_MODE_CBC)
1035 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1038 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001041 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001042 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1045 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001048 return( ret );
1049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001053 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001056 // Initialize compression
1057 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001059 {
Paul Bakker16770332013-10-11 09:59:44 +02001060 if( ssl->compress_buf == NULL )
1061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001063 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001064 if( ssl->compress_buf == NULL )
1065 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001067 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001069 }
1070 }
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001073
Paul Bakker48916f92012-09-16 19:57:18 +00001074 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1075 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001076
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001077 if( deflateInit( &transform->ctx_deflate,
1078 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001079 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1082 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001083 }
1084 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
1089 return( 0 );
1090}
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_SSL_PROTO_SSL3)
1093void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001094{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001095 mbedtls_md5_context md5;
1096 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 unsigned char pad_1[48];
1098 unsigned char pad_2[48];
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001102 mbedtls_md5_init( &md5 );
1103 mbedtls_sha1_init( &sha1 );
1104
1105 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1106 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Paul Bakker380da532012-04-18 16:10:25 +00001108 memset( pad_1, 0x36, 48 );
1109 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001111 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1112 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1113 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001115 mbedtls_md5_starts_ret( &md5 );
1116 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1117 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1118 mbedtls_md5_update_ret( &md5, hash, 16 );
1119 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1122 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1123 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001124
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001125 mbedtls_sha1_starts_ret( &sha1 );
1126 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1127 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1128 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1129 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001133
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001134 mbedtls_md5_free( &md5 );
1135 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001136
Paul Bakker380da532012-04-18 16:10:25 +00001137 return;
1138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1142void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001143{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001144 mbedtls_md5_context md5;
1145 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001148
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001149 mbedtls_md5_init( &md5 );
1150 mbedtls_sha1_init( &sha1 );
1151
1152 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1153 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001154
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001155 mbedtls_md5_finish_ret( &md5, hash );
1156 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001160
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001161 mbedtls_md5_free( &md5 );
1162 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001163
Paul Bakker380da532012-04-18 16:10:25 +00001164 return;
1165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1169#if defined(MBEDTLS_SHA256_C)
1170void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001171{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001172 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001174 mbedtls_sha256_init( &sha256 );
1175
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001177
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001178 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001179 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001183
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001184 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001185
Paul Bakker380da532012-04-18 16:10:25 +00001186 return;
1187}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SHA512_C)
1191void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001192{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001193 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001194
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001195 mbedtls_sha512_init( &sha512 );
1196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001198
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001199 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001200 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001205 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001206
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 return;
1208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_SHA512_C */
1210#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1213int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001214{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001215 unsigned char *p = ssl->handshake->premaster;
1216 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001217 const unsigned char *psk = ssl->conf->psk;
1218 size_t psk_len = ssl->conf->psk_len;
1219
1220 /* If the psk callback was called, use its result */
1221 if( ssl->handshake->psk != NULL )
1222 {
1223 psk = ssl->handshake->psk;
1224 psk_len = ssl->handshake->psk_len;
1225 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001226
1227 /*
1228 * PMS = struct {
1229 * opaque other_secret<0..2^16-1>;
1230 * opaque psk<0..2^16-1>;
1231 * };
1232 * with "other_secret" depending on the particular key exchange
1233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1235 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001236 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001237 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001239
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001240 *(p++) = (unsigned char)( psk_len >> 8 );
1241 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001242
1243 if( end < p || (size_t)( end - p ) < psk_len )
1244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1245
1246 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001247 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001248 }
1249 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1251#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1252 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001253 {
1254 /*
1255 * other_secret already set by the ClientKeyExchange message,
1256 * and is 48 bytes long
1257 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001258 if( end - p < 2 )
1259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1260
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001261 *p++ = 0;
1262 *p++ = 48;
1263 p += 48;
1264 }
1265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1267#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1268 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001269 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001270 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001271 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001272
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001273 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001275 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001276 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001279 return( ret );
1280 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001281 *(p++) = (unsigned char)( len >> 8 );
1282 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001283 p += len;
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001286 }
1287 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1289#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1290 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001291 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001292 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001293 size_t zlen;
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001296 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001297 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001300 return( ret );
1301 }
1302
1303 *(p++) = (unsigned char)( zlen >> 8 );
1304 *(p++) = (unsigned char)( zlen );
1305 p += zlen;
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001308 }
1309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001314 }
1315
1316 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001317 if( end - p < 2 )
1318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001319
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001320 *(p++) = (unsigned char)( psk_len >> 8 );
1321 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001322
1323 if( end < p || (size_t)( end - p ) < psk_len )
1324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1325
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001326 memcpy( p, psk, psk_len );
1327 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001328
1329 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1330
1331 return( 0 );
1332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001336/*
1337 * SSLv3.0 MAC functions
1338 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001339#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001340static void ssl_mac( mbedtls_md_context_t *md_ctx,
1341 const unsigned char *secret,
1342 const unsigned char *buf, size_t len,
1343 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001344 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001345{
1346 unsigned char header[11];
1347 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001348 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1350 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001351
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001352 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001354 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001355 else
Paul Bakker68884e32013-01-07 18:20:04 +01001356 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
1358 memcpy( header, ctr, 8 );
1359 header[ 8] = (unsigned char) type;
1360 header[ 9] = (unsigned char)( len >> 8 );
1361 header[10] = (unsigned char)( len );
1362
Paul Bakker68884e32013-01-07 18:20:04 +01001363 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_md_starts( md_ctx );
1365 mbedtls_md_update( md_ctx, secret, md_size );
1366 mbedtls_md_update( md_ctx, padding, padlen );
1367 mbedtls_md_update( md_ctx, header, 11 );
1368 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001369 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Paul Bakker68884e32013-01-07 18:20:04 +01001371 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_md_starts( md_ctx );
1373 mbedtls_md_update( md_ctx, secret, md_size );
1374 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001375 mbedtls_md_update( md_ctx, out, md_size );
1376 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1381 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001382 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001383#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001384#endif
1385
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001386/* The function below is only used in the Lucky 13 counter-measure in
1387 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1388#if defined(SSL_SOME_MODES_USE_MAC) && \
1389 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1390 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1391 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1392/* This function makes sure every byte in the memory region is accessed
1393 * (in ascending addresses order) */
1394static void ssl_read_memory( unsigned char *p, size_t len )
1395{
1396 unsigned char acc = 0;
1397 volatile unsigned char force;
1398
1399 for( ; len != 0; p++, len-- )
1400 acc ^= *p;
1401
1402 force = acc;
1403 (void) force;
1404}
1405#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1406
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001407/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001411{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001413 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001417 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001421 }
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001426 ssl->out_msg, ssl->out_msglen );
1427
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001429 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001431#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 if( mode == MBEDTLS_MODE_STREAM ||
1433 ( mode == MBEDTLS_MODE_CBC
1434#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1435 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436#endif
1437 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_PROTO_SSL3)
1440 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001441 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001442 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001443
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001444 ssl_mac( &ssl->transform_out->md_ctx_enc,
1445 ssl->transform_out->mac_enc,
1446 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001447 ssl->out_ctr, ssl->out_msgtype,
1448 mac );
1449
1450 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001451 }
1452 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1455 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1456 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001457 {
Hanno Becker992b6872017-11-09 18:57:39 +00001458 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1461 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1462 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1463 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001464 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001465 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001467
1468 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001469 }
1470 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001471#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001475 }
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001478 ssl->out_msg + ssl->out_msglen,
1479 ssl->transform_out->maclen );
1480
1481 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001482 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001483 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001484#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001485
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001486 /*
1487 * Encrypt
1488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1490 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001492 int ret;
1493 size_t olen = 0;
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 "including %d bytes of padding",
1497 ssl->out_msglen, 0 ) );
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001500 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001501 ssl->transform_out->ivlen,
1502 ssl->out_msg, ssl->out_msglen,
1503 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001506 return( ret );
1507 }
1508
1509 if( ssl->out_msglen != olen )
1510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1512 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
Paul Bakker68884e32013-01-07 18:20:04 +01001515 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001517#if defined(MBEDTLS_GCM_C) || \
1518 defined(MBEDTLS_CCM_C) || \
1519 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001521 mode == MBEDTLS_MODE_CCM ||
1522 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001523 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001524 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001525 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001526 unsigned char *enc_msg;
1527 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001528 unsigned char iv[12];
1529 mbedtls_ssl_transform *transform = ssl->transform_out;
1530 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001532 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001534 /*
1535 * Prepare additional authenticated data
1536 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001537 memcpy( add_data, ssl->out_ctr, 8 );
1538 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001540 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1542 add_data[12] = ssl->out_msglen & 0xFF;
1543
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001544 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001545
Paul Bakker68884e32013-01-07 18:20:04 +01001546 /*
1547 * Generate IV
1548 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001549 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1550 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001551 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001552 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1553 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1554 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1555
1556 }
1557 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1558 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001559 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001560 unsigned char i;
1561
1562 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1563
1564 for( i = 0; i < 8; i++ )
1565 iv[i+4] ^= ssl->out_ctr[i];
1566 }
1567 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001568 {
1569 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001572 }
1573
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001574 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1575 iv, transform->ivlen );
1576 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1577 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001578
Paul Bakker68884e32013-01-07 18:20:04 +01001579 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001581 */
1582 enc_msg = ssl->out_msg;
1583 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001584 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001587 "including 0 bytes of padding",
1588 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001589
Paul Bakker68884e32013-01-07 18:20:04 +01001590 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001592 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001593 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1594 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001595 add_data, 13,
1596 enc_msg, enc_msglen,
1597 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001598 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001601 return( ret );
1602 }
1603
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001604 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001608 }
1609
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001610 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001611 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1617#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001618 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001621 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001622 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001623 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001624
Paul Bakker48916f92012-09-16 19:57:18 +00001625 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1626 ssl->transform_out->ivlen;
1627 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 padlen = 0;
1629
1630 for( i = 0; i <= padlen; i++ )
1631 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1632
1633 ssl->out_msglen += padlen + 1;
1634
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001635 enc_msglen = ssl->out_msglen;
1636 enc_msg = ssl->out_msg;
1637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001640 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1641 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 {
1645 /*
1646 * Generate IV
1647 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001648 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001649 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001650 if( ret != 0 )
1651 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652
Paul Bakker92be97b2013-01-02 17:30:03 +01001653 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001654 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001655
1656 /*
1657 * Fix pointer positions and message length with added IV
1658 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001659 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001660 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001661 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001666 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001667 ssl->out_msglen, ssl->transform_out->ivlen,
1668 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001671 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001672 ssl->transform_out->ivlen,
1673 enc_msg, enc_msglen,
1674 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001677 return( ret );
1678 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001679
Paul Bakkercca5b812013-08-31 17:40:26 +02001680 if( enc_msglen != olen )
1681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001684 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1687 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001688 {
1689 /*
1690 * Save IV in SSL3 and TLS1
1691 */
1692 memcpy( ssl->transform_out->iv_enc,
1693 ssl->transform_out->cipher_ctx_enc.iv,
1694 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001696#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001699 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001700 {
1701 /*
1702 * MAC(MAC_write_key, seq_num +
1703 * TLSCipherText.type +
1704 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001705 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001706 * IV + // except for TLS 1.0
1707 * ENC(content + padding + padding_length));
1708 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001709 unsigned char pseudo_hdr[13];
1710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001712
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001713 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1714 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001715 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1716 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1721 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001722 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001724 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726
1727 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001728 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001732 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001734 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001740 /* Make extra sure authentication was performed, exactly once */
1741 if( auth_done != 1 )
1742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001745 }
1746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
1749 return( 0 );
1750}
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001755 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001756#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001757 size_t padlen = 0, correct = 1;
1758#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001766 }
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001769
Paul Bakker48916f92012-09-16 19:57:18 +00001770 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001773 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 }
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1778 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001779 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001780 int ret;
1781 size_t olen = 0;
1782
Paul Bakker68884e32013-01-07 18:20:04 +01001783 padlen = 0;
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001786 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001787 ssl->transform_in->ivlen,
1788 ssl->in_msg, ssl->in_msglen,
1789 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001792 return( ret );
1793 }
1794
1795 if( ssl->in_msglen != olen )
1796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001799 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 }
Paul Bakker68884e32013-01-07 18:20:04 +01001801 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803#if defined(MBEDTLS_GCM_C) || \
1804 defined(MBEDTLS_CCM_C) || \
1805 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001807 mode == MBEDTLS_MODE_CCM ||
1808 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001809 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001810 int ret;
1811 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001812 unsigned char *dec_msg;
1813 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001814 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001815 unsigned char iv[12];
1816 mbedtls_ssl_transform *transform = ssl->transform_in;
1817 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001819 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001820
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001821 /*
1822 * Compute and update sizes
1823 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001824 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001827 "+ taglen (%d)", ssl->in_msglen,
1828 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001830 }
1831 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1832
Paul Bakker68884e32013-01-07 18:20:04 +01001833 dec_msg = ssl->in_msg;
1834 dec_msg_result = ssl->in_msg;
1835 ssl->in_msglen = dec_msglen;
1836
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001837 /*
1838 * Prepare additional authenticated data
1839 */
Paul Bakker68884e32013-01-07 18:20:04 +01001840 memcpy( add_data, ssl->in_ctr, 8 );
1841 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001843 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001844 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1845 add_data[12] = ssl->in_msglen & 0xFF;
1846
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001847 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001848
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001849 /*
1850 * Prepare IV
1851 */
1852 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1853 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001854 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1856 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001857
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001858 }
1859 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1860 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001861 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862 unsigned char i;
1863
1864 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1865
1866 for( i = 0; i < 8; i++ )
1867 iv[i+4] ^= ssl->in_ctr[i];
1868 }
1869 else
1870 {
1871 /* Reminder if we ever add an AEAD mode with a different size */
1872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1874 }
1875
1876 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001878
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001879 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001880 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001883 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001884 add_data, 13,
1885 dec_msg, dec_msglen,
1886 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001887 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1892 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001893
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001894 return( ret );
1895 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001896 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001897
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001898 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1901 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001902 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1906#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001907 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 {
Paul Bakker45829992013-01-03 14:52:21 +01001910 /*
1911 * Decrypt and check the padding
1912 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001913 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001914 unsigned char *dec_msg;
1915 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001916 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001917 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001918 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 /*
Paul Bakker45829992013-01-03 14:52:21 +01001921 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1924 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001925 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001926#endif
Paul Bakker45829992013-01-03 14:52:21 +01001927
1928 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1929 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001932 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1933 ssl->transform_in->ivlen,
1934 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001936 }
1937
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001938 dec_msglen = ssl->in_msglen;
1939 dec_msg = ssl->in_msg;
1940 dec_msg_result = ssl->in_msg;
1941
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001942 /*
1943 * Authenticate before decrypt if enabled
1944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1946 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001947 {
Hanno Becker992b6872017-11-09 18:57:39 +00001948 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001949 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001952
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001953 dec_msglen -= ssl->transform_in->maclen;
1954 ssl->in_msglen -= ssl->transform_in->maclen;
1955
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001956 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1957 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1958 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1959 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1964 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001965 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001966 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001971 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 ssl->transform_in->maclen );
1973
Hanno Becker992b6872017-11-09 18:57:39 +00001974 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1975 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001981 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001982 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984
1985 /*
1986 * Check length sanity
1987 */
1988 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001991 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001993 }
1994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001997 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002001 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002002 dec_msglen -= ssl->transform_in->ivlen;
2003 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004
Paul Bakker48916f92012-09-16 19:57:18 +00002005 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002006 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002007 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002011 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002012 ssl->transform_in->ivlen,
2013 dec_msg, dec_msglen,
2014 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002017 return( ret );
2018 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002019
Paul Bakkercca5b812013-08-31 17:40:26 +02002020 if( dec_msglen != olen )
2021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002024 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2027 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002028 {
2029 /*
2030 * Save IV in SSL3 and TLS1
2031 */
2032 memcpy( ssl->transform_in->iv_dec,
2033 ssl->transform_in->cipher_ctx_dec.iv,
2034 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
2038 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002039
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002040 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002041 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#if defined(MBEDTLS_SSL_DEBUG_ALL)
2044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002045 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002046#endif
Paul Bakker45829992013-01-03 14:52:21 +01002047 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002048 correct = 0;
2049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#if defined(MBEDTLS_SSL_PROTO_SSL3)
2052 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 {
Paul Bakker48916f92012-09-16 19:57:18 +00002054 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#if defined(MBEDTLS_SSL_DEBUG_ALL)
2057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002059 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002060#endif
Paul Bakker45829992013-01-03 14:52:21 +01002061 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
2063 }
2064 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2066#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2067 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2068 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 {
2070 /*
Paul Bakker45829992013-01-03 14:52:21 +01002071 * TLSv1+: always check the padding up to the first failure
2072 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002074 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002075 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002076 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002077
Paul Bakker956c9e02013-12-19 14:42:28 +01002078 /*
2079 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002080 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002081 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002082 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002083 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002084 *
2085 * In both cases we reset padding_idx to a safe value (0) to
2086 * prevent out-of-buffer reads.
2087 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002088 correct &= ( padlen <= ssl->in_msglen );
2089 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002090 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002091
2092 padding_idx *= correct;
2093
Angus Grattonb512bc12018-06-19 15:57:50 +10002094 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002095 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002096 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002097 pad_count += real_count *
2098 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2099 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002100
2101 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002104 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002106#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002107 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002109 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2111 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002115 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002116
2117 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002119 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002121 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002127#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
2132 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002133 * Authenticate if not done yet.
2134 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002136#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002137 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002138 {
Hanno Becker992b6872017-11-09 18:57:39 +00002139 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002140
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002141 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002143 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2144 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#if defined(MBEDTLS_SSL_PROTO_SSL3)
2147 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002148 {
2149 ssl_mac( &ssl->transform_in->md_ctx_dec,
2150 ssl->transform_in->mac_dec,
2151 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002152 ssl->in_ctr, ssl->in_msgtype,
2153 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002154 }
2155 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2157#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2158 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2159 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 {
2161 /*
2162 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002163 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002164 *
2165 * Known timing attacks:
2166 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2167 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002168 * To compensate for different timings for the MAC calculation
2169 * depending on how much padding was removed (which is determined
2170 * by padlen), process extra_run more blocks through the hash
2171 * function.
2172 *
2173 * The formula in the paper is
2174 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2175 * where L1 is the size of the header plus the decrypted message
2176 * plus CBC padding and L2 is the size of the header plus the
2177 * decrypted message. This is for an underlying hash function
2178 * with 64-byte blocks.
2179 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2180 * correctly. We round down instead of up, so -56 is the correct
2181 * value for our calculations instead of -55.
2182 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002183 * Repeat the formula rather than defining a block_size variable.
2184 * This avoids requiring division by a variable at runtime
2185 * (which would be marginally less efficient and would require
2186 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002187 */
2188 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002189
2190 /*
2191 * The next two sizes are the minimum and maximum values of
2192 * in_msglen over all padlen values.
2193 *
2194 * They're independent of padlen, since we previously did
2195 * in_msglen -= padlen.
2196 *
2197 * Note that max_len + maclen is never more than the buffer
2198 * length, as we previously did in_msglen -= maclen too.
2199 */
2200 const size_t max_len = ssl->in_msglen + padlen;
2201 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2202
Gilles Peskine20b44082018-05-29 14:06:49 +02002203 switch( ssl->transform_in->ciphersuite_info->mac )
2204 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002205#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2206 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002207 case MBEDTLS_MD_MD5:
2208 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002209 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002210 /* 8 bytes of message size, 64-byte compression blocks */
2211 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2212 ( 13 + ssl->in_msglen + 8 ) / 64;
2213 break;
2214#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002215#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002216 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002217 /* 16 bytes of message size, 128-byte compression blocks */
2218 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2219 ( 13 + ssl->in_msglen + 16 ) / 128;
2220 break;
2221#endif
2222 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2225 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002226
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002227 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2230 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2231 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2232 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002234 /* Make sure we access everything even when padlen > 0. This
2235 * makes the synchronisation requirements for just-in-time
2236 * Prime+Probe attacks much tighter and hopefully impractical. */
2237 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002238 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002239
2240 /* Call mbedtls_md_process at least once due to cache attacks
2241 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002242 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002246
2247 /* Make sure we access all the memory that could contain the MAC,
2248 * before we check it in the next code block. This makes the
2249 * synchronisation requirements for just-in-time Prime+Probe
2250 * attacks much tighter and hopefully impractical. */
2251 ssl_read_memory( ssl->in_msg + min_len,
2252 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 }
2254 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2256 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002262#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002263 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2264 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2265 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Hanno Becker992b6872017-11-09 18:57:39 +00002268 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2269 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#if defined(MBEDTLS_SSL_DEBUG_ALL)
2272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002273#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002274 correct = 0;
2275 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002276 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002278 /*
2279 * Finally check the correct flag
2280 */
2281 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002283 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002284#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002285
2286 /* Make extra sure authentication was performed, exactly once */
2287 if( auth_done != 1 )
2288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
2293 if( ssl->in_msglen == 0 )
2294 {
Angus Gratton34817922018-06-19 15:58:22 +10002295#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2296 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2297 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2298 {
2299 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2301 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2302 }
2303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2304
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 ssl->nb_zero++;
2306
2307 /*
2308 * Three or more empty messages may be a DoS attack
2309 * (excessive CPU consumption).
2310 */
2311 if( ssl->nb_zero > 3 )
2312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002316 }
2317 }
2318 else
2319 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002322 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002323 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002324 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002325 }
2326 else
2327#endif
2328 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002329 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002330 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2331 if( ++ssl->in_ctr[i - 1] != 0 )
2332 break;
2333
2334 /* The loop goes to its end iff the counter is wrapping */
2335 if( i == ssl_ep_len( ssl ) )
2336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2338 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002339 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002340 }
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
2344 return( 0 );
2345}
2346
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002347#undef MAC_NONE
2348#undef MAC_PLAINTEXT
2349#undef MAC_CIPHERTEXT
2350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352/*
2353 * Compression/decompression functions
2354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356{
2357 int ret;
2358 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002359 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002361 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002364
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002365 if( len_pre == 0 )
2366 return( 0 );
2367
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368 memcpy( msg_pre, ssl->out_msg, len_pre );
2369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002371 ssl->out_msglen ) );
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374 ssl->out_msg, ssl->out_msglen );
2375
Paul Bakker48916f92012-09-16 19:57:18 +00002376 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2377 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2378 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002379 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380
Paul Bakker48916f92012-09-16 19:57:18 +00002381 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382 if( ret != Z_OK )
2383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2385 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386 }
2387
Angus Grattond8213d02016-05-25 20:56:48 +10002388 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002389 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392 ssl->out_msglen ) );
2393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002395 ssl->out_msg, ssl->out_msglen );
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002398
2399 return( 0 );
2400}
2401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403{
2404 int ret;
2405 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002406 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002407 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002408 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002411
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002412 if( len_pre == 0 )
2413 return( 0 );
2414
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415 memcpy( msg_pre, ssl->in_msg, len_pre );
2416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 ssl->in_msglen ) );
2419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421 ssl->in_msg, ssl->in_msglen );
2422
Paul Bakker48916f92012-09-16 19:57:18 +00002423 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2424 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2425 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002426 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002427 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002428
Paul Bakker48916f92012-09-16 19:57:18 +00002429 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002430 if( ret != Z_OK )
2431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2433 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434 }
2435
Angus Grattond8213d02016-05-25 20:56:48 +10002436 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002437 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440 ssl->in_msglen ) );
2441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002443 ssl->in_msg, ssl->in_msglen );
2444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446
2447 return( 0 );
2448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2452static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SSL_PROTO_DTLS)
2455static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002456{
2457 /* If renegotiation is not enforced, retransmit until we would reach max
2458 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002459 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002460 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002461 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002462 unsigned char doublings = 1;
2463
2464 while( ratio != 0 )
2465 {
2466 ++doublings;
2467 ratio >>= 1;
2468 }
2469
2470 if( ++ssl->renego_records_seen > doublings )
2471 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002473 return( 0 );
2474 }
2475 }
2476
2477 return( ssl_write_hello_request( ssl ) );
2478}
2479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002481
Paul Bakker5121ce52009-01-03 21:22:43 +00002482/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002483 * Fill the input message buffer by appending data to it.
2484 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002485 *
2486 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2487 * available (from this read and/or a previous one). Otherwise, an error code
2488 * is returned (possibly EOF or WANT_READ).
2489 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002490 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2491 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2492 * since we always read a whole datagram at once.
2493 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002494 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002495 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498{
Paul Bakker23986e52011-04-24 08:57:21 +00002499 int ret;
2500 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002504 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002507 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002509 }
2510
Angus Grattond8213d02016-05-25 20:56:48 +10002511 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002515 }
2516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002520 uint32_t timeout;
2521
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002522 /* Just to be sure */
2523 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2524 {
2525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2526 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2528 }
2529
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002530 /*
2531 * The point is, we need to always read a full datagram at once, so we
2532 * sometimes read more then requested, and handle the additional data.
2533 * It could be the rest of the current record (while fetching the
2534 * header) and/or some other records in the same datagram.
2535 */
2536
2537 /*
2538 * Move to the next record in the already read datagram if applicable
2539 */
2540 if( ssl->next_record_offset != 0 )
2541 {
2542 if( ssl->in_left < ssl->next_record_offset )
2543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002546 }
2547
2548 ssl->in_left -= ssl->next_record_offset;
2549
2550 if( ssl->in_left != 0 )
2551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002553 ssl->next_record_offset ) );
2554 memmove( ssl->in_hdr,
2555 ssl->in_hdr + ssl->next_record_offset,
2556 ssl->in_left );
2557 }
2558
2559 ssl->next_record_offset = 0;
2560 }
2561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002564
2565 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002566 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002567 */
2568 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002571 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002572 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002573
2574 /*
2575 * A record can't be split accross datagrams. If we need to read but
2576 * are not at the beginning of a new record, the caller did something
2577 * wrong.
2578 */
2579 if( ssl->in_left != 0 )
2580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002583 }
2584
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002585 /*
2586 * Don't even try to read if time's out already.
2587 * This avoids by-passing the timer when repeatedly receiving messages
2588 * that will end up being dropped.
2589 */
2590 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002593 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002594 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002595 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002596 {
Angus Grattond8213d02016-05-25 20:56:48 +10002597 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002600 timeout = ssl->handshake->retransmit_timeout;
2601 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002602 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002605
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002606 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002607 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2608 timeout );
2609 else
2610 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002613
2614 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002616 }
2617
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002618 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002621 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002624 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002625 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002628 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002629 }
2630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002634 return( ret );
2635 }
2636
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002637 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002640 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002642 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002643 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002646 return( ret );
2647 }
2648
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002649 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002652 }
2653
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 if( ret < 0 )
2655 return( ret );
2656
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002657 ssl->in_left = ret;
2658 }
2659 else
2660#endif
2661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002663 ssl->in_left, nb_want ) );
2664
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002665 while( ssl->in_left < nb_want )
2666 {
2667 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002668
2669 if( ssl_check_timer( ssl ) != 0 )
2670 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2671 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002672 {
2673 if( ssl->f_recv_timeout != NULL )
2674 {
2675 ret = ssl->f_recv_timeout( ssl->p_bio,
2676 ssl->in_hdr + ssl->in_left, len,
2677 ssl->conf->read_timeout );
2678 }
2679 else
2680 {
2681 ret = ssl->f_recv( ssl->p_bio,
2682 ssl->in_hdr + ssl->in_left, len );
2683 }
2684 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002687 ssl->in_left, nb_want ) );
2688 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002689
2690 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002692
2693 if( ret < 0 )
2694 return( ret );
2695
mohammad160352aecb92018-03-28 23:41:40 -07002696 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002697 {
Darryl Green11999bb2018-03-13 15:22:58 +00002698 MBEDTLS_SSL_DEBUG_MSG( 1,
2699 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002700 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2702 }
2703
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002704 ssl->in_left += ret;
2705 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 }
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
2710 return( 0 );
2711}
2712
2713/*
2714 * Flush any data not yet written
2715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002717{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002718 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002719 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002723 if( ssl->f_send == NULL )
2724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002726 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002728 }
2729
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002730 /* Avoid incrementing counter if data is flushed */
2731 if( ssl->out_left == 0 )
2732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002734 return( 0 );
2735 }
2736
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 while( ssl->out_left > 0 )
2738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2740 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Hanno Becker2b1e3542018-08-06 11:19:13 +01002742 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002743 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
2747 if( ret <= 0 )
2748 return( ret );
2749
mohammad160352aecb92018-03-28 23:41:40 -07002750 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002751 {
Darryl Green11999bb2018-03-13 15:22:58 +00002752 MBEDTLS_SSL_DEBUG_MSG( 1,
2753 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002754 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2756 }
2757
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 ssl->out_left -= ret;
2759 }
2760
Hanno Becker2b1e3542018-08-06 11:19:13 +01002761#if defined(MBEDTLS_SSL_PROTO_DTLS)
2762 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2763 {
2764 ssl->out_hdr = ssl->out_buf;
2765 }
2766 else
2767#endif
2768 {
2769 ssl->out_hdr = ssl->out_buf + 8;
2770 }
2771 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
2775 return( 0 );
2776}
2777
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002778/*
2779 * Functions to handle the DTLS retransmission state machine
2780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002782/*
2783 * Append current handshake message to current outgoing flight
2784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002786{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2789 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2790 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002791
2792 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002793 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002797 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002798 }
2799
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002800 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002801 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002804 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002805 }
2806
2807 /* Copy current handshake message with headers */
2808 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2809 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002810 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002811 msg->next = NULL;
2812
2813 /* Append to the current flight */
2814 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002815 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002816 else
2817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002819 while( cur->next != NULL )
2820 cur = cur->next;
2821 cur->next = msg;
2822 }
2823
Hanno Becker3b235902018-08-06 09:54:53 +01002824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825 return( 0 );
2826}
2827
2828/*
2829 * Free the current flight of handshake messages
2830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 mbedtls_ssl_flight_item *cur = flight;
2834 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002835
2836 while( cur != NULL )
2837 {
2838 next = cur->next;
2839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 mbedtls_free( cur->p );
2841 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002842
2843 cur = next;
2844 }
2845}
2846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2848static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002849#endif
2850
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002851/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002852 * Swap transform_out and out_ctr with the alternative ones
2853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002857 unsigned char tmp_out_ctr[8];
2858
2859 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002862 return;
2863 }
2864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002866
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002867 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 tmp_transform = ssl->transform_out;
2869 ssl->transform_out = ssl->handshake->alt_transform_out;
2870 ssl->handshake->alt_transform_out = tmp_transform;
2871
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002873 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2874 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002875 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002876
2877 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002878 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2881 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2886 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002887 }
2888 }
2889#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002890}
2891
2892/*
2893 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002894 */
2895int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2896{
2897 int ret = 0;
2898
2899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2900
2901 ret = mbedtls_ssl_flight_transmit( ssl );
2902
2903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2904
2905 return( ret );
2906}
2907
2908/*
2909 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002910 *
2911 * Need to remember the current message in case flush_output returns
2912 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002913 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002914 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002915int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002916{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002917 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002921 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002923
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002924 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002925 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002926 ssl_swap_epochs( ssl );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002929 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002930
2931 while( ssl->handshake->cur_msg != NULL )
2932 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002933 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002934 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002935
Hanno Beckere1dcb032018-08-17 16:47:58 +01002936 int const is_finished =
2937 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2938 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2939
Hanno Becker04da1892018-08-14 13:22:10 +01002940 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2941 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2942
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002943 /* Swap epochs before sending Finished: we can't do it after
2944 * sending ChangeCipherSpec, in case write returns WANT_READ.
2945 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002946 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002947 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002949 ssl_swap_epochs( ssl );
2950 }
2951
Hanno Becker67bc7c32018-08-06 11:33:50 +01002952 ret = ssl_get_remaining_payload_in_datagram( ssl );
2953 if( ret < 0 )
2954 return( ret );
2955 max_frag_len = (size_t) ret;
2956
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002957 /* CCS is copied as is, while HS messages may need fragmentation */
2958 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2959 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002960 if( max_frag_len == 0 )
2961 {
2962 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2963 return( ret );
2964
2965 continue;
2966 }
2967
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002968 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002969 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002970 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002971
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002972 /* Update position inside current message */
2973 ssl->handshake->cur_msg_p += cur->len;
2974 }
2975 else
2976 {
2977 const unsigned char * const p = ssl->handshake->cur_msg_p;
2978 const size_t hs_len = cur->len - 12;
2979 const size_t frag_off = p - ( cur->p + 12 );
2980 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002981 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002982
Hanno Beckere1dcb032018-08-17 16:47:58 +01002983 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002984 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002985 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002986 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002987
2988 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2989 return( ret );
2990
2991 continue;
2992 }
2993 max_hs_frag_len = max_frag_len - 12;
2994
2995 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2996 max_hs_frag_len : rem_len;
2997
2998 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002999 {
3000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003001 (unsigned) cur_hs_frag_len,
3002 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003003 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003004
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003005 /* Messages are stored with handshake headers as if not fragmented,
3006 * copy beginning of headers then fill fragmentation fields.
3007 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3008 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003009
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003010 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3011 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3012 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3013
Hanno Becker67bc7c32018-08-06 11:33:50 +01003014 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3015 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3016 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003017
3018 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3019
Hanno Becker67bc7c32018-08-06 11:33:50 +01003020 /* Copy the handshame message content and set records fields */
3021 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3022 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003023 ssl->out_msgtype = cur->type;
3024
3025 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003026 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003027 }
3028
3029 /* If done with the current message move to the next one if any */
3030 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3031 {
3032 if( cur->next != NULL )
3033 {
3034 ssl->handshake->cur_msg = cur->next;
3035 ssl->handshake->cur_msg_p = cur->next->p + 12;
3036 }
3037 else
3038 {
3039 ssl->handshake->cur_msg = NULL;
3040 ssl->handshake->cur_msg_p = NULL;
3041 }
3042 }
3043
3044 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003045 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003048 return( ret );
3049 }
3050 }
3051
Hanno Becker67bc7c32018-08-06 11:33:50 +01003052 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3053 return( ret );
3054
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003055 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3057 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003058 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003061 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3062 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003063
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003065
3066 return( 0 );
3067}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003068
3069/*
3070 * To be called when the last message of an incoming flight is received.
3071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003073{
3074 /* We won't need to resend that one any more */
3075 ssl_flight_free( ssl->handshake->flight );
3076 ssl->handshake->flight = NULL;
3077 ssl->handshake->cur_msg = NULL;
3078
3079 /* The next incoming flight will start with this msg_seq */
3080 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3081
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003082 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003083 ssl_set_timer( ssl, 0 );
3084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3086 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003089 }
3090 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003092}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003093
3094/*
3095 * To be called when the last message of an outgoing flight is send.
3096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003098{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003099 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003100 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3103 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003106 }
3107 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003111
Paul Bakker5121ce52009-01-03 21:22:43 +00003112/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003113 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003115
3116/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003117 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003118 *
3119 * - fill in handshake headers
3120 * - update handshake checksum
3121 * - DTLS: save message for resending
3122 * - then pass to the record layer
3123 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003124 * DTLS: except for HelloRequest, messages are only queued, and will only be
3125 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003126 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003127 * Inputs:
3128 * - ssl->out_msglen: 4 + actual handshake message len
3129 * (4 is the size of handshake headers for TLS)
3130 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3131 * - ssl->out_msg + 4: the handshake message body
3132 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003133 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003134 * - ssl->out_msglen: the length of the record contents
3135 * (including handshake headers but excluding record headers)
3136 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003137 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003138int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003139{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003140 int ret;
3141 const size_t hs_len = ssl->out_msglen - 4;
3142 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3145
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003146 /*
3147 * Sanity checks
3148 */
Hanno Becker2c98db22018-08-22 16:05:47 +01003149 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003150 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3151 {
Hanno Becker2c98db22018-08-22 16:05:47 +01003152 /* In SSLv3, the client might send a NoCertificate alert. */
3153#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3154 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3155 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3156 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3157#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3158 {
3159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3161 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003164 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3165 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3166 ssl->handshake == NULL )
3167 {
3168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3170 }
3171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003174 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003176 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003179 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003181
Hanno Beckerb50a2532018-08-06 11:52:54 +01003182 /* Double-check that we did not exceed the bounds
3183 * of the outgoing record buffer.
3184 * This should never fail as the various message
3185 * writing functions must obey the bounds of the
3186 * outgoing record buffer, but better be safe.
3187 *
3188 * Note: We deliberately do not check for the MTU or MFL here.
3189 */
3190 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3191 {
3192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3193 "size %u, maximum %u",
3194 (unsigned) ssl->out_msglen,
3195 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3197 }
3198
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003199 /*
3200 * Fill handshake headers
3201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003204 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3205 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3206 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003207
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003208 /*
3209 * DTLS has additional fields in the Handshake layer,
3210 * between the length field and the actual payload:
3211 * uint16 message_seq;
3212 * uint24 fragment_offset;
3213 * uint24 fragment_length;
3214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003217 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003218 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003219 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003220 {
3221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3222 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003223 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003224 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3226 }
3227
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003228 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003229 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003230
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003231 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003232 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003233 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003234 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3235 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3236 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003237 }
3238 else
3239 {
3240 ssl->out_msg[4] = 0;
3241 ssl->out_msg[5] = 0;
3242 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003243
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003244 /* Handshake hashes are computed without fragmentation,
3245 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003246 memset( ssl->out_msg + 6, 0x00, 3 );
3247 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003248 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003250
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003251 /* Update running hashes of hanshake messages seen */
3252 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3253 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254 }
3255
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003256 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003259 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003260 {
3261 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003264 return( ret );
3265 }
3266 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003267 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003268#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003269 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003270 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003271 {
3272 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3273 return( ret );
3274 }
3275 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003276
3277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3278
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003279 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003280}
3281
3282/*
3283 * Record layer functions
3284 */
3285
3286/*
3287 * Write current record.
3288 *
3289 * Uses:
3290 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3291 * - ssl->out_msglen: length of the record content (excl headers)
3292 * - ssl->out_msg: record content
3293 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003294int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003295{
3296 int ret, done = 0;
3297 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003298 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003299
3300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003303 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003305 {
3306 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309 return( ret );
3310 }
3311
3312 len = ssl->out_msglen;
3313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3317 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 ret = mbedtls_ssl_hw_record_write( ssl );
3322 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3325 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003326 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003327
3328 if( ret == 0 )
3329 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003332 if( !done )
3333 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003334 unsigned i;
3335 size_t protected_record_size;
3336
Paul Bakker05ef8352012-05-08 09:17:57 +00003337 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003339 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003340
Hanno Becker19859472018-08-06 09:40:20 +01003341 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003342 ssl->out_len[0] = (unsigned char)( len >> 8 );
3343 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003344
Paul Bakker48916f92012-09-16 19:57:18 +00003345 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003346 {
3347 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003350 return( ret );
3351 }
3352
3353 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003354 ssl->out_len[0] = (unsigned char)( len >> 8 );
3355 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003356 }
3357
Hanno Becker2b1e3542018-08-06 11:19:13 +01003358 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3359
3360#if defined(MBEDTLS_SSL_PROTO_DTLS)
3361 /* In case of DTLS, double-check that we don't exceed
3362 * the remaining space in the datagram. */
3363 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3364 {
3365 ret = ssl_get_maximum_datagram_size( ssl );
3366 if( ret < 0 )
3367 return( ret );
3368
3369 if( protected_record_size > (size_t) ret )
3370 {
3371 /* Should never happen */
3372 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3373 }
3374 }
3375#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003378 "version = [%d:%d], msglen = %d",
3379 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3380
Paul Bakker05ef8352012-05-08 09:17:57 +00003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003383 ssl->out_hdr, protected_record_size );
3384
3385 ssl->out_left += protected_record_size;
3386 ssl->out_hdr += protected_record_size;
3387 ssl_update_out_pointers( ssl, ssl->transform_out );
3388
Hanno Becker04484622018-08-06 09:49:38 +01003389 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3390 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3391 break;
3392
3393 /* The loop goes to its end iff the counter is wrapping */
3394 if( i == ssl_ep_len( ssl ) )
3395 {
3396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3397 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 }
3400
Hanno Becker67bc7c32018-08-06 11:33:50 +01003401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003402 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3403 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003404 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003405 size_t remaining;
3406 ret = ssl_get_remaining_payload_in_datagram( ssl );
3407 if( ret < 0 )
3408 {
3409 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3410 ret );
3411 return( ret );
3412 }
3413
3414 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003415 if( remaining == 0 )
3416 flush = SSL_FORCE_FLUSH;
3417 else
3418 {
Hanno Becker513815a2018-08-20 11:56:09 +01003419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003420 }
3421 }
3422#endif /* MBEDTLS_SSL_PROTO_DTLS */
3423
3424 if( ( flush == SSL_FORCE_FLUSH ) &&
3425 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 return( ret );
3429 }
3430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432
3433 return( 0 );
3434}
3435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003437/*
3438 * Mark bits in bitmask (used for DTLS HS reassembly)
3439 */
3440static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3441{
3442 unsigned int start_bits, end_bits;
3443
3444 start_bits = 8 - ( offset % 8 );
3445 if( start_bits != 8 )
3446 {
3447 size_t first_byte_idx = offset / 8;
3448
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003449 /* Special case */
3450 if( len <= start_bits )
3451 {
3452 for( ; len != 0; len-- )
3453 mask[first_byte_idx] |= 1 << ( start_bits - len );
3454
3455 /* Avoid potential issues with offset or len becoming invalid */
3456 return;
3457 }
3458
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003459 offset += start_bits; /* Now offset % 8 == 0 */
3460 len -= start_bits;
3461
3462 for( ; start_bits != 0; start_bits-- )
3463 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3464 }
3465
3466 end_bits = len % 8;
3467 if( end_bits != 0 )
3468 {
3469 size_t last_byte_idx = ( offset + len ) / 8;
3470
3471 len -= end_bits; /* Now len % 8 == 0 */
3472
3473 for( ; end_bits != 0; end_bits-- )
3474 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3475 }
3476
3477 memset( mask + offset / 8, 0xFF, len / 8 );
3478}
3479
3480/*
3481 * Check that bitmask is full
3482 */
3483static int ssl_bitmask_check( unsigned char *mask, size_t len )
3484{
3485 size_t i;
3486
3487 for( i = 0; i < len / 8; i++ )
3488 if( mask[i] != 0xFF )
3489 return( -1 );
3490
3491 for( i = 0; i < len % 8; i++ )
3492 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3493 return( -1 );
3494
3495 return( 0 );
3496}
3497
3498/*
3499 * Reassemble fragmented DTLS handshake messages.
3500 *
3501 * Use a temporary buffer for reassembly, divided in two parts:
3502 * - the first holds the reassembled message (including handshake header),
3503 * - the second holds a bitmask indicating which parts of the message
3504 * (excluding headers) have been received so far.
3505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003507{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003508 unsigned char *msg, *bitmask;
3509 size_t frag_len, frag_off;
3510 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3511
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003512 if( ssl->handshake == NULL )
3513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3515 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003516 }
3517
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003518 /*
3519 * For first fragment, check size and allocate buffer
3520 */
3521 if( ssl->handshake->hs_msg == NULL )
3522 {
3523 size_t alloc_len;
3524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003526 msg_len ) );
3527
Angus Grattond8213d02016-05-25 20:56:48 +10003528 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3531 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003532 }
3533
3534 /* The bitmask needs one bit per byte of message excluding header */
3535 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3536
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003537 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003538 if( ssl->handshake->hs_msg == NULL )
3539 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003542 }
3543
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003544 /* Prepare final header: copy msg_type, length and message_seq,
3545 * then add standardised fragment_offset and fragment_length */
3546 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3547 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3548 memcpy( ssl->handshake->hs_msg + 9,
3549 ssl->handshake->hs_msg + 1, 3 );
3550 }
3551 else
3552 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003553 /* Make sure msg_type and length are consistent */
3554 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3557 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003558 }
3559 }
3560
3561 msg = ssl->handshake->hs_msg + 12;
3562 bitmask = msg + msg_len;
3563
3564 /*
3565 * Check and copy current fragment
3566 */
3567 frag_off = ( ssl->in_msg[6] << 16 ) |
3568 ( ssl->in_msg[7] << 8 ) |
3569 ssl->in_msg[8];
3570 frag_len = ( ssl->in_msg[9] << 16 ) |
3571 ( ssl->in_msg[10] << 8 ) |
3572 ssl->in_msg[11];
3573
3574 if( frag_off + frag_len > msg_len )
3575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003577 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003579 }
3580
3581 if( frag_len + 12 > ssl->in_msglen )
3582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003584 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003586 }
3587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003589 frag_off, frag_len ) );
3590
3591 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3592 ssl_bitmask_set( bitmask, frag_off, frag_len );
3593
3594 /*
3595 * Do we have the complete message by now?
3596 * If yes, finalize it, else ask to read the next record.
3597 */
3598 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003601 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003602 }
3603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003605
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003606 if( frag_len + 12 < ssl->in_msglen )
3607 {
3608 /*
3609 * We'got more handshake messages in the same record.
3610 * This case is not handled now because no know implementation does
3611 * that and it's hard to test, so we prefer to fail cleanly for now.
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3614 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003615 }
3616
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003617 if( ssl->in_left > ssl->next_record_offset )
3618 {
3619 /*
3620 * We've got more data in the buffer after the current record,
3621 * that we don't want to overwrite. Move it before writing the
3622 * reassembled message, and adjust in_left and next_record_offset.
3623 */
3624 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3625 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3626 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3627
3628 /* First compute and check new lengths */
3629 ssl->next_record_offset = new_remain - ssl->in_hdr;
3630 ssl->in_left = ssl->next_record_offset + remain_len;
3631
Angus Grattond8213d02016-05-25 20:56:48 +10003632 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003633 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3636 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003637 }
3638
3639 memmove( new_remain, cur_remain, remain_len );
3640 }
3641
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003642 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003645 ssl->handshake->hs_msg = NULL;
3646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003648 ssl->in_msg, ssl->in_hslen );
3649
3650 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003653
Simon Butcher99000142016-10-13 17:21:01 +01003654int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003659 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003661 }
3662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003664 ( ssl->in_msg[1] << 16 ) |
3665 ( ssl->in_msg[2] << 8 ) |
3666 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003669 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003670 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003674 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003675 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003676 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003677
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003678 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003679 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3680 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3681 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3682 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003683 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003684 /* Retransmit only on last message from previous flight, to avoid
3685 * too many retransmissions.
3686 * Besides, No sane server ever retransmits HelloVerifyRequest */
3687 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003691 "message_seq = %d, start_of_flight = %d",
3692 recv_msg_seq,
3693 ssl->handshake->in_flight_start_seq ) );
3694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003698 return( ret );
3699 }
3700 }
3701 else
3702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003704 "message_seq = %d, expected = %d",
3705 recv_msg_seq,
3706 ssl->handshake->in_msg_seq ) );
3707 }
3708
Hanno Becker90333da2017-10-10 11:27:13 +01003709 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003710 }
3711 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003712
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003713 /* Reassemble if current message is fragmented or reassembly is
3714 * already in progress */
3715 if( ssl->in_msglen < ssl->in_hslen ||
3716 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3717 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3718 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003721
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003722 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003725 return( ret );
3726 }
3727 }
3728 }
3729 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003731 /* With TLS we don't handle fragmentation (for now) */
3732 if( ssl->in_msglen < ssl->in_hslen )
3733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3735 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003736 }
3737
Simon Butcher99000142016-10-13 17:21:01 +01003738 return( 0 );
3739}
3740
3741void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3742{
3743
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003744 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3745 ssl->handshake != NULL )
3746 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003747 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003748 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003749
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003750 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003752 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003753 ssl->handshake != NULL )
3754 {
3755 ssl->handshake->in_msg_seq++;
3756 }
3757#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003758}
3759
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003760/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003761 * DTLS anti-replay: RFC 6347 4.1.2.6
3762 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003763 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3764 * Bit n is set iff record number in_window_top - n has been seen.
3765 *
3766 * Usually, in_window_top is the last record number seen and the lsb of
3767 * in_window is set. The only exception is the initial state (record number 0
3768 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3771static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003772{
3773 ssl->in_window_top = 0;
3774 ssl->in_window = 0;
3775}
3776
3777static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3778{
3779 return( ( (uint64_t) buf[0] << 40 ) |
3780 ( (uint64_t) buf[1] << 32 ) |
3781 ( (uint64_t) buf[2] << 24 ) |
3782 ( (uint64_t) buf[3] << 16 ) |
3783 ( (uint64_t) buf[4] << 8 ) |
3784 ( (uint64_t) buf[5] ) );
3785}
3786
3787/*
3788 * Return 0 if sequence number is acceptable, -1 otherwise
3789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003791{
3792 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3793 uint64_t bit;
3794
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003795 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003796 return( 0 );
3797
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003798 if( rec_seqnum > ssl->in_window_top )
3799 return( 0 );
3800
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003801 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003802
3803 if( bit >= 64 )
3804 return( -1 );
3805
3806 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3807 return( -1 );
3808
3809 return( 0 );
3810}
3811
3812/*
3813 * Update replay window on new validated record
3814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003816{
3817 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3818
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003819 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003820 return;
3821
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003822 if( rec_seqnum > ssl->in_window_top )
3823 {
3824 /* Update window_top and the contents of the window */
3825 uint64_t shift = rec_seqnum - ssl->in_window_top;
3826
3827 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003828 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003829 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003830 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003831 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003832 ssl->in_window |= 1;
3833 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003834
3835 ssl->in_window_top = rec_seqnum;
3836 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003837 else
3838 {
3839 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003840 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003841
3842 if( bit < 64 ) /* Always true, but be extra sure */
3843 ssl->in_window |= (uint64_t) 1 << bit;
3844 }
3845}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003847
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003848#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003849/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003850static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3851
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003852/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003853 * Without any SSL context, check if a datagram looks like a ClientHello with
3854 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003855 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003856 *
3857 * - if cookie is valid, return 0
3858 * - if ClientHello looks superficially valid but cookie is not,
3859 * fill obuf and set olen, then
3860 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3861 * - otherwise return a specific error code
3862 */
3863static int ssl_check_dtls_clihlo_cookie(
3864 mbedtls_ssl_cookie_write_t *f_cookie_write,
3865 mbedtls_ssl_cookie_check_t *f_cookie_check,
3866 void *p_cookie,
3867 const unsigned char *cli_id, size_t cli_id_len,
3868 const unsigned char *in, size_t in_len,
3869 unsigned char *obuf, size_t buf_len, size_t *olen )
3870{
3871 size_t sid_len, cookie_len;
3872 unsigned char *p;
3873
3874 if( f_cookie_write == NULL || f_cookie_check == NULL )
3875 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3876
3877 /*
3878 * Structure of ClientHello with record and handshake headers,
3879 * and expected values. We don't need to check a lot, more checks will be
3880 * done when actually parsing the ClientHello - skipping those checks
3881 * avoids code duplication and does not make cookie forging any easier.
3882 *
3883 * 0-0 ContentType type; copied, must be handshake
3884 * 1-2 ProtocolVersion version; copied
3885 * 3-4 uint16 epoch; copied, must be 0
3886 * 5-10 uint48 sequence_number; copied
3887 * 11-12 uint16 length; (ignored)
3888 *
3889 * 13-13 HandshakeType msg_type; (ignored)
3890 * 14-16 uint24 length; (ignored)
3891 * 17-18 uint16 message_seq; copied
3892 * 19-21 uint24 fragment_offset; copied, must be 0
3893 * 22-24 uint24 fragment_length; (ignored)
3894 *
3895 * 25-26 ProtocolVersion client_version; (ignored)
3896 * 27-58 Random random; (ignored)
3897 * 59-xx SessionID session_id; 1 byte len + sid_len content
3898 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3899 * ...
3900 *
3901 * Minimum length is 61 bytes.
3902 */
3903 if( in_len < 61 ||
3904 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3905 in[3] != 0 || in[4] != 0 ||
3906 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3907 {
3908 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3909 }
3910
3911 sid_len = in[59];
3912 if( sid_len > in_len - 61 )
3913 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3914
3915 cookie_len = in[60 + sid_len];
3916 if( cookie_len > in_len - 60 )
3917 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3918
3919 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3920 cli_id, cli_id_len ) == 0 )
3921 {
3922 /* Valid cookie */
3923 return( 0 );
3924 }
3925
3926 /*
3927 * If we get here, we've got an invalid cookie, let's prepare HVR.
3928 *
3929 * 0-0 ContentType type; copied
3930 * 1-2 ProtocolVersion version; copied
3931 * 3-4 uint16 epoch; copied
3932 * 5-10 uint48 sequence_number; copied
3933 * 11-12 uint16 length; olen - 13
3934 *
3935 * 13-13 HandshakeType msg_type; hello_verify_request
3936 * 14-16 uint24 length; olen - 25
3937 * 17-18 uint16 message_seq; copied
3938 * 19-21 uint24 fragment_offset; copied
3939 * 22-24 uint24 fragment_length; olen - 25
3940 *
3941 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3942 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3943 *
3944 * Minimum length is 28.
3945 */
3946 if( buf_len < 28 )
3947 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3948
3949 /* Copy most fields and adapt others */
3950 memcpy( obuf, in, 25 );
3951 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3952 obuf[25] = 0xfe;
3953 obuf[26] = 0xff;
3954
3955 /* Generate and write actual cookie */
3956 p = obuf + 28;
3957 if( f_cookie_write( p_cookie,
3958 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3959 {
3960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3961 }
3962
3963 *olen = p - obuf;
3964
3965 /* Go back and fill length fields */
3966 obuf[27] = (unsigned char)( *olen - 28 );
3967
3968 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3969 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3970 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3971
3972 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3973 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3974
3975 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3976}
3977
3978/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003979 * Handle possible client reconnect with the same UDP quadruplet
3980 * (RFC 6347 Section 4.2.8).
3981 *
3982 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3983 * that looks like a ClientHello.
3984 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003985 * - if the input looks like a ClientHello without cookies,
3986 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003987 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003988 * - if the input looks like a ClientHello with a valid cookie,
3989 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003990 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003991 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003992 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003993 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003994 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3995 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003996 */
3997static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3998{
3999 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004000 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004001
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004002 ret = ssl_check_dtls_clihlo_cookie(
4003 ssl->conf->f_cookie_write,
4004 ssl->conf->f_cookie_check,
4005 ssl->conf->p_cookie,
4006 ssl->cli_id, ssl->cli_id_len,
4007 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004008 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004009
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004010 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4011
4012 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004013 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004014 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004015 * If the error is permanent we'll catch it later,
4016 * if it's not, then hopefully it'll work next time. */
4017 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4018
4019 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004020 }
4021
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004022 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004023 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004024 /* Got a valid cookie, partially reset context */
4025 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4026 {
4027 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4028 return( ret );
4029 }
4030
4031 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004032 }
4033
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004034 return( ret );
4035}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004036#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004037
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004038/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004039 * ContentType type;
4040 * ProtocolVersion version;
4041 * uint16 epoch; // DTLS only
4042 * uint48 sequence_number; // DTLS only
4043 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004044 *
4045 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004046 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004047 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4048 *
4049 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004050 * 1. proceed with the record if this function returns 0
4051 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4052 * 3. return CLIENT_RECONNECT if this function return that value
4053 * 4. drop the whole datagram if this function returns anything else.
4054 * Point 2 is needed when the peer is resending, and we have already received
4055 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004058{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004059 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 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 +02004062
Paul Bakker5121ce52009-01-03 21:22:43 +00004063 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004064 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004065 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004068 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004069 ssl->in_msgtype,
4070 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004071
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004072 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4074 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4075 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4076 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004079
4080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004081 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4082 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004083 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4084#endif /* MBEDTLS_SSL_PROTO_DTLS */
4085 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4086 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004089 }
4090
4091 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004092 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4095 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004096 }
4097
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004098 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4101 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004102 }
4103
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004104 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004105 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004106 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4109 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004110 }
4111
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004112 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004113 * DTLS-related tests.
4114 * Check epoch before checking length constraint because
4115 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4116 * message gets duplicated before the corresponding Finished message,
4117 * the second ChangeCipherSpec should be discarded because it belongs
4118 * to an old epoch, but not because its length is shorter than
4119 * the minimum record length for packets using the new record transform.
4120 * Note that these two kinds of failures are handled differently,
4121 * as an unexpected record is silently skipped but an invalid
4122 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004123 */
4124#if defined(MBEDTLS_SSL_PROTO_DTLS)
4125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4126 {
4127 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4128
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004129 /* Check epoch (and sequence number) with DTLS */
4130 if( rec_epoch != ssl->in_epoch )
4131 {
4132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4133 "expected %d, received %d",
4134 ssl->in_epoch, rec_epoch ) );
4135
4136#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4137 /*
4138 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4139 * access the first byte of record content (handshake type), as we
4140 * have an active transform (possibly iv_len != 0), so use the
4141 * fact that the record header len is 13 instead.
4142 */
4143 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4144 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4145 rec_epoch == 0 &&
4146 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4147 ssl->in_left > 13 &&
4148 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4149 {
4150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4151 "from the same port" ) );
4152 return( ssl_handle_possible_reconnect( ssl ) );
4153 }
4154 else
4155#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4156 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4157 }
4158
4159#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4160 /* Replay detection only works for the current epoch */
4161 if( rec_epoch == ssl->in_epoch &&
4162 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4163 {
4164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4165 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4166 }
4167#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004168
4169 /* Drop unexpected ChangeCipherSpec messages */
4170 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4171 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4172 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4173 {
4174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4175 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4176 }
4177
4178 /* Drop unexpected ApplicationData records,
4179 * except at the beginning of renegotiations */
4180 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4181 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4182#if defined(MBEDTLS_SSL_RENEGOTIATION)
4183 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4184 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4185#endif
4186 )
4187 {
4188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4189 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4190 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004191 }
4192#endif /* MBEDTLS_SSL_PROTO_DTLS */
4193
Hanno Becker52c6dc62017-05-26 16:07:36 +01004194
4195 /* Check length against bounds of the current transform and version */
4196 if( ssl->transform_in == NULL )
4197 {
4198 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004199 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004200 {
4201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4202 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4203 }
4204 }
4205 else
4206 {
4207 if( ssl->in_msglen < ssl->transform_in->minlen )
4208 {
4209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4210 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4211 }
4212
4213#if defined(MBEDTLS_SSL_PROTO_SSL3)
4214 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004215 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004216 {
4217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4218 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4219 }
4220#endif
4221#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4222 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4223 /*
4224 * TLS encrypted messages can have up to 256 bytes of padding
4225 */
4226 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4227 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004228 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004229 {
4230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4231 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4232 }
4233#endif
4234 }
4235
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004236 return( 0 );
4237}
Paul Bakker5121ce52009-01-03 21:22:43 +00004238
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004239/*
4240 * If applicable, decrypt (and decompress) record content
4241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004243{
4244 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4247 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4250 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 ret = mbedtls_ssl_hw_record_read( ssl );
4255 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4258 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004259 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004260
4261 if( ret == 0 )
4262 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004263 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004265 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 {
4267 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 return( ret );
4271 }
4272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004274 ssl->in_msg, ssl->in_msglen );
4275
Angus Grattond8213d02016-05-25 20:56:48 +10004276 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4279 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280 }
4281 }
4282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004284 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004286 {
4287 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004290 return( ret );
4291 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004292 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004296 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004299 }
4300#endif
4301
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004302 return( 0 );
4303}
4304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004306
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004307/*
4308 * Read a record.
4309 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004310 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4311 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4312 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004315{
4316 int ret;
4317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004319
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004320 if( ssl->keep_current_message == 0 )
4321 {
4322 do {
Simon Butcher99000142016-10-13 17:21:01 +01004323
Hanno Becker90333da2017-10-10 11:27:13 +01004324 do ret = mbedtls_ssl_read_record_layer( ssl );
4325 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4326
4327 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004328 {
4329 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4330 return( ret );
4331 }
4332
4333 ret = mbedtls_ssl_handle_message_type( ssl );
4334
Hanno Becker90333da2017-10-10 11:27:13 +01004335 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4336 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004337
4338 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004339 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004340 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004341 return( ret );
4342 }
4343
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004344 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4345 {
4346 mbedtls_ssl_update_handshake_status( ssl );
4347 }
Simon Butcher99000142016-10-13 17:21:01 +01004348 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004349 else
Simon Butcher99000142016-10-13 17:21:01 +01004350 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4352 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004353 }
4354
4355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4356
4357 return( 0 );
4358}
4359
4360int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4361{
4362 int ret;
4363
Hanno Becker4a810fb2017-05-24 16:27:30 +01004364 /*
4365 * Step A
4366 *
4367 * Consume last content-layer message and potentially
4368 * update in_msglen which keeps track of the contents'
4369 * consumption state.
4370 *
4371 * (1) Handshake messages:
4372 * Remove last handshake message, move content
4373 * and adapt in_msglen.
4374 *
4375 * (2) Alert messages:
4376 * Consume whole record content, in_msglen = 0.
4377 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004378 * (3) Change cipher spec:
4379 * Consume whole record content, in_msglen = 0.
4380 *
4381 * (4) Application data:
4382 * Don't do anything - the record layer provides
4383 * the application data as a stream transport
4384 * and consumes through mbedtls_ssl_read only.
4385 *
4386 */
4387
4388 /* Case (1): Handshake messages */
4389 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004390 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004391 /* Hard assertion to be sure that no application data
4392 * is in flight, as corrupting ssl->in_msglen during
4393 * ssl->in_offt != NULL is fatal. */
4394 if( ssl->in_offt != NULL )
4395 {
4396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4398 }
4399
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004400 /*
4401 * Get next Handshake message in the current record
4402 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004403
Hanno Becker4a810fb2017-05-24 16:27:30 +01004404 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004405 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004406 * current handshake content: If DTLS handshake
4407 * fragmentation is used, that's the fragment
4408 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004409 * size here is faulty and should be changed at
4410 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004411 * (2) While it doesn't seem to cause problems, one
4412 * has to be very careful not to assume that in_hslen
4413 * is always <= in_msglen in a sensible communication.
4414 * Again, it's wrong for DTLS handshake fragmentation.
4415 * The following check is therefore mandatory, and
4416 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004417 * Additionally, ssl->in_hslen might be arbitrarily out of
4418 * bounds after handling a DTLS message with an unexpected
4419 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004420 */
4421 if( ssl->in_hslen < ssl->in_msglen )
4422 {
4423 ssl->in_msglen -= ssl->in_hslen;
4424 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4425 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004426
Hanno Becker4a810fb2017-05-24 16:27:30 +01004427 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4428 ssl->in_msg, ssl->in_msglen );
4429 }
4430 else
4431 {
4432 ssl->in_msglen = 0;
4433 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004434
Hanno Becker4a810fb2017-05-24 16:27:30 +01004435 ssl->in_hslen = 0;
4436 }
4437 /* Case (4): Application data */
4438 else if( ssl->in_offt != NULL )
4439 {
4440 return( 0 );
4441 }
4442 /* Everything else (CCS & Alerts) */
4443 else
4444 {
4445 ssl->in_msglen = 0;
4446 }
4447
4448 /*
4449 * Step B
4450 *
4451 * Fetch and decode new record if current one is fully consumed.
4452 *
4453 */
4454
4455 if( ssl->in_msglen > 0 )
4456 {
4457 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004458 return( 0 );
4459 }
4460
Hanno Becker4a810fb2017-05-24 16:27:30 +01004461 /* Current record either fully processed or to be discarded. */
4462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004466 return( ret );
4467 }
4468
4469 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004472 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4473 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004474 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004475 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4476 {
4477 /* Skip unexpected record (but not whole datagram) */
4478 ssl->next_record_offset = ssl->in_msglen
4479 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004480
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4482 "(header)" ) );
4483 }
4484 else
4485 {
4486 /* Skip invalid record and the rest of the datagram */
4487 ssl->next_record_offset = 0;
4488 ssl->in_left = 0;
4489
4490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4491 "(header)" ) );
4492 }
4493
4494 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004495 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004496 }
4497#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004498 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004499 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004500
4501 /*
4502 * Read and optionally decrypt the message contents
4503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4505 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004508 return( ret );
4509 }
4510
4511 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004513 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004516 if( ssl->next_record_offset < ssl->in_left )
4517 {
4518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4519 }
4520 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004521 else
4522#endif
4523 ssl->in_left = 0;
4524
4525 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004529 {
4530 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4532 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004533 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004534 /* Except when waiting for Finished as a bad mac here
4535 * probably means something went wrong in the handshake
4536 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4537 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4538 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4539 {
4540#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4541 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4542 {
4543 mbedtls_ssl_send_alert_message( ssl,
4544 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4545 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4546 }
4547#endif
4548 return( ret );
4549 }
4550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004552 if( ssl->conf->badmac_limit != 0 &&
4553 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4556 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004557 }
4558#endif
4559
Hanno Becker4a810fb2017-05-24 16:27:30 +01004560 /* As above, invalid records cause
4561 * dismissal of the whole datagram. */
4562
4563 ssl->next_record_offset = 0;
4564 ssl->in_left = 0;
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004567 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004568 }
4569
4570 return( ret );
4571 }
4572 else
4573#endif
4574 {
4575 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4577 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 mbedtls_ssl_send_alert_message( ssl,
4580 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4581 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004582 }
4583#endif
4584 return( ret );
4585 }
4586 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004587
Simon Butcher99000142016-10-13 17:21:01 +01004588 return( 0 );
4589}
4590
4591int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4592{
4593 int ret;
4594
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004595 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004596 * Handle particular types of records
4597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 {
Simon Butcher99000142016-10-13 17:21:01 +01004600 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4601 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004602 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004603 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004604 }
4605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004607 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004608 if( ssl->in_msglen != 2 )
4609 {
4610 /* Note: Standard allows for more than one 2 byte alert
4611 to be packed in a single message, but Mbed TLS doesn't
4612 currently support this. */
4613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4614 ssl->in_msglen ) );
4615 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4616 }
4617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 ssl->in_msg[0], ssl->in_msg[1] ) );
4620
4621 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004622 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004627 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 }
4630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4632 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4635 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004637
4638#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4639 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4640 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4641 {
Hanno Becker90333da2017-10-10 11:27:13 +01004642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004643 /* Will be handled when trying to parse ServerHello */
4644 return( 0 );
4645 }
4646#endif
4647
4648#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4649 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4650 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4651 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4652 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4653 {
4654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4655 /* Will be handled in mbedtls_ssl_parse_certificate() */
4656 return( 0 );
4657 }
4658#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4659
4660 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004661 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004662 }
4663
Hanno Beckerc76c6192017-06-06 10:03:17 +01004664#if defined(MBEDTLS_SSL_PROTO_DTLS)
4665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4666 ssl->handshake != NULL &&
4667 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4668 {
4669 ssl_handshake_wrapup_free_hs_transform( ssl );
4670 }
4671#endif
4672
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 return( 0 );
4674}
4675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004677{
4678 int ret;
4679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4681 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4682 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004683 {
4684 return( ret );
4685 }
4686
4687 return( 0 );
4688}
4689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004691 unsigned char level,
4692 unsigned char message )
4693{
4694 int ret;
4695
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004696 if( ssl == NULL || ssl->conf == NULL )
4697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004703 ssl->out_msglen = 2;
4704 ssl->out_msg[0] = level;
4705 ssl->out_msg[1] = message;
4706
Hanno Becker67bc7c32018-08-06 11:33:50 +01004707 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004710 return( ret );
4711 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004713
4714 return( 0 );
4715}
4716
Paul Bakker5121ce52009-01-03 21:22:43 +00004717/*
4718 * Handshake functions
4719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4721 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4722 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4723 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4724 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4725 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4726 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004727/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004729{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4735 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004736 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4737 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004740 ssl->state++;
4741 return( 0 );
4742 }
4743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004746}
4747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4755 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004756 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4757 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004760 ssl->state++;
4761 return( 0 );
4762 }
4763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004766}
Gilles Peskinef9828522017-05-03 12:28:43 +02004767
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004768#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004769/* Some certificate support -> implement write and parse */
4770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004774 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 const mbedtls_x509_crt *crt;
4776 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4781 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004782 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4783 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004786 ssl->state++;
4787 return( 0 );
4788 }
4789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004791 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004792 {
4793 if( ssl->client_auth == 0 )
4794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 ssl->state++;
4797 return( 0 );
4798 }
4799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004801 /*
4802 * If using SSLv3 and got no cert, send an Alert message
4803 * (otherwise an empty Certificate message will be sent).
4804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4806 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004807 {
4808 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4810 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4811 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 goto write_msg;
4815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#endif /* MBEDTLS_SSL_CLI_C */
4819#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004820 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4825 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
4827 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004828#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004831
4832 /*
4833 * 0 . 0 handshake type
4834 * 1 . 3 handshake length
4835 * 4 . 6 length of all certs
4836 * 7 . 9 length of cert. 1
4837 * 10 . n-1 peer certificate
4838 * n . n+2 length of cert. 2
4839 * n+3 . ... upper level cert, etc.
4840 */
4841 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004843
Paul Bakker29087132010-03-21 21:03:34 +00004844 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 {
4846 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004847 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004850 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 }
4853
4854 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4855 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4856 ssl->out_msg[i + 2] = (unsigned char)( n );
4857
4858 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4859 i += n; crt = crt->next;
4860 }
4861
4862 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4863 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4864 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4865
4866 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4868 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004870#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004871write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004872#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004873
4874 ssl->state++;
4875
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004876 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004877 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004879 return( ret );
4880 }
4881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004883
Paul Bakkered27a042013-04-18 22:46:23 +02004884 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004885}
4886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004890 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004892 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004893 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4898 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004899 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4900 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004903 ssl->state++;
4904 return( 0 );
4905 }
4906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004908 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004909 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4910 {
4911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4912 ssl->state++;
4913 return( 0 );
4914 }
4915
4916#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4917 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4918 authmode = ssl->handshake->sni_authmode;
4919#endif
4920
4921 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4922 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004923 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004924 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004926 ssl->state++;
4927 return( 0 );
4928 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004929#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004932 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004933 /* mbedtls_ssl_read_record may have sent an alert already. We
4934 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004936 return( ret );
4937 }
4938
4939 ssl->state++;
4940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#if defined(MBEDTLS_SSL_SRV_C)
4942#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004943 /*
4944 * Check if the client sent an empty certificate
4945 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004946 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004948 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004949 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4951 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4952 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004955
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004956 /* The client was asked for a certificate but didn't send
4957 one. The client should know what's going on, so we
4958 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004959 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004960 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004961 return( 0 );
4962 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 }
4965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4969 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004970 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4974 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4975 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4976 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004979
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004980 /* The client was asked for a certificate but didn't send
4981 one. The client should know what's going on, so we
4982 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004983 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004984 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004985 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004986 else
4987 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004988 }
4989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4991 MBEDTLS_SSL_PROTO_TLS1_2 */
4992#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004997 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4998 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005000 }
5001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005002 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5003 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005006 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5007 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005009 }
5010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005012
Paul Bakker5121ce52009-01-03 21:22:43 +00005013 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005016 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005017
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005018 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005022 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5023 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005025 }
5026
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005027 /* In case we tried to reuse a session but it failed */
5028 if( ssl->session_negotiate->peer_cert != NULL )
5029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5031 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005032 }
5033
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005034 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005036 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005039 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5040 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005041 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005042 }
5043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005044 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005045
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005046 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005047
5048 while( i < ssl->in_hslen )
5049 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005050 if ( i + 3 > ssl->in_hslen ) {
5051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5052 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5053 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5054 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 if( ssl->in_msg[i] != 0 )
5057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005059 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5060 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005062 }
5063
5064 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5065 | (unsigned int) ssl->in_msg[i + 2];
5066 i += 3;
5067
5068 if( n < 128 || i + n > ssl->in_hslen )
5069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005071 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5072 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005074 }
5075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005077 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005078 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005079 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005080 case 0: /*ok*/
5081 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5082 /* Ignore certificate with an unknown algorithm: maybe a
5083 prior certificate was already trusted. */
5084 break;
5085
5086 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5087 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5088 goto crt_parse_der_failed;
5089
5090 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5091 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5092 goto crt_parse_der_failed;
5093
5094 default:
5095 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5096 crt_parse_der_failed:
5097 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005099 return( ret );
5100 }
5101
5102 i += n;
5103 }
5104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005106
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005107 /*
5108 * On client, make sure the server cert doesn't change during renego to
5109 * avoid "triple handshake" attack: https://secure-resumption.com/
5110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005112 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005114 {
5115 if( ssl->session->peer_cert == NULL )
5116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005118 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5119 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005121 }
5122
5123 if( ssl->session->peer_cert->raw.len !=
5124 ssl->session_negotiate->peer_cert->raw.len ||
5125 memcmp( ssl->session->peer_cert->raw.p,
5126 ssl->session_negotiate->peer_cert->raw.p,
5127 ssl->session->peer_cert->raw.len ) != 0 )
5128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005130 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5131 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005133 }
5134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005136
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005137 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005138 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005139 mbedtls_x509_crt *ca_chain;
5140 mbedtls_x509_crl *ca_crl;
5141
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005142#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005143 if( ssl->handshake->sni_ca_chain != NULL )
5144 {
5145 ca_chain = ssl->handshake->sni_ca_chain;
5146 ca_crl = ssl->handshake->sni_ca_crl;
5147 }
5148 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005149#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005150 {
5151 ca_chain = ssl->conf->ca_chain;
5152 ca_crl = ssl->conf->ca_crl;
5153 }
5154
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005155 /*
5156 * Main check: verify certificate
5157 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005158 ret = mbedtls_x509_crt_verify_with_profile(
5159 ssl->session_negotiate->peer_cert,
5160 ca_chain, ca_crl,
5161 ssl->conf->cert_profile,
5162 ssl->hostname,
5163 &ssl->session_negotiate->verify_result,
5164 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005165
5166 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005169 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005170
5171 /*
5172 * Secondary checks: always done, but change 'ret' only if it was 0
5173 */
5174
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005175#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005178
5179 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005181 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005182 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005183 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005186 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005188 }
5189 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005190#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005193 ciphersuite_info,
5194 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005195 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005198 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005200 }
5201
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005202 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5203 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5204 * with details encoded in the verification flags. All other kinds
5205 * of error codes, including those from the user provided f_vrfy
5206 * functions, are treated as fatal and lead to a failure of
5207 * ssl_parse_certificate even if verification was optional. */
5208 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5209 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5210 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5211 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005212 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005213 }
5214
5215 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5216 {
5217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5218 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5219 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005220
5221 if( ret != 0 )
5222 {
5223 /* The certificate may have been rejected for several reasons.
5224 Pick one and send the corresponding alert. Which alert to send
5225 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005226 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5227 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5228 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5229 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5230 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5231 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5232 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5233 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5234 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5235 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5236 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5237 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5238 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5239 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5240 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5241 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5242 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5243 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5244 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5245 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005246 else
5247 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005248 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5249 alert );
5250 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005251
Hanno Beckere6706e62017-05-15 16:05:15 +01005252#if defined(MBEDTLS_DEBUG_C)
5253 if( ssl->session_negotiate->verify_result != 0 )
5254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5256 ssl->session_negotiate->verify_result ) );
5257 }
5258 else
5259 {
5260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5261 }
5262#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005263 }
5264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005266
5267 return( ret );
5268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5270 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5271 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5272 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5273 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5274 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5275 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005278{
5279 int ret;
5280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005284 ssl->out_msglen = 1;
5285 ssl->out_msg[0] = 1;
5286
Paul Bakker5121ce52009-01-03 21:22:43 +00005287 ssl->state++;
5288
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005289 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005290 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005292 return( ret );
5293 }
5294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005296
5297 return( 0 );
5298}
5299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005301{
5302 int ret;
5303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005309 return( ret );
5310 }
5311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 {
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 }
5319
5320 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005323 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5324 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 }
5327
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005328 /*
5329 * Switch to our negotiated transform and session parameters for inbound
5330 * data.
5331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005333 ssl->transform_in = ssl->transform_negotiate;
5334 ssl->session_in = ssl->session_negotiate;
5335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005337 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005340 ssl_dtls_replay_reset( ssl );
5341#endif
5342
5343 /* Increment epoch */
5344 if( ++ssl->in_epoch == 0 )
5345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005347 /* This is highly unlikely to happen for legitimate reasons, so
5348 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005350 }
5351 }
5352 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005354 memset( ssl->in_ctr, 0, 8 );
5355
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005356 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5359 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005364 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5365 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005367 }
5368 }
5369#endif
5370
Paul Bakker5121ce52009-01-03 21:22:43 +00005371 ssl->state++;
5372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005374
5375 return( 0 );
5376}
5377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5379 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005380{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005381 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5384 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5385 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005386 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005387 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005388#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5390#if defined(MBEDTLS_SHA512_C)
5391 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005392 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5393 else
5394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395#if defined(MBEDTLS_SHA256_C)
5396 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005397 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005398 else
5399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005403 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005404 }
Paul Bakker380da532012-04-18 16:10:25 +00005405}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5410 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005411 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5412 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5415#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005416 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005417#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005419 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005420#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005422}
5423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005425 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5428 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005429 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5430 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5433#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005434 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005437 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005440}
5441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5443 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5444static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005445 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005446{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005447 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5448 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005449}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005450#endif
Paul Bakker380da532012-04-18 16:10:25 +00005451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5453#if defined(MBEDTLS_SHA256_C)
5454static void ssl_update_checksum_sha256( 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_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005458}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005459#endif
Paul Bakker380da532012-04-18 16:10:25 +00005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461#if defined(MBEDTLS_SHA512_C)
5462static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005463 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005464{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005465 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005466}
Paul Bakker769075d2012-11-24 11:26:46 +01005467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005471static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005473{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005474 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005475 mbedtls_md5_context md5;
5476 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005477
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 unsigned char padbuf[48];
5479 unsigned char md5sum[16];
5480 unsigned char sha1sum[20];
5481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005483 if( !session )
5484 session = ssl->session;
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005488 mbedtls_md5_init( &md5 );
5489 mbedtls_sha1_init( &sha1 );
5490
5491 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5492 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005493
5494 /*
5495 * SSLv3:
5496 * hash =
5497 * MD5( master + pad2 +
5498 * MD5( handshake + sender + master + pad1 ) )
5499 * + SHA1( master + pad2 +
5500 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 */
5502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005504 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5505 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005509 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5510 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005514 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005515
Paul Bakker1ef83d62012-04-11 12:09:53 +00005516 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005517
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005518 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5519 mbedtls_md5_update_ret( &md5, session->master, 48 );
5520 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5521 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005522
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005523 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5524 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5525 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5526 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005527
Paul Bakker1ef83d62012-04-11 12:09:53 +00005528 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005529
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005530 mbedtls_md5_starts_ret( &md5 );
5531 mbedtls_md5_update_ret( &md5, session->master, 48 );
5532 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5533 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5534 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005536 mbedtls_sha1_starts_ret( &sha1 );
5537 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5538 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5539 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5540 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005543
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005544 mbedtls_md5_free( &md5 );
5545 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005546
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005547 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5548 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5549 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005556static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005558{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005559 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005560 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005561 mbedtls_md5_context md5;
5562 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005563 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005566 if( !session )
5567 session = ssl->session;
5568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005570
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005571 mbedtls_md5_init( &md5 );
5572 mbedtls_sha1_init( &sha1 );
5573
5574 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5575 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005576
Paul Bakker1ef83d62012-04-11 12:09:53 +00005577 /*
5578 * TLSv1:
5579 * hash = PRF( master, finished_label,
5580 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5581 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005584 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5585 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005586#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005589 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5590 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005591#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005594 ? "client finished"
5595 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005596
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005597 mbedtls_md5_finish_ret( &md5, padbuf );
5598 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005599
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005600 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005601 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005605 mbedtls_md5_free( &md5 );
5606 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005607
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005608 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5615#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005616static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005618{
5619 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005620 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005621 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005622 unsigned char padbuf[32];
5623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005625 if( !session )
5626 session = ssl->session;
5627
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005628 mbedtls_sha256_init( &sha256 );
5629
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005631
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005632 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005633
5634 /*
5635 * TLSv1.2:
5636 * hash = PRF( master, finished_label,
5637 * Hash( handshake ) )[0.11]
5638 */
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#if !defined(MBEDTLS_SHA256_ALT)
5641 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005642 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005643#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005646 ? "client finished"
5647 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005648
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005649 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005650
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005651 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005652 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005655
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005656 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005657
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005658 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005665static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005667{
5668 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005669 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005670 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005671 unsigned char padbuf[48];
5672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005674 if( !session )
5675 session = ssl->session;
5676
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005677 mbedtls_sha512_init( &sha512 );
5678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005680
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005681 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005682
5683 /*
5684 * TLSv1.2:
5685 * hash = PRF( master, finished_label,
5686 * Hash( handshake ) )[0.11]
5687 */
5688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005690 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5691 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005692#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005695 ? "client finished"
5696 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005697
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005698 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005699
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005700 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005701 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005704
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005705 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005706
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005707 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711#endif /* MBEDTLS_SHA512_C */
5712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005715{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005717
5718 /*
5719 * Free our handshake params
5720 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005721 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005723 ssl->handshake = NULL;
5724
5725 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005726 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005727 */
5728 if( ssl->transform )
5729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 mbedtls_ssl_transform_free( ssl->transform );
5731 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005732 }
5733 ssl->transform = ssl->transform_negotiate;
5734 ssl->transform_negotiate = NULL;
5735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005737}
5738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005740{
5741 int resume = ssl->handshake->resume;
5742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745#if defined(MBEDTLS_SSL_RENEGOTIATION)
5746 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005749 ssl->renego_records_seen = 0;
5750 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005751#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005752
5753 /*
5754 * Free the previous session and switch in the current one
5755 */
Paul Bakker0a597072012-09-25 21:55:46 +00005756 if( ssl->session )
5757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005759 /* RFC 7366 3.1: keep the EtM state */
5760 ssl->session_negotiate->encrypt_then_mac =
5761 ssl->session->encrypt_then_mac;
5762#endif
5763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764 mbedtls_ssl_session_free( ssl->session );
5765 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005766 }
5767 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005768 ssl->session_negotiate = NULL;
5769
Paul Bakker0a597072012-09-25 21:55:46 +00005770 /*
5771 * Add cache entry
5772 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005773 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005774 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005775 resume == 0 )
5776 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005777 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005779 }
Paul Bakker0a597072012-09-25 21:55:46 +00005780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005783 ssl->handshake->flight != NULL )
5784 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005785 /* Cancel handshake timer */
5786 ssl_set_timer( ssl, 0 );
5787
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005788 /* Keep last flight around in case we need to resend it:
5789 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005791 }
5792 else
5793#endif
5794 ssl_handshake_wrapup_free_hs_transform( ssl );
5795
Paul Bakker48916f92012-09-16 19:57:18 +00005796 ssl->state++;
5797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005799}
5800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005802{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005803 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005806
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005807 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01005808
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005809 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005810
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005811 /*
5812 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5813 * may define some other value. Currently (early 2016), no defined
5814 * ciphersuite does this (and this is unlikely to change as activity has
5815 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005820 ssl->verify_data_len = hash_len;
5821 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005822#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005823
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5826 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005827
5828 /*
5829 * In case of session resuming, invert the client and server
5830 * ChangeCipherSpec messages order.
5831 */
Paul Bakker0a597072012-09-25 21:55:46 +00005832 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005835 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005837#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005839 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005842 }
5843 else
5844 ssl->state++;
5845
Paul Bakker48916f92012-09-16 19:57:18 +00005846 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005847 * Switch to our negotiated transform and session parameters for outbound
5848 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005854 {
5855 unsigned char i;
5856
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005857 /* Remember current epoch settings for resending */
5858 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01005859 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005860
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005861 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01005862 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005863
5864 /* Increment epoch */
5865 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01005866 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005867 break;
5868
5869 /* The loop goes to its end iff the counter is wrapping */
5870 if( i == 0 )
5871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5873 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005874 }
5875 }
5876 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01005878 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005879
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005880 ssl->transform_out = ssl->transform_negotiate;
5881 ssl->session_out = ssl->session_negotiate;
5882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5884 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5889 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005890 }
5891 }
5892#endif
5893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005895 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005897#endif
5898
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005899 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005902 return( ret );
5903 }
5904
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005905#if defined(MBEDTLS_SSL_PROTO_DTLS)
5906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5907 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5908 {
5909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5910 return( ret );
5911 }
5912#endif
5913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005915
5916 return( 0 );
5917}
5918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005920#define SSL_MAX_HASH_LEN 36
5921#else
5922#define SSL_MAX_HASH_LEN 12
5923#endif
5924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005926{
Paul Bakker23986e52011-04-24 08:57:21 +00005927 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005928 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005929 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005932
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005933 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005938 return( ret );
5939 }
5940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005944 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5945 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005947 }
5948
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005949 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950#if defined(MBEDTLS_SSL_PROTO_SSL3)
5951 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005952 hash_len = 36;
5953 else
5954#endif
5955 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5958 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005961 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5962 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005964 }
5965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005967 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005970 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5971 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 }
5974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005976 ssl->verify_data_len = hash_len;
5977 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005978#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005979
Paul Bakker0a597072012-09-25 21:55:46 +00005980 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005983 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005985#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005987 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005990 }
5991 else
5992 ssl->state++;
5993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005995 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005997#endif
5998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006000
6001 return( 0 );
6002}
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006006 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6009 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6010 mbedtls_md5_init( &handshake->fin_md5 );
6011 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006012 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6013 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6016#if defined(MBEDTLS_SHA256_C)
6017 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006018 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020#if defined(MBEDTLS_SHA512_C)
6021 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006022 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006025
6026 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006027
6028#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6029 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6030 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6031#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033#if defined(MBEDTLS_DHM_C)
6034 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if defined(MBEDTLS_ECDH_C)
6037 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006038#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006039#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006040 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006041#if defined(MBEDTLS_SSL_CLI_C)
6042 handshake->ecjpake_cache = NULL;
6043 handshake->ecjpake_cache_len = 0;
6044#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006045#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006046
6047#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6048 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6049#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006050}
6051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6057 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 mbedtls_md_init( &transform->md_ctx_enc );
6060 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006061}
6062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006066}
6067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006069{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006070 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006071 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006073 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006075 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006076 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006077
6078 /*
6079 * Either the pointers are now NULL or cleared properly and can be freed.
6080 * Now allocate missing structures.
6081 */
6082 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006083 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006084 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006085 }
Paul Bakker48916f92012-09-16 19:57:18 +00006086
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006087 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006088 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006089 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006090 }
Paul Bakker48916f92012-09-16 19:57:18 +00006091
Paul Bakker82788fb2014-10-20 13:59:19 +02006092 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006093 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006094 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006095 }
Paul Bakker48916f92012-09-16 19:57:18 +00006096
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006097 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006098 if( ssl->handshake == NULL ||
6099 ssl->transform_negotiate == NULL ||
6100 ssl->session_negotiate == NULL )
6101 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 mbedtls_free( ssl->handshake );
6105 mbedtls_free( ssl->transform_negotiate );
6106 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006107
6108 ssl->handshake = NULL;
6109 ssl->transform_negotiate = NULL;
6110 ssl->session_negotiate = NULL;
6111
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006113 }
6114
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006115 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006117 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006118 ssl_handshake_params_init( ssl->handshake );
6119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006121 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6122 {
6123 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006124
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006125 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6126 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6127 else
6128 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006129
6130 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006131 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006132#endif
6133
Paul Bakker48916f92012-09-16 19:57:18 +00006134 return( 0 );
6135}
6136
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006137#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006138/* Dummy cookie callbacks for defaults */
6139static int ssl_cookie_write_dummy( void *ctx,
6140 unsigned char **p, unsigned char *end,
6141 const unsigned char *cli_id, size_t cli_id_len )
6142{
6143 ((void) ctx);
6144 ((void) p);
6145 ((void) end);
6146 ((void) cli_id);
6147 ((void) cli_id_len);
6148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006150}
6151
6152static int ssl_cookie_check_dummy( void *ctx,
6153 const unsigned char *cookie, size_t cookie_len,
6154 const unsigned char *cli_id, size_t cli_id_len )
6155{
6156 ((void) ctx);
6157 ((void) cookie);
6158 ((void) cookie_len);
6159 ((void) cli_id);
6160 ((void) cli_id_len);
6161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006163}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006164#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006165
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006166/* Once ssl->out_hdr as the address of the beginning of the
6167 * next outgoing record is set, deduce the other pointers.
6168 *
6169 * Note: For TLS, we save the implicit record sequence number
6170 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6171 * and the caller has to make sure there's space for this.
6172 */
6173
6174static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6175 mbedtls_ssl_transform *transform )
6176{
6177#if defined(MBEDTLS_SSL_PROTO_DTLS)
6178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6179 {
6180 ssl->out_ctr = ssl->out_hdr + 3;
6181 ssl->out_len = ssl->out_hdr + 11;
6182 ssl->out_iv = ssl->out_hdr + 13;
6183 }
6184 else
6185#endif
6186 {
6187 ssl->out_ctr = ssl->out_hdr - 8;
6188 ssl->out_len = ssl->out_hdr + 3;
6189 ssl->out_iv = ssl->out_hdr + 5;
6190 }
6191
6192 /* Adjust out_msg to make space for explicit IV, if used. */
6193 if( transform != NULL &&
6194 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6195 {
6196 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6197 }
6198 else
6199 ssl->out_msg = ssl->out_iv;
6200}
6201
6202/* Once ssl->in_hdr as the address of the beginning of the
6203 * next incoming record is set, deduce the other pointers.
6204 *
6205 * Note: For TLS, we save the implicit record sequence number
6206 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6207 * and the caller has to make sure there's space for this.
6208 */
6209
6210static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6211 mbedtls_ssl_transform *transform )
6212{
6213#if defined(MBEDTLS_SSL_PROTO_DTLS)
6214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6215 {
6216 ssl->in_ctr = ssl->in_hdr + 3;
6217 ssl->in_len = ssl->in_hdr + 11;
6218 ssl->in_iv = ssl->in_hdr + 13;
6219 }
6220 else
6221#endif
6222 {
6223 ssl->in_ctr = ssl->in_hdr - 8;
6224 ssl->in_len = ssl->in_hdr + 3;
6225 ssl->in_iv = ssl->in_hdr + 5;
6226 }
6227
6228 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6229 if( transform != NULL &&
6230 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6231 {
6232 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6233 }
6234 else
6235 ssl->in_msg = ssl->in_iv;
6236}
6237
Paul Bakker5121ce52009-01-03 21:22:43 +00006238/*
6239 * Initialize an SSL context
6240 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006241void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6242{
6243 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6244}
6245
6246/*
6247 * Setup an SSL context
6248 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006249
6250static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6251{
6252 /* Set the incoming and outgoing record pointers. */
6253#if defined(MBEDTLS_SSL_PROTO_DTLS)
6254 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6255 {
6256 ssl->out_hdr = ssl->out_buf;
6257 ssl->in_hdr = ssl->in_buf;
6258 }
6259 else
6260#endif /* MBEDTLS_SSL_PROTO_DTLS */
6261 {
6262 ssl->out_hdr = ssl->out_buf + 8;
6263 ssl->in_hdr = ssl->in_buf + 8;
6264 }
6265
6266 /* Derive other internal pointers. */
6267 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6268 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6269}
6270
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006271int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006272 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006273{
Paul Bakker48916f92012-09-16 19:57:18 +00006274 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006275
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006276 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006277
6278 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006279 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006280 */
Angus Grattond8213d02016-05-25 20:56:48 +10006281 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6282 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 {
Angus Grattond8213d02016-05-25 20:56:48 +10006284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6285 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6286 }
6287
6288 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6289 if( ssl->out_buf == NULL )
6290 {
6291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006293 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 }
6296
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006297 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006298
Paul Bakker48916f92012-09-16 19:57:18 +00006299 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6300 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006301
6302 return( 0 );
6303}
6304
6305/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006306 * Reset an initialized and used SSL context for re-use while retaining
6307 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006308 *
6309 * If partial is non-zero, keep data in the input buffer and client ID.
6310 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006311 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006312static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006313{
Paul Bakker48916f92012-09-16 19:57:18 +00006314 int ret;
6315
Hanno Becker7e772132018-08-10 12:38:21 +01006316#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6317 !defined(MBEDTLS_SSL_SRV_C)
6318 ((void) partial);
6319#endif
6320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006322
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006323 /* Cancel any possibly running timer */
6324 ssl_set_timer( ssl, 0 );
6325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326#if defined(MBEDTLS_SSL_RENEGOTIATION)
6327 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006328 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006329
6330 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6332 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006335
Paul Bakker7eb013f2011-10-06 12:37:39 +00006336 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006337 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006338
6339 ssl->in_msgtype = 0;
6340 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006342 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006343 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006344#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006346 ssl_dtls_replay_reset( ssl );
6347#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006348
6349 ssl->in_hslen = 0;
6350 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006351
6352 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006353
6354 ssl->out_msgtype = 0;
6355 ssl->out_msglen = 0;
6356 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6358 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006359 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006360#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006361
Hanno Becker19859472018-08-06 09:40:20 +01006362 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6363
Paul Bakker48916f92012-09-16 19:57:18 +00006364 ssl->transform_in = NULL;
6365 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006366
Hanno Becker78640902018-08-13 16:35:15 +01006367 ssl->session_in = NULL;
6368 ssl->session_out = NULL;
6369
Angus Grattond8213d02016-05-25 20:56:48 +10006370 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006371
6372#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006373 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006374#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6375 {
6376 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006377 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006378 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6381 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6384 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6387 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006388 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006389 }
6390#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006391
Paul Bakker48916f92012-09-16 19:57:18 +00006392 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 mbedtls_ssl_transform_free( ssl->transform );
6395 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006396 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006397 }
Paul Bakker48916f92012-09-16 19:57:18 +00006398
Paul Bakkerc0463502013-02-14 11:19:38 +01006399 if( ssl->session )
6400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 mbedtls_ssl_session_free( ssl->session );
6402 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006403 ssl->session = NULL;
6404 }
6405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006407 ssl->alpn_chosen = NULL;
6408#endif
6409
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006410#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006411#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006412 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006413#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006414 {
6415 mbedtls_free( ssl->cli_id );
6416 ssl->cli_id = NULL;
6417 ssl->cli_id_len = 0;
6418 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006419#endif
6420
Paul Bakker48916f92012-09-16 19:57:18 +00006421 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6422 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006423
6424 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006425}
6426
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006427/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006428 * Reset an initialized and used SSL context for re-use while retaining
6429 * all application-set variables, function pointers and data.
6430 */
6431int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6432{
6433 return( ssl_session_reset_int( ssl, 0 ) );
6434}
6435
6436/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006437 * SSL set accessors
6438 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006439void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006440{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006441 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006442}
6443
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006444void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006446 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006447}
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006450void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006451{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006452 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006453}
6454#endif
6455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006457void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006458{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006459 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006460}
6461#endif
6462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006464
6465void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6466 unsigned allow_packing )
6467{
6468 ssl->disable_datagram_packing = !allow_packing;
6469}
6470
6471void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6472 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006473{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006474 conf->hs_timeout_min = min;
6475 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006476}
6477#endif
6478
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006479void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006480{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006481 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006482}
6483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006485void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006486 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006487 void *p_vrfy )
6488{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006489 conf->f_vrfy = f_vrfy;
6490 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006493
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006494void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006495 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 void *p_rng )
6497{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006498 conf->f_rng = f_rng;
6499 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006500}
6501
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006502void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006503 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 void *p_dbg )
6505{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006506 conf->f_dbg = f_dbg;
6507 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006508}
6509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006511 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006512 mbedtls_ssl_send_t *f_send,
6513 mbedtls_ssl_recv_t *f_recv,
6514 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006515{
6516 ssl->p_bio = p_bio;
6517 ssl->f_send = f_send;
6518 ssl->f_recv = f_recv;
6519 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006520}
6521
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02006522#if defined(MBEDTLS_SSL_PROTO_DTLS)
6523void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
6524{
6525 ssl->mtu = mtu;
6526}
6527#endif
6528
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006529void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006530{
6531 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006532}
6533
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006534void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6535 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006536 mbedtls_ssl_set_timer_t *f_set_timer,
6537 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006538{
6539 ssl->p_timer = p_timer;
6540 ssl->f_set_timer = f_set_timer;
6541 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006542
6543 /* Make sure we start with no timer running */
6544 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006545}
6546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006548void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006549 void *p_cache,
6550 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6551 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006552{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006553 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006554 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006555 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559#if defined(MBEDTLS_SSL_CLI_C)
6560int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006561{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006562 int ret;
6563
6564 if( ssl == NULL ||
6565 session == NULL ||
6566 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006567 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006570 }
6571
6572 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6573 return( ret );
6574
Paul Bakker0a597072012-09-25 21:55:46 +00006575 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006576
6577 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006580
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006581void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006582 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006583{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006584 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6585 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6586 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6587 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006588}
6589
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006590void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006591 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006592 int major, int minor )
6593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006595 return;
6596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006598 return;
6599
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006600 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006601}
6602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006604void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006605 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006606{
6607 conf->cert_profile = profile;
6608}
6609
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006610/* Append a new keycert entry to a (possibly empty) list */
6611static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6612 mbedtls_x509_crt *cert,
6613 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006614{
niisato8ee24222018-06-25 19:05:48 +09006615 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006616
niisato8ee24222018-06-25 19:05:48 +09006617 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6618 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006619 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006620
niisato8ee24222018-06-25 19:05:48 +09006621 new_cert->cert = cert;
6622 new_cert->key = key;
6623 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006624
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006625 /* Update head is the list was null, else add to the end */
6626 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006627 {
niisato8ee24222018-06-25 19:05:48 +09006628 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006629 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006630 else
6631 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006632 mbedtls_ssl_key_cert *cur = *head;
6633 while( cur->next != NULL )
6634 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006635 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006636 }
6637
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006638 return( 0 );
6639}
6640
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006641int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006642 mbedtls_x509_crt *own_cert,
6643 mbedtls_pk_context *pk_key )
6644{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006645 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006646}
6647
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006648void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006649 mbedtls_x509_crt *ca_chain,
6650 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006651{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006652 conf->ca_chain = ca_chain;
6653 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006656
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006657#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6658int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6659 mbedtls_x509_crt *own_cert,
6660 mbedtls_pk_context *pk_key )
6661{
6662 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6663 own_cert, pk_key ) );
6664}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006665
6666void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6667 mbedtls_x509_crt *ca_chain,
6668 mbedtls_x509_crl *ca_crl )
6669{
6670 ssl->handshake->sni_ca_chain = ca_chain;
6671 ssl->handshake->sni_ca_crl = ca_crl;
6672}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006673
6674void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6675 int authmode )
6676{
6677 ssl->handshake->sni_authmode = authmode;
6678}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006679#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6680
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006681#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006682/*
6683 * Set EC J-PAKE password for current handshake
6684 */
6685int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6686 const unsigned char *pw,
6687 size_t pw_len )
6688{
6689 mbedtls_ecjpake_role role;
6690
Janos Follath8eb64132016-06-03 15:40:57 +01006691 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6693
6694 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6695 role = MBEDTLS_ECJPAKE_SERVER;
6696 else
6697 role = MBEDTLS_ECJPAKE_CLIENT;
6698
6699 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6700 role,
6701 MBEDTLS_MD_SHA256,
6702 MBEDTLS_ECP_DP_SECP256R1,
6703 pw, pw_len ) );
6704}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006705#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006708int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006709 const unsigned char *psk, size_t psk_len,
6710 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006711{
Paul Bakker6db455e2013-09-18 17:29:31 +02006712 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006717
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006718 /* Identity len will be encoded on two bytes */
6719 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006720 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006721 {
6722 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6723 }
6724
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006725 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006726 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006727 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006728
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006729 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006730 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006731 conf->psk_len = 0;
6732 }
6733 if( conf->psk_identity != NULL )
6734 {
6735 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006736 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006737 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006738 }
6739
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006740 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6741 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006742 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006743 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006744 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006745 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006746 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006747 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006748 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006749
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006750 conf->psk_len = psk_len;
6751 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006752
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006753 memcpy( conf->psk, psk, conf->psk_len );
6754 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006755
6756 return( 0 );
6757}
6758
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006759int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6760 const unsigned char *psk, size_t psk_len )
6761{
6762 if( psk == NULL || ssl->handshake == NULL )
6763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6764
6765 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6767
6768 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006769 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006770 mbedtls_platform_zeroize( ssl->handshake->psk,
6771 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006772 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006773 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006774 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006775
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006776 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006777 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006778
6779 ssl->handshake->psk_len = psk_len;
6780 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6781
6782 return( 0 );
6783}
6784
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006785void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006787 size_t),
6788 void *p_psk )
6789{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006790 conf->f_psk = f_psk;
6791 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006794
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006795#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006796
6797#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006798int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006799{
6800 int ret;
6801
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006802 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6803 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6804 {
6805 mbedtls_mpi_free( &conf->dhm_P );
6806 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006809
6810 return( 0 );
6811}
Hanno Becker470a8c42017-10-04 15:28:46 +01006812#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006813
Hanno Beckera90658f2017-10-04 15:29:08 +01006814int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6815 const unsigned char *dhm_P, size_t P_len,
6816 const unsigned char *dhm_G, size_t G_len )
6817{
6818 int ret;
6819
6820 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6821 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6822 {
6823 mbedtls_mpi_free( &conf->dhm_P );
6824 mbedtls_mpi_free( &conf->dhm_G );
6825 return( ret );
6826 }
6827
6828 return( 0 );
6829}
Paul Bakker5121ce52009-01-03 21:22:43 +00006830
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006831int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006832{
6833 int ret;
6834
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006835 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6836 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6837 {
6838 mbedtls_mpi_free( &conf->dhm_P );
6839 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006840 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006841 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006842
6843 return( 0 );
6844}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006845#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006846
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006847#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6848/*
6849 * Set the minimum length for Diffie-Hellman parameters
6850 */
6851void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6852 unsigned int bitlen )
6853{
6854 conf->dhm_min_bitlen = bitlen;
6855}
6856#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6857
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006858#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006859/*
6860 * Set allowed/preferred hashes for handshake signatures
6861 */
6862void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6863 const int *hashes )
6864{
6865 conf->sig_hashes = hashes;
6866}
Hanno Becker947194e2017-04-07 13:25:49 +01006867#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006868
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006869#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006870/*
6871 * Set the allowed elliptic curves
6872 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006873void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006874 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006875{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006876 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006877}
Hanno Becker947194e2017-04-07 13:25:49 +01006878#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006879
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006880#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006882{
Hanno Becker947194e2017-04-07 13:25:49 +01006883 /* Initialize to suppress unnecessary compiler warning */
6884 size_t hostname_len = 0;
6885
6886 /* Check if new hostname is valid before
6887 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006888 if( hostname != NULL )
6889 {
6890 hostname_len = strlen( hostname );
6891
6892 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6894 }
6895
6896 /* Now it's clear that we will overwrite the old hostname,
6897 * so we can free it safely */
6898
6899 if( ssl->hostname != NULL )
6900 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006901 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006902 mbedtls_free( ssl->hostname );
6903 }
6904
6905 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006906
Paul Bakker5121ce52009-01-03 21:22:43 +00006907 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006908 {
6909 ssl->hostname = NULL;
6910 }
6911 else
6912 {
6913 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006914 if( ssl->hostname == NULL )
6915 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006916
Hanno Becker947194e2017-04-07 13:25:49 +01006917 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006918
Hanno Becker947194e2017-04-07 13:25:49 +01006919 ssl->hostname[hostname_len] = '\0';
6920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006921
6922 return( 0 );
6923}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006924#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006925
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006927void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006929 const unsigned char *, size_t),
6930 void *p_sni )
6931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006932 conf->f_sni = f_sni;
6933 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006938int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006939{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006940 size_t cur_len, tot_len;
6941 const char **p;
6942
6943 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006944 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6945 * MUST NOT be truncated."
6946 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006947 */
6948 tot_len = 0;
6949 for( p = protos; *p != NULL; p++ )
6950 {
6951 cur_len = strlen( *p );
6952 tot_len += cur_len;
6953
6954 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006956 }
6957
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006958 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006959
6960 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006961}
6962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006964{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006965 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006968
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006969void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006971 conf->max_major_ver = major;
6972 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006973}
6974
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006975void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006976{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006977 conf->min_major_ver = major;
6978 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006979}
6980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006982void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006983{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006984 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006985}
6986#endif
6987
Janos Follath088ce432017-04-10 12:42:31 +01006988#if defined(MBEDTLS_SSL_SRV_C)
6989void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6990 char cert_req_ca_list )
6991{
6992 conf->cert_req_ca_list = cert_req_ca_list;
6993}
6994#endif
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006997void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006998{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006999 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007000}
7001#endif
7002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007004void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007005{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007006 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007007}
7008#endif
7009
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007010#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007011void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007012{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007013 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007014}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007015#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007018int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007021 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007024 }
7025
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007026 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007027
7028 return( 0 );
7029}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007033void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007034{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007035 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007040void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007041{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007042 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007043}
7044#endif
7045
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007046void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007048 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007049}
7050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007052void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007054 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007055}
7056
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007057void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007059 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007060}
7061
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007062void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007063 const unsigned char period[8] )
7064{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007065 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007070#if defined(MBEDTLS_SSL_CLI_C)
7071void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007072{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007073 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007074}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007075#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007076
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007077#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007078void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7079 mbedtls_ssl_ticket_write_t *f_ticket_write,
7080 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7081 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007082{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007083 conf->f_ticket_write = f_ticket_write;
7084 conf->f_ticket_parse = f_ticket_parse;
7085 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007086}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007089
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007090#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7091void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7092 mbedtls_ssl_export_keys_t *f_export_keys,
7093 void *p_export_keys )
7094{
7095 conf->f_export_keys = f_export_keys;
7096 conf->p_export_keys = p_export_keys;
7097}
7098#endif
7099
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007100#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007101void mbedtls_ssl_conf_async_private_cb(
7102 mbedtls_ssl_config *conf,
7103 mbedtls_ssl_async_sign_t *f_async_sign,
7104 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7105 mbedtls_ssl_async_resume_t *f_async_resume,
7106 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007107 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007108{
7109 conf->f_async_sign_start = f_async_sign;
7110 conf->f_async_decrypt_start = f_async_decrypt;
7111 conf->f_async_resume = f_async_resume;
7112 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007113 conf->p_async_config_data = async_config_data;
7114}
7115
Gilles Peskine8f97af72018-04-26 11:46:10 +02007116void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7117{
7118 return( conf->p_async_config_data );
7119}
7120
Gilles Peskine1febfef2018-04-30 11:54:39 +02007121void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007122{
7123 if( ssl->handshake == NULL )
7124 return( NULL );
7125 else
7126 return( ssl->handshake->user_async_ctx );
7127}
7128
Gilles Peskine1febfef2018-04-30 11:54:39 +02007129void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007130 void *ctx )
7131{
7132 if( ssl->handshake != NULL )
7133 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007134}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007135#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007136
Paul Bakker5121ce52009-01-03 21:22:43 +00007137/*
7138 * SSL get accessors
7139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007141{
7142 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7143}
7144
Hanno Becker8b170a02017-10-10 11:51:19 +01007145int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7146{
7147 /*
7148 * Case A: We're currently holding back
7149 * a message for further processing.
7150 */
7151
7152 if( ssl->keep_current_message == 1 )
7153 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007155 return( 1 );
7156 }
7157
7158 /*
7159 * Case B: Further records are pending in the current datagram.
7160 */
7161
7162#if defined(MBEDTLS_SSL_PROTO_DTLS)
7163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7164 ssl->in_left > ssl->next_record_offset )
7165 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007167 return( 1 );
7168 }
7169#endif /* MBEDTLS_SSL_PROTO_DTLS */
7170
7171 /*
7172 * Case C: A handshake message is being processed.
7173 */
7174
Hanno Becker8b170a02017-10-10 11:51:19 +01007175 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7176 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007178 return( 1 );
7179 }
7180
7181 /*
7182 * Case D: An application data message is being processed
7183 */
7184 if( ssl->in_offt != NULL )
7185 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007187 return( 1 );
7188 }
7189
7190 /*
7191 * In all other cases, the rest of the message can be dropped.
7192 * As in ssl_read_record_layer, this needs to be adapted if
7193 * we implement support for multiple alerts in single records.
7194 */
7195
7196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7197 return( 0 );
7198}
7199
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007200uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007201{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007202 if( ssl->session != NULL )
7203 return( ssl->session->verify_result );
7204
7205 if( ssl->session_negotiate != NULL )
7206 return( ssl->session_negotiate->verify_result );
7207
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007208 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007209}
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007212{
Paul Bakker926c8e42013-03-06 10:23:34 +01007213 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007214 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007217}
7218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007223 {
7224 switch( ssl->minor_ver )
7225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007227 return( "DTLSv1.0" );
7228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007230 return( "DTLSv1.2" );
7231
7232 default:
7233 return( "unknown (DTLS)" );
7234 }
7235 }
7236#endif
7237
Paul Bakker43ca69c2011-01-15 17:35:19 +00007238 switch( ssl->minor_ver )
7239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007241 return( "SSLv3.0" );
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007244 return( "TLSv1.0" );
7245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007247 return( "TLSv1.1" );
7248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007250 return( "TLSv1.2" );
7251
Paul Bakker43ca69c2011-01-15 17:35:19 +00007252 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007253 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007254 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007255}
7256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007258{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007259 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007261 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007262
Hanno Becker78640902018-08-13 16:35:15 +01007263 if( transform == NULL )
7264 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_ZLIB_SUPPORT)
7267 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7268 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007269#endif
7270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 case MBEDTLS_MODE_GCM:
7274 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007275 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007277 transform_expansion = transform->minlen;
7278 break;
7279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007281
7282 block_size = mbedtls_cipher_get_block_size(
7283 &transform->cipher_ctx_enc );
7284
7285#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7286 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7287 {
7288 /* Expansion due to addition of
7289 * - MAC
7290 * - CBC padding (theoretically up to 256 bytes, but
7291 * we never use more than block_size)
7292 * - explicit IV
7293 */
7294 transform_expansion = transform->maclen + 2 * block_size;
7295 }
7296 else
7297#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7298 {
7299 /* No explicit IV prior to TLS 1.1. */
7300 transform_expansion = transform->maclen + block_size;
7301 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007302 break;
7303
7304 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007307 }
7308
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007309 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007310}
7311
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007312#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7313size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7314{
7315 size_t max_len;
7316
7317 /*
7318 * Assume mfl_code is correct since it was checked when set
7319 */
Angus Grattond8213d02016-05-25 20:56:48 +10007320 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007321
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007322 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007323 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007324 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007325 {
Angus Grattond8213d02016-05-25 20:56:48 +10007326 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007327 }
7328
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007329 /* During a handshake, use the value being negotiated */
7330 if( ssl->session_negotiate != NULL &&
7331 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7332 {
7333 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7334 }
7335
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007336 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007337}
7338#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7339
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007340#if defined(MBEDTLS_SSL_PROTO_DTLS)
7341static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7342{
7343 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7344 return( ssl->mtu );
7345
7346 if( ssl->mtu == 0 )
7347 return( ssl->handshake->mtu );
7348
7349 return( ssl->mtu < ssl->handshake->mtu ?
7350 ssl->mtu : ssl->handshake->mtu );
7351}
7352#endif /* MBEDTLS_SSL_PROTO_DTLS */
7353
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007354int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7355{
7356 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7357
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007358#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7359 !defined(MBEDTLS_SSL_PROTO_DTLS)
7360 (void) ssl;
7361#endif
7362
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007363#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7364 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7365
7366 if( max_len > mfl )
7367 max_len = mfl;
7368#endif
7369
7370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007371 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007372 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007373 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007374 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7375 const size_t overhead = (size_t) ret;
7376
7377 if( ret < 0 )
7378 return( ret );
7379
7380 if( mtu <= overhead )
7381 {
7382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7383 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7384 }
7385
7386 if( max_len > mtu - overhead )
7387 max_len = mtu - overhead;
7388 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007389#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007390
Hanno Becker0defedb2018-08-10 12:35:02 +01007391#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7392 !defined(MBEDTLS_SSL_PROTO_DTLS)
7393 ((void) ssl);
7394#endif
7395
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007396 return( (int) max_len );
7397}
7398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#if defined(MBEDTLS_X509_CRT_PARSE_C)
7400const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007401{
7402 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007403 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007404
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007405 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_CLI_C)
7410int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007411{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007412 if( ssl == NULL ||
7413 dst == NULL ||
7414 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007415 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007418 }
7419
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007420 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007423
Paul Bakker5121ce52009-01-03 21:22:43 +00007424/*
Paul Bakker1961b702013-01-25 14:49:24 +01007425 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007428{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007430
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007431 if( ssl == NULL || ssl->conf == NULL )
7432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007435 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007437#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007439 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007441#endif
7442
Paul Bakker1961b702013-01-25 14:49:24 +01007443 return( ret );
7444}
7445
7446/*
7447 * Perform the SSL handshake
7448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007450{
7451 int ret = 0;
7452
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007453 if( ssl == NULL || ssl->conf == NULL )
7454 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007461
7462 if( ret != 0 )
7463 break;
7464 }
7465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
7468 return( ret );
7469}
7470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#if defined(MBEDTLS_SSL_RENEGOTIATION)
7472#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007473/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007474 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007477{
7478 int ret;
7479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007481
7482 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7484 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007485
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007486 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007487 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007489 return( ret );
7490 }
7491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007493
7494 return( 0 );
7495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007497
7498/*
7499 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 * - any side: calling mbedtls_ssl_renegotiate(),
7501 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7502 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007503 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007504 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007508{
7509 int ret;
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007512
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007513 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7514 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007515
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007516 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7517 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007521 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007522 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007523 ssl->handshake->out_msg_seq = 1;
7524 else
7525 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007526 }
7527#endif
7528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7530 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007535 return( ret );
7536 }
7537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007539
7540 return( 0 );
7541}
7542
7543/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007544 * Renegotiate current connection on client,
7545 * or request renegotiation on server
7546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007551 if( ssl == NULL || ssl->conf == NULL )
7552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007555 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007562
7563 /* Did we already try/start sending HelloRequest? */
7564 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007566
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007567 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007568 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007572 /*
7573 * On client, either start the renegotiation process or,
7574 * if already in progress, continue the handshake
7575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007580
7581 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007584 return( ret );
7585 }
7586 }
7587 else
7588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007592 return( ret );
7593 }
7594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007596
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007597 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007598}
7599
7600/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007601 * Check record counters and renegotiate if they're above the limit.
7602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007604{
Andres AG2196c7f2016-12-15 17:01:16 +00007605 size_t ep_len = ssl_ep_len( ssl );
7606 int in_ctr_cmp;
7607 int out_ctr_cmp;
7608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7610 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007611 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007612 {
7613 return( 0 );
7614 }
7615
Andres AG2196c7f2016-12-15 17:01:16 +00007616 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7617 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007618 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007619 ssl->conf->renego_period + ep_len, 8 - ep_len );
7620
7621 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007622 {
7623 return( 0 );
7624 }
7625
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007630
7631/*
7632 * Receive application data decrypted from the SSL layer
7633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007635{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007636 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007637 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007638
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007639 if( ssl == NULL || ssl->conf == NULL )
7640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007648 return( ret );
7649
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007650 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007652 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007653 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007654 return( ret );
7655 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007656 }
7657#endif
7658
Hanno Becker4a810fb2017-05-24 16:27:30 +01007659 /*
7660 * Check if renegotiation is necessary and/or handshake is
7661 * in process. If yes, perform/continue, and fall through
7662 * if an unexpected packet is received while the client
7663 * is waiting for the ServerHello.
7664 *
7665 * (There is no equivalent to the last condition on
7666 * the server-side as it is not treated as within
7667 * a handshake while waiting for the ClientHello
7668 * after a renegotiation request.)
7669 */
7670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007672 ret = ssl_check_ctr_renegotiate( ssl );
7673 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7674 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007677 return( ret );
7678 }
7679#endif
7680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007684 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7685 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007688 return( ret );
7689 }
7690 }
7691
Hanno Beckere41158b2017-10-23 13:30:32 +01007692 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007693 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007694 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007695 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007696 if( ssl->f_get_timer != NULL &&
7697 ssl->f_get_timer( ssl->p_timer ) == -1 )
7698 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007699 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007700 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007701
Hanno Becker4a810fb2017-05-24 16:27:30 +01007702 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007703 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007704 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7705 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007706
Hanno Becker4a810fb2017-05-24 16:27:30 +01007707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7708 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007709 }
7710
7711 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007713 {
7714 /*
7715 * OpenSSL sends empty messages to randomize the IV
7716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007720 return( 0 );
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007723 return( ret );
7724 }
7725 }
7726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007730
Hanno Becker4a810fb2017-05-24 16:27:30 +01007731 /*
7732 * - For client-side, expect SERVER_HELLO_REQUEST.
7733 * - For server-side, expect CLIENT_HELLO.
7734 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7735 */
7736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007740 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007743
7744 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007746 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007747 {
7748 continue;
7749 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007750#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007752 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007753#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007754
Hanno Becker4a810fb2017-05-24 16:27:30 +01007755#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007756 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007760
7761 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007764 {
7765 continue;
7766 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007769 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007770#endif /* MBEDTLS_SSL_SRV_C */
7771
Hanno Becker21df7f92017-10-17 11:03:26 +01007772#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007773 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007774 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7775 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7776 ssl->conf->allow_legacy_renegotiation ==
7777 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7778 {
7779 /*
7780 * Accept renegotiation request
7781 */
Paul Bakker48916f92012-09-16 19:57:18 +00007782
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007783 /* DTLS clients need to know renego is server-initiated */
7784#if defined(MBEDTLS_SSL_PROTO_DTLS)
7785 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7786 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7787 {
7788 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7789 }
7790#endif
7791 ret = ssl_start_renegotiation( ssl );
7792 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7793 ret != 0 )
7794 {
7795 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7796 return( ret );
7797 }
7798 }
7799 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007800#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007801 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007802 /*
7803 * Refuse renegotiation
7804 */
7805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808#if defined(MBEDTLS_SSL_PROTO_SSL3)
7809 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007810 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007811 /* SSLv3 does not have a "no_renegotiation" warning, so
7812 we send a fatal alert and abort the connection. */
7813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7814 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7815 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007816 }
7817 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7819#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7820 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7821 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7824 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7825 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007826 {
7827 return( ret );
7828 }
Paul Bakker48916f92012-09-16 19:57:18 +00007829 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007830 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7832 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007836 }
Paul Bakker48916f92012-09-16 19:57:18 +00007837 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007838
Hanno Becker90333da2017-10-10 11:27:13 +01007839 /* At this point, we don't know whether the renegotiation has been
7840 * completed or not. The cases to consider are the following:
7841 * 1) The renegotiation is complete. In this case, no new record
7842 * has been read yet.
7843 * 2) The renegotiation is incomplete because the client received
7844 * an application data record while awaiting the ServerHello.
7845 * 3) The renegotiation is incomplete because the client received
7846 * a non-handshake, non-application data message while awaiting
7847 * the ServerHello.
7848 * In each of these case, looping will be the proper action:
7849 * - For 1), the next iteration will read a new record and check
7850 * if it's application data.
7851 * - For 2), the loop condition isn't satisfied as application data
7852 * is present, hence continue is the same as break
7853 * - For 3), the loop condition is satisfied and read_record
7854 * will re-deliver the message that was held back by the client
7855 * when expecting the ServerHello.
7856 */
7857 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007858 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007859#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007861 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007862 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007863 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007864 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007867 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007869 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007870 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7875 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007878 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007879 }
7880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7884 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007885 }
7886
7887 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007888
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007889 /* We're going to return something now, cancel timer,
7890 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007892 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007893
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007895 /* If we requested renego but received AppData, resend HelloRequest.
7896 * Do it now, after setting in_offt, to avoid taking this branch
7897 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007899 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007901 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007902 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007905 return( ret );
7906 }
7907 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007910 }
7911
7912 n = ( len < ssl->in_msglen )
7913 ? len : ssl->in_msglen;
7914
7915 memcpy( buf, ssl->in_offt, n );
7916 ssl->in_msglen -= n;
7917
7918 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007919 {
7920 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007921 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007922 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007923 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007924 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007925 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007926 /* more data available */
7927 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007931
Paul Bakker23986e52011-04-24 08:57:21 +00007932 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007933}
7934
7935/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007936 * Send application data to be encrypted by the SSL layer, taking care of max
7937 * fragment length and buffer size.
7938 *
7939 * According to RFC 5246 Section 6.2.1:
7940 *
7941 * Zero-length fragments of Application data MAY be sent as they are
7942 * potentially useful as a traffic analysis countermeasure.
7943 *
7944 * Therefore, it is possible that the input message length is 0 and the
7945 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007946 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007947static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007948 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007949{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007950 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7951 const size_t max_len = (size_t) ret;
7952
7953 if( ret < 0 )
7954 {
7955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7956 return( ret );
7957 }
7958
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007959 if( len > max_len )
7960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007965 "maximum fragment length: %d > %d",
7966 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007968 }
7969 else
7970#endif
7971 len = max_len;
7972 }
Paul Bakker887bd502011-06-08 13:10:54 +00007973
Paul Bakker5121ce52009-01-03 21:22:43 +00007974 if( ssl->out_left != 0 )
7975 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007976 /*
7977 * The user has previously tried to send the data and
7978 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7979 * written. In this case, we expect the high-level write function
7980 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007985 return( ret );
7986 }
7987 }
Paul Bakker887bd502011-06-08 13:10:54 +00007988 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007989 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007990 /*
7991 * The user is trying to send a message the first time, so we need to
7992 * copy the data into the internal buffers and setup the data structure
7993 * to keep track of partial writes
7994 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007995 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007997 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007998
Hanno Becker67bc7c32018-08-06 11:33:50 +01007999 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008002 return( ret );
8003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008004 }
8005
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008006 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008007}
8008
8009/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008010 * Write application data, doing 1/n-1 splitting if necessary.
8011 *
8012 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008013 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008014 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008017static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008018 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008019{
8020 int ret;
8021
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008022 if( ssl->conf->cbc_record_splitting ==
8023 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008024 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8026 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8027 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008028 {
8029 return( ssl_write_real( ssl, buf, len ) );
8030 }
8031
8032 if( ssl->split_done == 0 )
8033 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008034 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008035 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008036 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008037 }
8038
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008039 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8040 return( ret );
8041 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008042
8043 return( ret + 1 );
8044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008046
8047/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008048 * Write application data (public-facing wrapper)
8049 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008050int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008051{
8052 int ret;
8053
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008055
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008056 if( ssl == NULL || ssl->conf == NULL )
8057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8058
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008059#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008060 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8061 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008062 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008063 return( ret );
8064 }
8065#endif
8066
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008067 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008068 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008069 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008070 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008072 return( ret );
8073 }
8074 }
8075
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008076#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008077 ret = ssl_write_split( ssl, buf, len );
8078#else
8079 ret = ssl_write_real( ssl, buf, len );
8080#endif
8081
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008083
8084 return( ret );
8085}
8086
8087/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008088 * Notify the peer that the connection is being closed
8089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008091{
8092 int ret;
8093
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008094 if( ssl == NULL || ssl->conf == NULL )
8095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008098
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008099 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8105 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8106 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008109 return( ret );
8110 }
8111 }
8112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008114
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008115 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008116}
8117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008119{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008120 if( transform == NULL )
8121 return;
8122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008124 deflateEnd( &transform->ctx_deflate );
8125 inflateEnd( &transform->ctx_inflate );
8126#endif
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8129 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131 mbedtls_md_free( &transform->md_ctx_enc );
8132 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008133
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008134 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008135}
8136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137#if defined(MBEDTLS_X509_CRT_PARSE_C)
8138static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008141
8142 while( cur != NULL )
8143 {
8144 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008146 cur = next;
8147 }
8148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008150
Gilles Peskine9b562d52018-04-25 20:32:43 +02008151void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008152{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008153 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8154
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008155 if( handshake == NULL )
8156 return;
8157
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008158#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8159 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8160 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008161 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008162 handshake->async_in_progress = 0;
8163 }
8164#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8165
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008166#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8167 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8168 mbedtls_md5_free( &handshake->fin_md5 );
8169 mbedtls_sha1_free( &handshake->fin_sha1 );
8170#endif
8171#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8172#if defined(MBEDTLS_SHA256_C)
8173 mbedtls_sha256_free( &handshake->fin_sha256 );
8174#endif
8175#if defined(MBEDTLS_SHA512_C)
8176 mbedtls_sha512_free( &handshake->fin_sha512 );
8177#endif
8178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_DHM_C)
8181 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_ECDH_C)
8184 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008185#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008186#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008187 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008188#if defined(MBEDTLS_SSL_CLI_C)
8189 mbedtls_free( handshake->ecjpake_cache );
8190 handshake->ecjpake_cache = NULL;
8191 handshake->ecjpake_cache_len = 0;
8192#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008193#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008194
Janos Follath4ae5c292016-02-10 11:27:43 +00008195#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8196 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008197 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008199#endif
8200
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008201#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8202 if( handshake->psk != NULL )
8203 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008204 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008205 mbedtls_free( handshake->psk );
8206 }
8207#endif
8208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8210 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008211 /*
8212 * Free only the linked list wrapper, not the keys themselves
8213 * since the belong to the SNI callback
8214 */
8215 if( handshake->sni_key_cert != NULL )
8216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008218
8219 while( cur != NULL )
8220 {
8221 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008223 cur = next;
8224 }
8225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228#if defined(MBEDTLS_SSL_PROTO_DTLS)
8229 mbedtls_free( handshake->verify_cookie );
8230 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008231 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008232#endif
8233
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008234 mbedtls_platform_zeroize( handshake,
8235 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008236}
8237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008239{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008240 if( session == NULL )
8241 return;
8242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008244 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246 mbedtls_x509_crt_free( session->peer_cert );
8247 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008248 }
Paul Bakkered27a042013-04-18 22:46:23 +02008249#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008250
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008251#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008253#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008254
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008255 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008256}
8257
Paul Bakker5121ce52009-01-03 21:22:43 +00008258/*
8259 * Free an SSL context
8260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008262{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008263 if( ssl == NULL )
8264 return;
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008267
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008268 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008269 {
Angus Grattond8213d02016-05-25 20:56:48 +10008270 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008272 }
8273
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008274 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008275 {
Angus Grattond8213d02016-05-25 20:56:48 +10008276 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008278 }
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008281 if( ssl->compress_buf != NULL )
8282 {
Angus Grattond8213d02016-05-25 20:56:48 +10008283 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008285 }
8286#endif
8287
Paul Bakker48916f92012-09-16 19:57:18 +00008288 if( ssl->transform )
8289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 mbedtls_ssl_transform_free( ssl->transform );
8291 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008292 }
8293
8294 if( ssl->handshake )
8295 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008296 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8298 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300 mbedtls_free( ssl->handshake );
8301 mbedtls_free( ssl->transform_negotiate );
8302 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008303 }
8304
Paul Bakkerc0463502013-02-14 11:19:38 +01008305 if( ssl->session )
8306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 mbedtls_ssl_session_free( ssl->session );
8308 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008309 }
8310
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008311#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008312 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008313 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008314 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008316 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8320 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8323 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008324 }
8325#endif
8326
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008327#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008329#endif
8330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008332
Paul Bakker86f04f42013-02-14 11:20:09 +01008333 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008334 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008335}
8336
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008337/*
8338 * Initialze mbedtls_ssl_config
8339 */
8340void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8341{
8342 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8343}
8344
Simon Butcherc97b6972015-12-27 23:48:17 +00008345#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008346static int ssl_preset_default_hashes[] = {
8347#if defined(MBEDTLS_SHA512_C)
8348 MBEDTLS_MD_SHA512,
8349 MBEDTLS_MD_SHA384,
8350#endif
8351#if defined(MBEDTLS_SHA256_C)
8352 MBEDTLS_MD_SHA256,
8353 MBEDTLS_MD_SHA224,
8354#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008355#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008356 MBEDTLS_MD_SHA1,
8357#endif
8358 MBEDTLS_MD_NONE
8359};
Simon Butcherc97b6972015-12-27 23:48:17 +00008360#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008361
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008362static int ssl_preset_suiteb_ciphersuites[] = {
8363 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8364 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8365 0
8366};
8367
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008368#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008369static int ssl_preset_suiteb_hashes[] = {
8370 MBEDTLS_MD_SHA256,
8371 MBEDTLS_MD_SHA384,
8372 MBEDTLS_MD_NONE
8373};
8374#endif
8375
8376#if defined(MBEDTLS_ECP_C)
8377static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8378 MBEDTLS_ECP_DP_SECP256R1,
8379 MBEDTLS_ECP_DP_SECP384R1,
8380 MBEDTLS_ECP_DP_NONE
8381};
8382#endif
8383
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008384/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008385 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008386 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008387int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008388 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008389{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008390#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008391 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008392#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008393
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008394 /* Use the functions here so that they are covered in tests,
8395 * but otherwise access member directly for efficiency */
8396 mbedtls_ssl_conf_endpoint( conf, endpoint );
8397 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008398
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008399 /*
8400 * Things that are common to all presets
8401 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008402#if defined(MBEDTLS_SSL_CLI_C)
8403 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8404 {
8405 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8406#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8407 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8408#endif
8409 }
8410#endif
8411
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008412#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008413 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008414#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008415
8416#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8417 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8418#endif
8419
8420#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8421 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8422#endif
8423
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008424#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8425 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8426#endif
8427
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008428#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008429 conf->f_cookie_write = ssl_cookie_write_dummy;
8430 conf->f_cookie_check = ssl_cookie_check_dummy;
8431#endif
8432
8433#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8434 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8435#endif
8436
Janos Follath088ce432017-04-10 12:42:31 +01008437#if defined(MBEDTLS_SSL_SRV_C)
8438 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8439#endif
8440
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008441#if defined(MBEDTLS_SSL_PROTO_DTLS)
8442 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8443 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8444#endif
8445
8446#if defined(MBEDTLS_SSL_RENEGOTIATION)
8447 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008448 memset( conf->renego_period, 0x00, 2 );
8449 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008450#endif
8451
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008452#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8453 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8454 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008455 const unsigned char dhm_p[] =
8456 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8457 const unsigned char dhm_g[] =
8458 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8459
Hanno Beckera90658f2017-10-04 15:29:08 +01008460 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8461 dhm_p, sizeof( dhm_p ),
8462 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008463 {
8464 return( ret );
8465 }
8466 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008467#endif
8468
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008469 /*
8470 * Preset-specific defaults
8471 */
8472 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008473 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008474 /*
8475 * NSA Suite B
8476 */
8477 case MBEDTLS_SSL_PRESET_SUITEB:
8478 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8479 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8480 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8481 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8482
8483 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8484 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8485 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8486 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8487 ssl_preset_suiteb_ciphersuites;
8488
8489#if defined(MBEDTLS_X509_CRT_PARSE_C)
8490 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008491#endif
8492
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008493#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008494 conf->sig_hashes = ssl_preset_suiteb_hashes;
8495#endif
8496
8497#if defined(MBEDTLS_ECP_C)
8498 conf->curve_list = ssl_preset_suiteb_curves;
8499#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008500 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008501
8502 /*
8503 * Default
8504 */
8505 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008506 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8507 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8508 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8509 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8510 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8511 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8512 MBEDTLS_SSL_MIN_MINOR_VERSION :
8513 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008514 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8515 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8516
8517#if defined(MBEDTLS_SSL_PROTO_DTLS)
8518 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8519 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8520#endif
8521
8522 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8523 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8524 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8525 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8526 mbedtls_ssl_list_ciphersuites();
8527
8528#if defined(MBEDTLS_X509_CRT_PARSE_C)
8529 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8530#endif
8531
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008532#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008533 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008534#endif
8535
8536#if defined(MBEDTLS_ECP_C)
8537 conf->curve_list = mbedtls_ecp_grp_id_list();
8538#endif
8539
8540#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8541 conf->dhm_min_bitlen = 1024;
8542#endif
8543 }
8544
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008545 return( 0 );
8546}
8547
8548/*
8549 * Free mbedtls_ssl_config
8550 */
8551void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8552{
8553#if defined(MBEDTLS_DHM_C)
8554 mbedtls_mpi_free( &conf->dhm_P );
8555 mbedtls_mpi_free( &conf->dhm_G );
8556#endif
8557
8558#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8559 if( conf->psk != NULL )
8560 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008561 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008562 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008563 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008564 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008565 }
8566
8567 if( conf->psk_identity != NULL )
8568 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008569 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008570 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008571 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008572 conf->psk_identity_len = 0;
8573 }
8574#endif
8575
8576#if defined(MBEDTLS_X509_CRT_PARSE_C)
8577 ssl_key_cert_free( conf->key_cert );
8578#endif
8579
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008580 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008581}
8582
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008583#if defined(MBEDTLS_PK_C) && \
8584 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008585/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#if defined(MBEDTLS_RSA_C)
8591 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8592 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594#if defined(MBEDTLS_ECDSA_C)
8595 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8596 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008599}
8600
Hanno Becker7e5437a2017-04-28 17:15:26 +01008601unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8602{
8603 switch( type ) {
8604 case MBEDTLS_PK_RSA:
8605 return( MBEDTLS_SSL_SIG_RSA );
8606 case MBEDTLS_PK_ECDSA:
8607 case MBEDTLS_PK_ECKEY:
8608 return( MBEDTLS_SSL_SIG_ECDSA );
8609 default:
8610 return( MBEDTLS_SSL_SIG_ANON );
8611 }
8612}
8613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008615{
8616 switch( sig )
8617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_RSA_C)
8619 case MBEDTLS_SSL_SIG_RSA:
8620 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622#if defined(MBEDTLS_ECDSA_C)
8623 case MBEDTLS_SSL_SIG_ECDSA:
8624 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008625#endif
8626 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008628 }
8629}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008630#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008631
Hanno Becker7e5437a2017-04-28 17:15:26 +01008632#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8633 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8634
8635/* Find an entry in a signature-hash set matching a given hash algorithm. */
8636mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8637 mbedtls_pk_type_t sig_alg )
8638{
8639 switch( sig_alg )
8640 {
8641 case MBEDTLS_PK_RSA:
8642 return( set->rsa );
8643 case MBEDTLS_PK_ECDSA:
8644 return( set->ecdsa );
8645 default:
8646 return( MBEDTLS_MD_NONE );
8647 }
8648}
8649
8650/* Add a signature-hash-pair to a signature-hash set */
8651void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8652 mbedtls_pk_type_t sig_alg,
8653 mbedtls_md_type_t md_alg )
8654{
8655 switch( sig_alg )
8656 {
8657 case MBEDTLS_PK_RSA:
8658 if( set->rsa == MBEDTLS_MD_NONE )
8659 set->rsa = md_alg;
8660 break;
8661
8662 case MBEDTLS_PK_ECDSA:
8663 if( set->ecdsa == MBEDTLS_MD_NONE )
8664 set->ecdsa = md_alg;
8665 break;
8666
8667 default:
8668 break;
8669 }
8670}
8671
8672/* Allow exactly one hash algorithm for each signature. */
8673void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8674 mbedtls_md_type_t md_alg )
8675{
8676 set->rsa = md_alg;
8677 set->ecdsa = md_alg;
8678}
8679
8680#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8681 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8682
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008683/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008684 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008687{
8688 switch( hash )
8689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690#if defined(MBEDTLS_MD5_C)
8691 case MBEDTLS_SSL_HASH_MD5:
8692 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694#if defined(MBEDTLS_SHA1_C)
8695 case MBEDTLS_SSL_HASH_SHA1:
8696 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698#if defined(MBEDTLS_SHA256_C)
8699 case MBEDTLS_SSL_HASH_SHA224:
8700 return( MBEDTLS_MD_SHA224 );
8701 case MBEDTLS_SSL_HASH_SHA256:
8702 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#if defined(MBEDTLS_SHA512_C)
8705 case MBEDTLS_SSL_HASH_SHA384:
8706 return( MBEDTLS_MD_SHA384 );
8707 case MBEDTLS_SSL_HASH_SHA512:
8708 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008709#endif
8710 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008712 }
8713}
8714
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008715/*
8716 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8717 */
8718unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8719{
8720 switch( md )
8721 {
8722#if defined(MBEDTLS_MD5_C)
8723 case MBEDTLS_MD_MD5:
8724 return( MBEDTLS_SSL_HASH_MD5 );
8725#endif
8726#if defined(MBEDTLS_SHA1_C)
8727 case MBEDTLS_MD_SHA1:
8728 return( MBEDTLS_SSL_HASH_SHA1 );
8729#endif
8730#if defined(MBEDTLS_SHA256_C)
8731 case MBEDTLS_MD_SHA224:
8732 return( MBEDTLS_SSL_HASH_SHA224 );
8733 case MBEDTLS_MD_SHA256:
8734 return( MBEDTLS_SSL_HASH_SHA256 );
8735#endif
8736#if defined(MBEDTLS_SHA512_C)
8737 case MBEDTLS_MD_SHA384:
8738 return( MBEDTLS_SSL_HASH_SHA384 );
8739 case MBEDTLS_MD_SHA512:
8740 return( MBEDTLS_SSL_HASH_SHA512 );
8741#endif
8742 default:
8743 return( MBEDTLS_SSL_HASH_NONE );
8744 }
8745}
8746
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008747#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008748/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008749 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008750 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008751 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008752int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008755
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008756 if( ssl->conf->curve_list == NULL )
8757 return( -1 );
8758
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008759 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008760 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008761 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008762
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008763 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008764}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008765#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008766
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008767#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008768/*
8769 * Check if a hash proposed by the peer is in our list.
8770 * Return 0 if we're willing to use it, -1 otherwise.
8771 */
8772int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8773 mbedtls_md_type_t md )
8774{
8775 const int *cur;
8776
8777 if( ssl->conf->sig_hashes == NULL )
8778 return( -1 );
8779
8780 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8781 if( *cur == (int) md )
8782 return( 0 );
8783
8784 return( -1 );
8785}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008786#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788#if defined(MBEDTLS_X509_CRT_PARSE_C)
8789int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8790 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008791 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008792 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008793{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008794 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008796 int usage = 0;
8797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008799 const char *ext_oid;
8800 size_t ext_len;
8801#endif
8802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8804 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008805 ((void) cert);
8806 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008807 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008808#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8811 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008812 {
8813 /* Server part of the key exchange */
8814 switch( ciphersuite->key_exchange )
8815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816 case MBEDTLS_KEY_EXCHANGE_RSA:
8817 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008818 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008819 break;
8820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8822 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8823 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8824 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008825 break;
8826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8828 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008829 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008830 break;
8831
8832 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833 case MBEDTLS_KEY_EXCHANGE_NONE:
8834 case MBEDTLS_KEY_EXCHANGE_PSK:
8835 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8836 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008837 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008838 usage = 0;
8839 }
8840 }
8841 else
8842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8844 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008845 }
8846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008848 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008849 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008850 ret = -1;
8851 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008852#else
8853 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8857 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8860 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008861 }
8862 else
8863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8865 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008866 }
8867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008869 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008870 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008871 ret = -1;
8872 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008874
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008875 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008878
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008879/*
8880 * Convert version numbers to/from wire format
8881 * and, for DTLS, to/from TLS equivalent.
8882 *
8883 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008884 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008885 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8886 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008889 unsigned char ver[2] )
8890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891#if defined(MBEDTLS_SSL_PROTO_DTLS)
8892 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008895 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8896
8897 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8898 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8899 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008900 else
8901#else
8902 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008903#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008904 {
8905 ver[0] = (unsigned char) major;
8906 ver[1] = (unsigned char) minor;
8907 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008908}
8909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008910void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008911 const unsigned char ver[2] )
8912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913#if defined(MBEDTLS_SSL_PROTO_DTLS)
8914 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008915 {
8916 *major = 255 - ver[0] + 2;
8917 *minor = 255 - ver[1] + 1;
8918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008920 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8921 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008922 else
8923#else
8924 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008925#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008926 {
8927 *major = ver[0];
8928 *minor = ver[1];
8929 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008930}
8931
Simon Butcher99000142016-10-13 17:21:01 +01008932int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8933{
8934#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8935 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8936 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8937
8938 switch( md )
8939 {
8940#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8941#if defined(MBEDTLS_MD5_C)
8942 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008943 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008944#endif
8945#if defined(MBEDTLS_SHA1_C)
8946 case MBEDTLS_SSL_HASH_SHA1:
8947 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8948 break;
8949#endif
8950#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8951#if defined(MBEDTLS_SHA512_C)
8952 case MBEDTLS_SSL_HASH_SHA384:
8953 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8954 break;
8955#endif
8956#if defined(MBEDTLS_SHA256_C)
8957 case MBEDTLS_SSL_HASH_SHA256:
8958 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8959 break;
8960#endif
8961 default:
8962 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8963 }
8964
8965 return 0;
8966#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8967 (void) ssl;
8968 (void) md;
8969
8970 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8971#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8972}
8973
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008974#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8975 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8976int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8977 unsigned char *output,
8978 unsigned char *data, size_t data_len )
8979{
8980 int ret = 0;
8981 mbedtls_md5_context mbedtls_md5;
8982 mbedtls_sha1_context mbedtls_sha1;
8983
8984 mbedtls_md5_init( &mbedtls_md5 );
8985 mbedtls_sha1_init( &mbedtls_sha1 );
8986
8987 /*
8988 * digitally-signed struct {
8989 * opaque md5_hash[16];
8990 * opaque sha_hash[20];
8991 * };
8992 *
8993 * md5_hash
8994 * MD5(ClientHello.random + ServerHello.random
8995 * + ServerParams);
8996 * sha_hash
8997 * SHA(ClientHello.random + ServerHello.random
8998 * + ServerParams);
8999 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009000 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009001 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009003 goto exit;
9004 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009005 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009006 ssl->handshake->randbytes, 64 ) ) != 0 )
9007 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009009 goto exit;
9010 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009011 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009012 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009014 goto exit;
9015 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009016 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009017 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009019 goto exit;
9020 }
9021
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009022 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009023 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009025 goto exit;
9026 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009027 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009028 ssl->handshake->randbytes, 64 ) ) != 0 )
9029 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009031 goto exit;
9032 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009033 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009034 data_len ) ) != 0 )
9035 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009037 goto exit;
9038 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009039 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009040 output + 16 ) ) != 0 )
9041 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009043 goto exit;
9044 }
9045
9046exit:
9047 mbedtls_md5_free( &mbedtls_md5 );
9048 mbedtls_sha1_free( &mbedtls_sha1 );
9049
9050 if( ret != 0 )
9051 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9052 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9053
9054 return( ret );
9055
9056}
9057#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9058 MBEDTLS_SSL_PROTO_TLS1_1 */
9059
9060#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9061 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9062int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009063 unsigned char *hash, size_t *hashlen,
9064 unsigned char *data, size_t data_len,
9065 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009066{
9067 int ret = 0;
9068 mbedtls_md_context_t ctx;
9069 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009070 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009071
9072 mbedtls_md_init( &ctx );
9073
9074 /*
9075 * digitally-signed struct {
9076 * opaque client_random[32];
9077 * opaque server_random[32];
9078 * ServerDHParams params;
9079 * };
9080 */
9081 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9082 {
9083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9084 goto exit;
9085 }
9086 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9087 {
9088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9089 goto exit;
9090 }
9091 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9092 {
9093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9094 goto exit;
9095 }
9096 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9097 {
9098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9099 goto exit;
9100 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009101 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009102 {
9103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9104 goto exit;
9105 }
9106
9107exit:
9108 mbedtls_md_free( &ctx );
9109
9110 if( ret != 0 )
9111 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9112 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9113
9114 return( ret );
9115}
9116#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9117 MBEDTLS_SSL_PROTO_TLS1_2 */
9118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#endif /* MBEDTLS_SSL_TLS_C */