blob: 41803b609446353ecf77adaeee42fb8105132e75 [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 );
Hanno Becker12555c62018-08-16 12:47:53 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010059
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010060/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020064 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010066#else
67 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068#endif
69 return( 0 );
70}
71
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072/*
73 * Start a timer.
74 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020078 if( ssl->f_set_timer == NULL )
79 return;
80
81 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
82 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083}
84
85/*
86 * Return -1 is timer is expired, 0 if it isn't.
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020090 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020091 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092
93 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 {
95 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020097 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
99 return( 0 );
100}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100102static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
103 mbedtls_ssl_transform *transform );
104static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
105 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100106
107#define SSL_DONT_FORCE_FLUSH 0
108#define SSL_FORCE_FLUSH 1
109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100111
Hanno Beckera67dee22018-08-22 10:05:20 +0100112static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100113static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100114{
Hanno Becker11682cc2018-08-22 14:41:02 +0100115 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
117 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100118 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100119
120 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
121}
122
Hanno Becker67bc7c32018-08-06 11:33:50 +0100123static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
124{
Hanno Becker11682cc2018-08-22 14:41:02 +0100125 size_t const bytes_written = ssl->out_left;
126 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100127
128 /* Double-check that the write-index hasn't gone
129 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100130 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100131 {
132 /* Should never happen... */
133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
134 }
135
136 return( (int) ( mtu - bytes_written ) );
137}
138
139static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
140{
141 int ret;
142 size_t remaining, expansion;
143 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
144
145#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
146 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
147
148 if( max_len > mfl )
149 max_len = mfl;
150#endif
151
152 ret = ssl_get_remaining_space_in_datagram( ssl );
153 if( ret < 0 )
154 return( ret );
155 remaining = (size_t) ret;
156
157 ret = mbedtls_ssl_get_record_expansion( ssl );
158 if( ret < 0 )
159 return( ret );
160 expansion = (size_t) ret;
161
162 if( remaining <= expansion )
163 return( 0 );
164
165 remaining -= expansion;
166 if( remaining >= max_len )
167 remaining = max_len;
168
169 return( (int) remaining );
170}
171
Hanno Becker0271f962018-08-16 13:23:47 +0100172static void ssl_buffering_free( mbedtls_ssl_context *ssl );
173
Hanno Beckere605b192018-08-21 15:59:07 +0100174static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
175 uint8_t slot );
176
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200177/*
178 * Double the retransmit timeout value, within the allowed range,
179 * returning -1 if the maximum value has already been reached.
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200182{
183 uint32_t new_timeout;
184
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200185 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200186 return( -1 );
187
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200188 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
189 * in the following way: after the initial transmission and a first
190 * retransmission, back off to a temporary estimated MTU of 508 bytes.
191 * This value is guaranteed to be deliverable (if not guaranteed to be
192 * delivered) of any compliant IPv4 (and IPv6) network, and should work
193 * on most non-IP stacks too. */
194 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
195 ssl->handshake->mtu = 508;
196
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200197 new_timeout = 2 * ssl->handshake->retransmit_timeout;
198
199 /* Avoid arithmetic overflow and range overflow */
200 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200201 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200203 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200204 }
205
206 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200208 ssl->handshake->retransmit_timeout ) );
209
210 return( 0 );
211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200214{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200215 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200217 ssl->handshake->retransmit_timeout ) );
218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222/*
223 * Convert max_fragment_length codes to length.
224 * RFC 6066 says:
225 * enum{
226 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
227 * } MaxFragmentLength;
228 * and we add 0 -> extension unused
229 */
Angus Grattond8213d02016-05-25 20:56:48 +1000230static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200231{
Angus Grattond8213d02016-05-25 20:56:48 +1000232 switch( mfl )
233 {
234 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
235 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
236 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
237 return 512;
238 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
239 return 1024;
240 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
241 return 2048;
242 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
243 return 4096;
244 default:
245 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
246 }
247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200249
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200250#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_ssl_session_free( dst );
254 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200257 if( src->peer_cert != NULL )
258 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200259 int ret;
260
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200261 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200262 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200263 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200268 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200271 dst->peer_cert = NULL;
272 return( ret );
273 }
274 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200276
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200277#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278 if( src->ticket != NULL )
279 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200280 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200281 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200282 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
284 memcpy( dst->ticket, src->ticket, src->ticket_len );
285 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200286#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
288 return( 0 );
289}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200290#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
293int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200294 const unsigned char *key_enc, const unsigned char *key_dec,
295 size_t keylen,
296 const unsigned char *iv_enc, const unsigned char *iv_dec,
297 size_t ivlen,
298 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200299 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
301int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
302int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
303int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
304int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
305#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000306
Paul Bakker5121ce52009-01-03 21:22:43 +0000307/*
308 * Key material generation
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200311static int ssl3_prf( const unsigned char *secret, size_t slen,
312 const char *label,
313 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000314 unsigned char *dstbuf, size_t dlen )
315{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100316 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000317 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200318 mbedtls_md5_context md5;
319 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000320 unsigned char padding[16];
321 unsigned char sha1sum[20];
322 ((void)label);
323
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200324 mbedtls_md5_init( &md5 );
325 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200326
Paul Bakker5f70b252012-09-13 14:23:06 +0000327 /*
328 * SSLv3:
329 * block =
330 * MD5( secret + SHA1( 'A' + secret + random ) ) +
331 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
332 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
333 * ...
334 */
335 for( i = 0; i < dlen / 16; i++ )
336 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200337 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000338
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100339 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100340 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100341 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100342 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100343 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100344 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100345 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100346 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100347 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100348 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000349
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100350 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100351 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100352 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100353 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100354 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100355 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100356 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100357 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000358 }
359
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100360exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200361 mbedtls_md5_free( &md5 );
362 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000363
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500364 mbedtls_platform_zeroize( padding, sizeof( padding ) );
365 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000366
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100367 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200372static int tls1_prf( const unsigned char *secret, size_t slen,
373 const char *label,
374 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000375 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000376{
Paul Bakker23986e52011-04-24 08:57:21 +0000377 size_t nb, hs;
378 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200379 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char tmp[128];
381 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 const mbedtls_md_info_t *md_info;
383 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100384 int ret;
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 hs = ( slen + 1 ) / 2;
392 S1 = secret;
393 S2 = secret + slen - hs;
394
395 nb = strlen( label );
396 memcpy( tmp + 20, label, nb );
397 memcpy( tmp + 20 + nb, random, rlen );
398 nb += rlen;
399
400 /*
401 * First compute P_md5(secret,label+random)[0..dlen]
402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100407 return( ret );
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
410 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
411 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 for( i = 0; i < dlen; i += 16 )
414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_md_hmac_reset ( &md_ctx );
416 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
417 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_md_hmac_reset ( &md_ctx );
420 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
421 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
424
425 for( j = 0; j < k; j++ )
426 dstbuf[i + j] = h_i[j];
427 }
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100430
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 /*
432 * XOR out with P_sha1(secret,label+random)[0..dlen]
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
435 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100438 return( ret );
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
441 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
442 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444 for( i = 0; i < dlen; i += 20 )
445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_md_hmac_reset ( &md_ctx );
447 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
448 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_hmac_reset ( &md_ctx );
451 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
452 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
455
456 for( j = 0; j < k; j++ )
457 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
458 }
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100461
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500462 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
463 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 return( 0 );
466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
470static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100471 const unsigned char *secret, size_t slen,
472 const char *label,
473 const unsigned char *random, size_t rlen,
474 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475{
476 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100477 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000478 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
480 const mbedtls_md_info_t *md_info;
481 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100482 int ret;
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100490
491 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000493
494 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100495 memcpy( tmp + md_len, label, nb );
496 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000497 nb += rlen;
498
499 /*
500 * Compute P_<hash>(secret, label + random)[0..dlen]
501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100503 return( ret );
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
506 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
507 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100508
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100509 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_md_hmac_reset ( &md_ctx );
512 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
513 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_md_hmac_reset ( &md_ctx );
516 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
517 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000518
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100519 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000520
521 for( j = 0; j < k; j++ )
522 dstbuf[i + j] = h_i[j];
523 }
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100526
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500527 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
528 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000529
530 return( 0 );
531}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100534static int tls_prf_sha256( const unsigned char *secret, size_t slen,
535 const char *label,
536 const unsigned char *random, size_t rlen,
537 unsigned char *dstbuf, size_t dlen )
538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100540 label, random, rlen, dstbuf, dlen ) );
541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200545static int tls_prf_sha384( const unsigned char *secret, size_t slen,
546 const char *label,
547 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000548 unsigned char *dstbuf, size_t dlen )
549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100551 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_SHA512_C */
554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
559 defined(MBEDTLS_SSL_PROTO_TLS1_1)
560static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200561#endif
Paul Bakker380da532012-04-18 16:10:25 +0000562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#if defined(MBEDTLS_SSL_PROTO_SSL3)
564static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
565static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200566#endif
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
569static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
570static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200571#endif
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
574#if defined(MBEDTLS_SHA256_C)
575static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
576static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
577static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200578#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#if defined(MBEDTLS_SHA512_C)
581static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
582static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
583static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000588{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200589 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 unsigned char keyblk[256];
592 unsigned char *key1;
593 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100594 unsigned char *mac_enc;
595 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000596 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200597 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 const mbedtls_cipher_info_t *cipher_info;
599 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_ssl_session *session = ssl->session_negotiate;
602 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
603 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100608 if( cipher_info == NULL )
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100611 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100613 }
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100616 if( md_info == NULL )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100619 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100621 }
622
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000624 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_SSL_PROTO_SSL3)
627 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000628 {
Paul Bakker48916f92012-09-16 19:57:18 +0000629 handshake->tls_prf = ssl3_prf;
630 handshake->calc_verify = ssl_calc_verify_ssl;
631 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000632 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200633 else
634#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
636 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000637 {
Paul Bakker48916f92012-09-16 19:57:18 +0000638 handshake->tls_prf = tls1_prf;
639 handshake->calc_verify = ssl_calc_verify_tls;
640 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200642 else
643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
645#if defined(MBEDTLS_SHA512_C)
646 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
647 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000648 {
Paul Bakker48916f92012-09-16 19:57:18 +0000649 handshake->tls_prf = tls_prf_sha384;
650 handshake->calc_verify = ssl_calc_verify_tls_sha384;
651 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000652 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000653 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200654#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SHA256_C)
656 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000657 {
Paul Bakker48916f92012-09-16 19:57:18 +0000658 handshake->tls_prf = tls_prf_sha256;
659 handshake->calc_verify = ssl_calc_verify_tls_sha256;
660 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000661 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200662 else
663#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
667 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200668 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000669
670 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 * SSLv3:
672 * master =
673 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
674 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
675 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200676 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 * master = PRF( premaster, "master secret", randbytes )[0..47]
679 */
Paul Bakker0a597072012-09-25 21:55:46 +0000680 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000683 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
686 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200687 {
688 unsigned char session_hash[48];
689 size_t hash_len;
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200692
693 ssl->handshake->calc_verify( ssl, session_hash );
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
696 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200699 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200701 {
702 hash_len = 48;
703 }
704 else
705#endif
706 hash_len = 32;
707 }
708 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200710 hash_len = 36;
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200713
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100714 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
715 "extended master secret",
716 session_hash, hash_len,
717 session->master, 48 );
718 if( ret != 0 )
719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100721 return( ret );
722 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200723
724 }
725 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200726#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100727 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
728 "master secret",
729 handshake->randbytes, 64,
730 session->master, 48 );
731 if( ret != 0 )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100734 return( ret );
735 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200736
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500737 mbedtls_platform_zeroize( handshake->premaster,
738 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 }
740 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743 /*
744 * Swap the client and server random values.
745 */
Paul Bakker48916f92012-09-16 19:57:18 +0000746 memcpy( tmp, handshake->randbytes, 64 );
747 memcpy( handshake->randbytes, tmp + 32, 32 );
748 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500749 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751 /*
752 * SSLv3:
753 * key block =
754 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
755 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
756 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
757 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
758 * ...
759 *
760 * TLSv1:
761 * key block = PRF( master, "key expansion", randbytes )
762 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100763 ret = handshake->tls_prf( session->master, 48, "key expansion",
764 handshake->randbytes, 64, keyblk, 256 );
765 if( ret != 0 )
766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100768 return( ret );
769 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
772 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
773 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
774 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
775 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500777 mbedtls_platform_zeroize( handshake->randbytes,
778 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780 /*
781 * Determine the appropriate key, IV and MAC length.
782 */
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200784 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200787 cipher_info->mode == MBEDTLS_MODE_CCM ||
788 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200790 size_t taglen, explicit_ivlen;
791
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200792 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000793 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200794
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200795 /* All modes haves 96-bit IVs;
796 * GCM and CCM has 4 implicit and 8 explicit bytes
797 * ChachaPoly has all 12 bytes implicit
798 */
Paul Bakker68884e32013-01-07 18:20:04 +0100799 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200800 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
801 transform->fixed_ivlen = 12;
802 else
803 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200804
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200805 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
806 taglen = transform->ciphersuite_info->flags &
807 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
808
809
810 /* Minimum length of encrypted record */
811 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
812 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100813 }
814 else
815 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200816 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
818 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200821 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100822 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200824 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000825 mac_key_len = mbedtls_md_get_size( md_info );
826 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200829 /*
830 * If HMAC is to be truncated, we shall keep the leftmost bytes,
831 * (rfc 6066 page 13 or rfc 2104 section 4),
832 * so we only need to adjust the length here.
833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000837
838#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
839 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000840 * HMAC implementation which also truncates the key
841 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000842 mac_key_len = transform->maclen;
843#endif
844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200846
847 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100848 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200850 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200852 transform->minlen = transform->maclen;
853 else
Paul Bakker68884e32013-01-07 18:20:04 +0100854 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200855 /*
856 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100857 * 1. if EtM is in use: one block plus MAC
858 * otherwise: * first multiple of blocklen greater than maclen
859 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
862 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100863 {
864 transform->minlen = transform->maclen
865 + cipher_info->block_size;
866 }
867 else
868#endif
869 {
870 transform->minlen = transform->maclen
871 + cipher_info->block_size
872 - transform->maclen % cipher_info->block_size;
873 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
876 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
877 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200878 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100879 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
882 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
883 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200884 {
885 transform->minlen += transform->ivlen;
886 }
887 else
888#endif
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200892 }
Paul Bakker68884e32013-01-07 18:20:04 +0100893 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 }
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000897 transform->keylen, transform->minlen, transform->ivlen,
898 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
900 /*
901 * Finally setup the cipher contexts, IVs and MAC secrets.
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200904 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000906 key1 = keyblk + mac_key_len * 2;
907 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakker68884e32013-01-07 18:20:04 +0100909 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000912 /*
913 * This is not used in TLS v1.1.
914 */
Paul Bakker48916f92012-09-16 19:57:18 +0000915 iv_copy_len = ( transform->fixed_ivlen ) ?
916 transform->fixed_ivlen : transform->ivlen;
917 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
918 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000919 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 }
921 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_SSL_CLI_C */
923#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200924 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000926 key1 = keyblk + mac_key_len * 2 + transform->keylen;
927 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Hanno Becker81c7b182017-11-09 18:39:33 +0000929 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100930 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000932 /*
933 * This is not used in TLS v1.1.
934 */
Paul Bakker48916f92012-09-16 19:57:18 +0000935 iv_copy_len = ( transform->fixed_ivlen ) ?
936 transform->fixed_ivlen : transform->ivlen;
937 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
938 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000939 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100941 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100946 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#if defined(MBEDTLS_SSL_PROTO_SSL3)
949 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100950 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000951 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100955 }
956
Hanno Becker81c7b182017-11-09 18:39:33 +0000957 memcpy( transform->mac_enc, mac_enc, mac_key_len );
958 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100959 }
960 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#endif /* MBEDTLS_SSL_PROTO_SSL3 */
962#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
963 defined(MBEDTLS_SSL_PROTO_TLS1_2)
964 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100965 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100966 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
967 For AEAD-based ciphersuites, there is nothing to do here. */
968 if( mac_key_len != 0 )
969 {
970 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
971 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
972 }
Paul Bakker68884e32013-01-07 18:20:04 +0100973 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200974 else
975#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200979 }
Paul Bakker68884e32013-01-07 18:20:04 +0100980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
982 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000983 {
984 int ret = 0;
985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100989 transform->iv_enc, transform->iv_dec,
990 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100991 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000992 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
995 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000996 }
997 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000999
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001000#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1001 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001002 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001003 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1004 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001005 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001006 iv_copy_len );
1007 }
1008#endif
1009
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001010 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001011 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001014 return( ret );
1015 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001016
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001017 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001018 cipher_info ) ) != 0 )
1019 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001021 return( ret );
1022 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001025 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 return( ret );
1030 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001033 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001037 return( ret );
1038 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_MODE_CBC)
1041 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1044 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001047 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001048 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1051 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001054 return( ret );
1055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001059 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001062 // Initialize compression
1063 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001065 {
Paul Bakker16770332013-10-11 09:59:44 +02001066 if( ssl->compress_buf == NULL )
1067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001069 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001070 if( ssl->compress_buf == NULL )
1071 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001073 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001074 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001075 }
1076 }
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001079
Paul Bakker48916f92012-09-16 19:57:18 +00001080 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1081 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001082
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001083 if( deflateInit( &transform->ctx_deflate,
1084 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001085 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1088 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001089 }
1090 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
1095 return( 0 );
1096}
1097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SSL_PROTO_SSL3)
1099void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001101 mbedtls_md5_context md5;
1102 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 unsigned char pad_1[48];
1104 unsigned char pad_2[48];
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001108 mbedtls_md5_init( &md5 );
1109 mbedtls_sha1_init( &sha1 );
1110
1111 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1112 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
Paul Bakker380da532012-04-18 16:10:25 +00001114 memset( pad_1, 0x36, 48 );
1115 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001117 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1118 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1119 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001121 mbedtls_md5_starts_ret( &md5 );
1122 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1123 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1124 mbedtls_md5_update_ret( &md5, hash, 16 );
1125 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001127 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1128 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1129 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001130
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001131 mbedtls_sha1_starts_ret( &sha1 );
1132 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1133 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1134 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1135 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001139
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001140 mbedtls_md5_free( &md5 );
1141 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001142
Paul Bakker380da532012-04-18 16:10:25 +00001143 return;
1144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1148void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001149{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001150 mbedtls_md5_context md5;
1151 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001154
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001155 mbedtls_md5_init( &md5 );
1156 mbedtls_sha1_init( &sha1 );
1157
1158 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1159 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001160
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001161 mbedtls_md5_finish_ret( &md5, hash );
1162 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001166
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001167 mbedtls_md5_free( &md5 );
1168 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001169
Paul Bakker380da532012-04-18 16:10:25 +00001170 return;
1171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1175#if defined(MBEDTLS_SHA256_C)
1176void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001177{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001178 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001179
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001180 mbedtls_sha256_init( &sha256 );
1181
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001183
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001184 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001185 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001189
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001190 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001191
Paul Bakker380da532012-04-18 16:10:25 +00001192 return;
1193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_SHA512_C)
1197void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001198{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001199 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001200
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001201 mbedtls_sha512_init( &sha512 );
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001204
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001205 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001206 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001211 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001212
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 return;
1214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_SHA512_C */
1216#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1219int 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 +02001220{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001221 unsigned char *p = ssl->handshake->premaster;
1222 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001223 const unsigned char *psk = ssl->conf->psk;
1224 size_t psk_len = ssl->conf->psk_len;
1225
1226 /* If the psk callback was called, use its result */
1227 if( ssl->handshake->psk != NULL )
1228 {
1229 psk = ssl->handshake->psk;
1230 psk_len = ssl->handshake->psk_len;
1231 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001232
1233 /*
1234 * PMS = struct {
1235 * opaque other_secret<0..2^16-1>;
1236 * opaque psk<0..2^16-1>;
1237 * };
1238 * with "other_secret" depending on the particular key exchange
1239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1241 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001242 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001243 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001245
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001246 *(p++) = (unsigned char)( psk_len >> 8 );
1247 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001248
1249 if( end < p || (size_t)( end - p ) < psk_len )
1250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1251
1252 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001253 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001254 }
1255 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1257#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1258 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001259 {
1260 /*
1261 * other_secret already set by the ClientKeyExchange message,
1262 * and is 48 bytes long
1263 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001264 if( end - p < 2 )
1265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1266
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001267 *p++ = 0;
1268 *p++ = 48;
1269 p += 48;
1270 }
1271 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1273#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1274 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001275 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001276 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001277 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001278
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001279 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001281 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001282 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001285 return( ret );
1286 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001287 *(p++) = (unsigned char)( len >> 8 );
1288 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001289 p += len;
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001292 }
1293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1295#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1296 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001297 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001298 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001299 size_t zlen;
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001302 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001303 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001306 return( ret );
1307 }
1308
1309 *(p++) = (unsigned char)( zlen >> 8 );
1310 *(p++) = (unsigned char)( zlen );
1311 p += zlen;
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001314 }
1315 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001320 }
1321
1322 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001323 if( end - p < 2 )
1324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001325
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001326 *(p++) = (unsigned char)( psk_len >> 8 );
1327 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001328
1329 if( end < p || (size_t)( end - p ) < psk_len )
1330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1331
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001332 memcpy( p, psk, psk_len );
1333 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001334
1335 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1336
1337 return( 0 );
1338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001342/*
1343 * SSLv3.0 MAC functions
1344 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001345#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001346static void ssl_mac( mbedtls_md_context_t *md_ctx,
1347 const unsigned char *secret,
1348 const unsigned char *buf, size_t len,
1349 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001350 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001351{
1352 unsigned char header[11];
1353 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001354 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1356 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001357
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001358 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001360 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001361 else
Paul Bakker68884e32013-01-07 18:20:04 +01001362 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
1364 memcpy( header, ctr, 8 );
1365 header[ 8] = (unsigned char) type;
1366 header[ 9] = (unsigned char)( len >> 8 );
1367 header[10] = (unsigned char)( len );
1368
Paul Bakker68884e32013-01-07 18:20:04 +01001369 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_md_starts( md_ctx );
1371 mbedtls_md_update( md_ctx, secret, md_size );
1372 mbedtls_md_update( md_ctx, padding, padlen );
1373 mbedtls_md_update( md_ctx, header, 11 );
1374 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001375 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Paul Bakker68884e32013-01-07 18:20:04 +01001377 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 mbedtls_md_starts( md_ctx );
1379 mbedtls_md_update( md_ctx, secret, md_size );
1380 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001381 mbedtls_md_update( md_ctx, out, md_size );
1382 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1387 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001388 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001389#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001390#endif
1391
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001392/* The function below is only used in the Lucky 13 counter-measure in
1393 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1394#if defined(SSL_SOME_MODES_USE_MAC) && \
1395 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1396 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1397 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1398/* This function makes sure every byte in the memory region is accessed
1399 * (in ascending addresses order) */
1400static void ssl_read_memory( unsigned char *p, size_t len )
1401{
1402 unsigned char acc = 0;
1403 volatile unsigned char force;
1404
1405 for( ; len != 0; p++, len-- )
1406 acc ^= *p;
1407
1408 force = acc;
1409 (void) force;
1410}
1411#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1412
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001413/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001419 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001423 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1426 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001427 }
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001432 ssl->out_msg, ssl->out_msglen );
1433
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001435 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001437#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( mode == MBEDTLS_MODE_STREAM ||
1439 ( mode == MBEDTLS_MODE_CBC
1440#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1441 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001442#endif
1443 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_SSL_PROTO_SSL3)
1446 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001447 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001448 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001449
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001450 ssl_mac( &ssl->transform_out->md_ctx_enc,
1451 ssl->transform_out->mac_enc,
1452 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001453 ssl->out_ctr, ssl->out_msgtype,
1454 mac );
1455
1456 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001457 }
1458 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1461 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1462 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001463 {
Hanno Becker992b6872017-11-09 18:57:39 +00001464 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1467 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1468 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1469 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001470 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001471 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001473
1474 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001475 }
1476 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001477#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001481 }
1482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001484 ssl->out_msg + ssl->out_msglen,
1485 ssl->transform_out->maclen );
1486
1487 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001488 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001489 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001490#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001492 /*
1493 * Encrypt
1494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1496 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001498 int ret;
1499 size_t olen = 0;
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 "including %d bytes of padding",
1503 ssl->out_msglen, 0 ) );
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001506 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001507 ssl->transform_out->ivlen,
1508 ssl->out_msg, ssl->out_msglen,
1509 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001512 return( ret );
1513 }
1514
1515 if( ssl->out_msglen != olen )
1516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1518 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001519 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 }
Paul Bakker68884e32013-01-07 18:20:04 +01001521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001523#if defined(MBEDTLS_GCM_C) || \
1524 defined(MBEDTLS_CCM_C) || \
1525 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001527 mode == MBEDTLS_MODE_CCM ||
1528 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001530 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001531 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001532 unsigned char *enc_msg;
1533 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001534 unsigned char iv[12];
1535 mbedtls_ssl_transform *transform = ssl->transform_out;
1536 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001538 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001539
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001540 /*
1541 * Prepare additional authenticated data
1542 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001543 memcpy( add_data, ssl->out_ctr, 8 );
1544 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001546 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001547 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1548 add_data[12] = ssl->out_msglen & 0xFF;
1549
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001550 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551
Paul Bakker68884e32013-01-07 18:20:04 +01001552 /*
1553 * Generate IV
1554 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001555 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1556 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001557 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001558 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1559 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1560 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1561
1562 }
1563 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1564 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001565 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001566 unsigned char i;
1567
1568 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1569
1570 for( i = 0; i < 8; i++ )
1571 iv[i+4] ^= ssl->out_ctr[i];
1572 }
1573 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001574 {
1575 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1577 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001578 }
1579
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1581 iv, transform->ivlen );
1582 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1583 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001584
Paul Bakker68884e32013-01-07 18:20:04 +01001585 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001586 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001587 */
1588 enc_msg = ssl->out_msg;
1589 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001590 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001593 "including 0 bytes of padding",
1594 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001595
Paul Bakker68884e32013-01-07 18:20:04 +01001596 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001597 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001598 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001599 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1600 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001601 add_data, 13,
1602 enc_msg, enc_msglen,
1603 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001604 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001607 return( ret );
1608 }
1609
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001610 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001614 }
1615
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001616 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001617 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1623#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001624 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001626 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001627 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001628 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001629 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001630
Paul Bakker48916f92012-09-16 19:57:18 +00001631 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1632 ssl->transform_out->ivlen;
1633 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 padlen = 0;
1635
1636 for( i = 0; i <= padlen; i++ )
1637 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1638
1639 ssl->out_msglen += padlen + 1;
1640
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641 enc_msglen = ssl->out_msglen;
1642 enc_msg = ssl->out_msg;
1643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001646 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1647 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 {
1651 /*
1652 * Generate IV
1653 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001654 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001655 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001656 if( ret != 0 )
1657 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001658
Paul Bakker92be97b2013-01-02 17:30:03 +01001659 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001660 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001661
1662 /*
1663 * Fix pointer positions and message length with added IV
1664 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001665 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001666 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001667 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001672 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001673 ssl->out_msglen, ssl->transform_out->ivlen,
1674 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001677 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001678 ssl->transform_out->ivlen,
1679 enc_msg, enc_msglen,
1680 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001683 return( ret );
1684 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001685
Paul Bakkercca5b812013-08-31 17:40:26 +02001686 if( enc_msglen != olen )
1687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001690 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1693 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001694 {
1695 /*
1696 * Save IV in SSL3 and TLS1
1697 */
1698 memcpy( ssl->transform_out->iv_enc,
1699 ssl->transform_out->cipher_ctx_enc.iv,
1700 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001702#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001705 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001706 {
1707 /*
1708 * MAC(MAC_write_key, seq_num +
1709 * TLSCipherText.type +
1710 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001711 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001712 * IV + // except for TLS 1.0
1713 * ENC(content + padding + padding_length));
1714 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001715 unsigned char pseudo_hdr[13];
1716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001718
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1720 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001721 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1722 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1727 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001728 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001730 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001732
1733 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001734 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001735 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001740 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001744 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001746 /* Make extra sure authentication was performed, exactly once */
1747 if( auth_done != 1 )
1748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1750 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 }
1752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
1755 return( 0 );
1756}
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001761 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001762#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001763 size_t padlen = 0, correct = 1;
1764#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001768 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001772 }
1773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001775
Paul Bakker48916f92012-09-16 19:57:18 +00001776 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001779 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 }
1782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1784 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001785 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001786 int ret;
1787 size_t olen = 0;
1788
Paul Bakker68884e32013-01-07 18:20:04 +01001789 padlen = 0;
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001792 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001793 ssl->transform_in->ivlen,
1794 ssl->in_msg, ssl->in_msglen,
1795 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001798 return( ret );
1799 }
1800
1801 if( ssl->in_msglen != olen )
1802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 }
Paul Bakker68884e32013-01-07 18:20:04 +01001807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001809#if defined(MBEDTLS_GCM_C) || \
1810 defined(MBEDTLS_CCM_C) || \
1811 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001813 mode == MBEDTLS_MODE_CCM ||
1814 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001815 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001816 int ret;
1817 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001818 unsigned char *dec_msg;
1819 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001820 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001821 unsigned char iv[12];
1822 mbedtls_ssl_transform *transform = ssl->transform_in;
1823 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001825 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001826
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001827 /*
1828 * Compute and update sizes
1829 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001830 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001833 "+ taglen (%d)", ssl->in_msglen,
1834 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001836 }
1837 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1838
Paul Bakker68884e32013-01-07 18:20:04 +01001839 dec_msg = ssl->in_msg;
1840 dec_msg_result = ssl->in_msg;
1841 ssl->in_msglen = dec_msglen;
1842
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001843 /*
1844 * Prepare additional authenticated data
1845 */
Paul Bakker68884e32013-01-07 18:20:04 +01001846 memcpy( add_data, ssl->in_ctr, 8 );
1847 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001849 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001850 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1851 add_data[12] = ssl->in_msglen & 0xFF;
1852
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001853 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001854
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 /*
1856 * Prepare IV
1857 */
1858 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1859 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001860 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001861 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1862 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001863
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001864 }
1865 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1866 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001867 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001868 unsigned char i;
1869
1870 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1871
1872 for( i = 0; i < 8; i++ )
1873 iv[i+4] ^= ssl->in_ctr[i];
1874 }
1875 else
1876 {
1877 /* Reminder if we ever add an AEAD mode with a different size */
1878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1880 }
1881
1882 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001884
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001885 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001886 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001889 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001890 add_data, 13,
1891 dec_msg, dec_msglen,
1892 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001893 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1898 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001899
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001900 return( ret );
1901 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001902 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001903
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001904 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001908 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001909 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1912#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001913 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 {
Paul Bakker45829992013-01-03 14:52:21 +01001916 /*
1917 * Decrypt and check the padding
1918 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001919 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001920 unsigned char *dec_msg;
1921 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001922 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001923 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001924 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 /*
Paul Bakker45829992013-01-03 14:52:21 +01001927 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1930 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001931 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001932#endif
Paul Bakker45829992013-01-03 14:52:21 +01001933
1934 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1935 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001938 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1939 ssl->transform_in->ivlen,
1940 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001942 }
1943
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001944 dec_msglen = ssl->in_msglen;
1945 dec_msg = ssl->in_msg;
1946 dec_msg_result = ssl->in_msg;
1947
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001948 /*
1949 * Authenticate before decrypt if enabled
1950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1952 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001953 {
Hanno Becker992b6872017-11-09 18:57:39 +00001954 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001955 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001958
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001959 dec_msglen -= ssl->transform_in->maclen;
1960 ssl->in_msglen -= ssl->transform_in->maclen;
1961
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001962 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1963 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1964 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1965 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1970 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001971 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001972 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001977 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 ssl->transform_in->maclen );
1979
Hanno Becker992b6872017-11-09 18:57:39 +00001980 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1981 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001987 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001988 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990
1991 /*
1992 * Check length sanity
1993 */
1994 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001997 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001999 }
2000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002002 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002003 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002006 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002007 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002008 dec_msglen -= ssl->transform_in->ivlen;
2009 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002010
Paul Bakker48916f92012-09-16 19:57:18 +00002011 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002012 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002017 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002018 ssl->transform_in->ivlen,
2019 dec_msg, dec_msglen,
2020 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002023 return( ret );
2024 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002025
Paul Bakkercca5b812013-08-31 17:40:26 +02002026 if( dec_msglen != olen )
2027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002030 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2033 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002034 {
2035 /*
2036 * Save IV in SSL3 and TLS1
2037 */
2038 memcpy( ssl->transform_in->iv_dec,
2039 ssl->transform_in->cipher_ctx_dec.iv,
2040 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002045
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002046 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002047 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_SSL_DEBUG_ALL)
2050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002051 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002052#endif
Paul Bakker45829992013-01-03 14:52:21 +01002053 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002054 correct = 0;
2055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_SSL_PROTO_SSL3)
2058 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 {
Paul Bakker48916f92012-09-16 19:57:18 +00002060 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_SSL_DEBUG_ALL)
2063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002065 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002066#endif
Paul Bakker45829992013-01-03 14:52:21 +01002067 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069 }
2070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2072#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2073 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2074 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 {
2076 /*
Paul Bakker45829992013-01-03 14:52:21 +01002077 * TLSv1+: always check the padding up to the first failure
2078 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002080 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002081 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002083
Paul Bakker956c9e02013-12-19 14:42:28 +01002084 /*
2085 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002086 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002087 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002088 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002089 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002090 *
2091 * In both cases we reset padding_idx to a safe value (0) to
2092 * prevent out-of-buffer reads.
2093 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002094 correct &= ( padlen <= ssl->in_msglen );
2095 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002096 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002097
2098 padding_idx *= correct;
2099
Angus Grattonb512bc12018-06-19 15:57:50 +10002100 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002101 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002102 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002103 pad_count += real_count *
2104 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2105 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002106
2107 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002110 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002112#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002113 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2117 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002121 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002122
2123 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002127 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002131 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002133#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002137
2138 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002139 * Authenticate if not done yet.
2140 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002142#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002143 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002144 {
Hanno Becker992b6872017-11-09 18:57:39 +00002145 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002146
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002147 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002149 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2150 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_SSL_PROTO_SSL3)
2153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002154 {
2155 ssl_mac( &ssl->transform_in->md_ctx_dec,
2156 ssl->transform_in->mac_dec,
2157 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002158 ssl->in_ctr, ssl->in_msgtype,
2159 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002160 }
2161 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2163#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2164 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2165 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002166 {
2167 /*
2168 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002169 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002170 *
2171 * Known timing attacks:
2172 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2173 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002174 * To compensate for different timings for the MAC calculation
2175 * depending on how much padding was removed (which is determined
2176 * by padlen), process extra_run more blocks through the hash
2177 * function.
2178 *
2179 * The formula in the paper is
2180 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2181 * where L1 is the size of the header plus the decrypted message
2182 * plus CBC padding and L2 is the size of the header plus the
2183 * decrypted message. This is for an underlying hash function
2184 * with 64-byte blocks.
2185 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2186 * correctly. We round down instead of up, so -56 is the correct
2187 * value for our calculations instead of -55.
2188 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002189 * Repeat the formula rather than defining a block_size variable.
2190 * This avoids requiring division by a variable at runtime
2191 * (which would be marginally less efficient and would require
2192 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002193 */
2194 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002195
2196 /*
2197 * The next two sizes are the minimum and maximum values of
2198 * in_msglen over all padlen values.
2199 *
2200 * They're independent of padlen, since we previously did
2201 * in_msglen -= padlen.
2202 *
2203 * Note that max_len + maclen is never more than the buffer
2204 * length, as we previously did in_msglen -= maclen too.
2205 */
2206 const size_t max_len = ssl->in_msglen + padlen;
2207 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2208
Gilles Peskine20b44082018-05-29 14:06:49 +02002209 switch( ssl->transform_in->ciphersuite_info->mac )
2210 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002211#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2212 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002213 case MBEDTLS_MD_MD5:
2214 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002215 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002216 /* 8 bytes of message size, 64-byte compression blocks */
2217 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2218 ( 13 + ssl->in_msglen + 8 ) / 64;
2219 break;
2220#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002221#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002222 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002223 /* 16 bytes of message size, 128-byte compression blocks */
2224 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2225 ( 13 + ssl->in_msglen + 16 ) / 128;
2226 break;
2227#endif
2228 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2231 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002232
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2236 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2237 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2238 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002240 /* Make sure we access everything even when padlen > 0. This
2241 * makes the synchronisation requirements for just-in-time
2242 * Prime+Probe attacks much tighter and hopefully impractical. */
2243 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002244 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002245
2246 /* Call mbedtls_md_process at least once due to cache attacks
2247 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002248 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002252
2253 /* Make sure we access all the memory that could contain the MAC,
2254 * before we check it in the next code block. This makes the
2255 * synchronisation requirements for just-in-time Prime+Probe
2256 * attacks much tighter and hopefully impractical. */
2257 ssl_read_memory( ssl->in_msg + min_len,
2258 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002259 }
2260 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2262 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002268#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002269 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2270 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2271 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Hanno Becker992b6872017-11-09 18:57:39 +00002274 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2275 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277#if defined(MBEDTLS_SSL_DEBUG_ALL)
2278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002279#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002280 correct = 0;
2281 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002284 /*
2285 * Finally check the correct flag
2286 */
2287 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002289 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002290#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002291
2292 /* Make extra sure authentication was performed, exactly once */
2293 if( auth_done != 1 )
2294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 if( ssl->in_msglen == 0 )
2300 {
Angus Gratton34817922018-06-19 15:58:22 +10002301#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2302 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2303 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2304 {
2305 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2307 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2308 }
2309#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2310
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 ssl->nb_zero++;
2312
2313 /*
2314 * Three or more empty messages may be a DoS attack
2315 * (excessive CPU consumption).
2316 */
2317 if( ssl->nb_zero > 3 )
2318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
2323 }
2324 else
2325 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002328 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002329 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002330 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002331 }
2332 else
2333#endif
2334 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002336 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2337 if( ++ssl->in_ctr[i - 1] != 0 )
2338 break;
2339
2340 /* The loop goes to its end iff the counter is wrapping */
2341 if( i == ssl_ep_len( ssl ) )
2342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2344 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002345 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002346 }
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
2350 return( 0 );
2351}
2352
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002353#undef MAC_NONE
2354#undef MAC_PLAINTEXT
2355#undef MAC_CIPHERTEXT
2356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358/*
2359 * Compression/decompression functions
2360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362{
2363 int ret;
2364 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002365 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002366 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002367 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002371 if( len_pre == 0 )
2372 return( 0 );
2373
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374 memcpy( msg_pre, ssl->out_msg, len_pre );
2375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002377 ssl->out_msglen ) );
2378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380 ssl->out_msg, ssl->out_msglen );
2381
Paul Bakker48916f92012-09-16 19:57:18 +00002382 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2383 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2384 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002385 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386
Paul Bakker48916f92012-09-16 19:57:18 +00002387 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 if( ret != Z_OK )
2389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2391 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002392 }
2393
Angus Grattond8213d02016-05-25 20:56:48 +10002394 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002395 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002398 ssl->out_msglen ) );
2399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002401 ssl->out_msg, ssl->out_msglen );
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404
2405 return( 0 );
2406}
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409{
2410 int ret;
2411 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002412 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002413 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002414 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002418 if( len_pre == 0 )
2419 return( 0 );
2420
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421 memcpy( msg_pre, ssl->in_msg, len_pre );
2422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424 ssl->in_msglen ) );
2425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 ssl->in_msg, ssl->in_msglen );
2428
Paul Bakker48916f92012-09-16 19:57:18 +00002429 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2430 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2431 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002432 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002433 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434
Paul Bakker48916f92012-09-16 19:57:18 +00002435 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436 if( ret != Z_OK )
2437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2439 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440 }
2441
Angus Grattond8213d02016-05-25 20:56:48 +10002442 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002443 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446 ssl->in_msglen ) );
2447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002449 ssl->in_msg, ssl->in_msglen );
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002452
2453 return( 0 );
2454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2458static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_SSL_PROTO_DTLS)
2461static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002462{
2463 /* If renegotiation is not enforced, retransmit until we would reach max
2464 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002465 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002466 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002467 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002468 unsigned char doublings = 1;
2469
2470 while( ratio != 0 )
2471 {
2472 ++doublings;
2473 ratio >>= 1;
2474 }
2475
2476 if( ++ssl->renego_records_seen > doublings )
2477 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002479 return( 0 );
2480 }
2481 }
2482
2483 return( ssl_write_hello_request( ssl ) );
2484}
2485#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002487
Paul Bakker5121ce52009-01-03 21:22:43 +00002488/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002489 * Fill the input message buffer by appending data to it.
2490 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002491 *
2492 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2493 * available (from this read and/or a previous one). Otherwise, an error code
2494 * is returned (possibly EOF or WANT_READ).
2495 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002496 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2497 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2498 * since we always read a whole datagram at once.
2499 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002500 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002501 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504{
Paul Bakker23986e52011-04-24 08:57:21 +00002505 int ret;
2506 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002510 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002513 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002515 }
2516
Angus Grattond8213d02016-05-25 20:56:48 +10002517 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002521 }
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002526 uint32_t timeout;
2527
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002528 /* Just to be sure */
2529 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2530 {
2531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2532 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2534 }
2535
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002536 /*
2537 * The point is, we need to always read a full datagram at once, so we
2538 * sometimes read more then requested, and handle the additional data.
2539 * It could be the rest of the current record (while fetching the
2540 * header) and/or some other records in the same datagram.
2541 */
2542
2543 /*
2544 * Move to the next record in the already read datagram if applicable
2545 */
2546 if( ssl->next_record_offset != 0 )
2547 {
2548 if( ssl->in_left < ssl->next_record_offset )
2549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002552 }
2553
2554 ssl->in_left -= ssl->next_record_offset;
2555
2556 if( ssl->in_left != 0 )
2557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002559 ssl->next_record_offset ) );
2560 memmove( ssl->in_hdr,
2561 ssl->in_hdr + ssl->next_record_offset,
2562 ssl->in_left );
2563 }
2564
2565 ssl->next_record_offset = 0;
2566 }
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002570
2571 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002572 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002573 */
2574 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002577 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002578 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002579
2580 /*
2581 * A record can't be split accross datagrams. If we need to read but
2582 * are not at the beginning of a new record, the caller did something
2583 * wrong.
2584 */
2585 if( ssl->in_left != 0 )
2586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002589 }
2590
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002591 /*
2592 * Don't even try to read if time's out already.
2593 * This avoids by-passing the timer when repeatedly receiving messages
2594 * that will end up being dropped.
2595 */
2596 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002597 {
2598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002599 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002600 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002601 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002602 {
Angus Grattond8213d02016-05-25 20:56:48 +10002603 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002606 timeout = ssl->handshake->retransmit_timeout;
2607 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002608 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002611
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002612 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002613 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2614 timeout );
2615 else
2616 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002619
2620 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002622 }
2623
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002624 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002627 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002630 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002631 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002634 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002635 }
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002640 return( ret );
2641 }
2642
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002643 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002646 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002648 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002649 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002652 return( ret );
2653 }
2654
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002655 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002658 }
2659
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 if( ret < 0 )
2661 return( ret );
2662
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002663 ssl->in_left = ret;
2664 }
2665 else
2666#endif
2667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002669 ssl->in_left, nb_want ) );
2670
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002671 while( ssl->in_left < nb_want )
2672 {
2673 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002674
2675 if( ssl_check_timer( ssl ) != 0 )
2676 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2677 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002678 {
2679 if( ssl->f_recv_timeout != NULL )
2680 {
2681 ret = ssl->f_recv_timeout( ssl->p_bio,
2682 ssl->in_hdr + ssl->in_left, len,
2683 ssl->conf->read_timeout );
2684 }
2685 else
2686 {
2687 ret = ssl->f_recv( ssl->p_bio,
2688 ssl->in_hdr + ssl->in_left, len );
2689 }
2690 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002693 ssl->in_left, nb_want ) );
2694 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002695
2696 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002698
2699 if( ret < 0 )
2700 return( ret );
2701
mohammad160352aecb92018-03-28 23:41:40 -07002702 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002703 {
Darryl Green11999bb2018-03-13 15:22:58 +00002704 MBEDTLS_SSL_DEBUG_MSG( 1,
2705 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002706 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2708 }
2709
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002710 ssl->in_left += ret;
2711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
2716 return( 0 );
2717}
2718
2719/*
2720 * Flush any data not yet written
2721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002724 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002725 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002729 if( ssl->f_send == NULL )
2730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002732 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002734 }
2735
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002736 /* Avoid incrementing counter if data is flushed */
2737 if( ssl->out_left == 0 )
2738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002740 return( 0 );
2741 }
2742
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 while( ssl->out_left > 0 )
2744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2746 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Hanno Becker2b1e3542018-08-06 11:19:13 +01002748 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002749 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
2753 if( ret <= 0 )
2754 return( ret );
2755
mohammad160352aecb92018-03-28 23:41:40 -07002756 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002757 {
Darryl Green11999bb2018-03-13 15:22:58 +00002758 MBEDTLS_SSL_DEBUG_MSG( 1,
2759 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002760 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2762 }
2763
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 ssl->out_left -= ret;
2765 }
2766
Hanno Becker2b1e3542018-08-06 11:19:13 +01002767#if defined(MBEDTLS_SSL_PROTO_DTLS)
2768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2769 {
2770 ssl->out_hdr = ssl->out_buf;
2771 }
2772 else
2773#endif
2774 {
2775 ssl->out_hdr = ssl->out_buf + 8;
2776 }
2777 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
2781 return( 0 );
2782}
2783
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002784/*
2785 * Functions to handle the DTLS retransmission state machine
2786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002788/*
2789 * Append current handshake message to current outgoing flight
2790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002792{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2795 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2796 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002797
2798 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002799 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002800 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002803 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002804 }
2805
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002806 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002807 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002810 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002811 }
2812
2813 /* Copy current handshake message with headers */
2814 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2815 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002816 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002817 msg->next = NULL;
2818
2819 /* Append to the current flight */
2820 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002821 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002822 else
2823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825 while( cur->next != NULL )
2826 cur = cur->next;
2827 cur->next = msg;
2828 }
2829
Hanno Becker3b235902018-08-06 09:54:53 +01002830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831 return( 0 );
2832}
2833
2834/*
2835 * Free the current flight of handshake messages
2836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 mbedtls_ssl_flight_item *cur = flight;
2840 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002841
2842 while( cur != NULL )
2843 {
2844 next = cur->next;
2845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 mbedtls_free( cur->p );
2847 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002848
2849 cur = next;
2850 }
2851}
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2854static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002855#endif
2856
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002857/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002858 * Swap transform_out and out_ctr with the alternative ones
2859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002863 unsigned char tmp_out_ctr[8];
2864
2865 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868 return;
2869 }
2870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002872
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002873 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002874 tmp_transform = ssl->transform_out;
2875 ssl->transform_out = ssl->handshake->alt_transform_out;
2876 ssl->handshake->alt_transform_out = tmp_transform;
2877
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002878 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002879 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2880 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002881 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002882
2883 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002884 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2887 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2892 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002893 }
2894 }
2895#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002896}
2897
2898/*
2899 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002900 */
2901int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2902{
2903 int ret = 0;
2904
2905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2906
2907 ret = mbedtls_ssl_flight_transmit( ssl );
2908
2909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2910
2911 return( ret );
2912}
2913
2914/*
2915 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002916 *
2917 * Need to remember the current message in case flush_output returns
2918 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002919 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002920 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002921int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002922{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002923 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002927 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002929
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002930 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002931 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002932 ssl_swap_epochs( ssl );
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002935 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002936
2937 while( ssl->handshake->cur_msg != NULL )
2938 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002939 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002940 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002941
Hanno Beckere1dcb032018-08-17 16:47:58 +01002942 int const is_finished =
2943 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2944 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2945
Hanno Becker04da1892018-08-14 13:22:10 +01002946 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2947 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2948
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002949 /* Swap epochs before sending Finished: we can't do it after
2950 * sending ChangeCipherSpec, in case write returns WANT_READ.
2951 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002952 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002953 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002955 ssl_swap_epochs( ssl );
2956 }
2957
Hanno Becker67bc7c32018-08-06 11:33:50 +01002958 ret = ssl_get_remaining_payload_in_datagram( ssl );
2959 if( ret < 0 )
2960 return( ret );
2961 max_frag_len = (size_t) ret;
2962
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002963 /* CCS is copied as is, while HS messages may need fragmentation */
2964 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2965 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002966 if( max_frag_len == 0 )
2967 {
2968 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2969 return( ret );
2970
2971 continue;
2972 }
2973
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002974 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002975 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002976 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002977
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002978 /* Update position inside current message */
2979 ssl->handshake->cur_msg_p += cur->len;
2980 }
2981 else
2982 {
2983 const unsigned char * const p = ssl->handshake->cur_msg_p;
2984 const size_t hs_len = cur->len - 12;
2985 const size_t frag_off = p - ( cur->p + 12 );
2986 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002987 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002988
Hanno Beckere1dcb032018-08-17 16:47:58 +01002989 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002990 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002991 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002992 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002993
2994 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2995 return( ret );
2996
2997 continue;
2998 }
2999 max_hs_frag_len = max_frag_len - 12;
3000
3001 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3002 max_hs_frag_len : rem_len;
3003
3004 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003005 {
3006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003007 (unsigned) cur_hs_frag_len,
3008 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003009 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003010
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003011 /* Messages are stored with handshake headers as if not fragmented,
3012 * copy beginning of headers then fill fragmentation fields.
3013 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3014 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003015
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003016 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3017 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3018 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3019
Hanno Becker67bc7c32018-08-06 11:33:50 +01003020 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3021 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3022 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003023
3024 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3025
Hanno Becker67bc7c32018-08-06 11:33:50 +01003026 /* Copy the handshame message content and set records fields */
3027 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3028 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003029 ssl->out_msgtype = cur->type;
3030
3031 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003032 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003033 }
3034
3035 /* If done with the current message move to the next one if any */
3036 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3037 {
3038 if( cur->next != NULL )
3039 {
3040 ssl->handshake->cur_msg = cur->next;
3041 ssl->handshake->cur_msg_p = cur->next->p + 12;
3042 }
3043 else
3044 {
3045 ssl->handshake->cur_msg = NULL;
3046 ssl->handshake->cur_msg_p = NULL;
3047 }
3048 }
3049
3050 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003051 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003054 return( ret );
3055 }
3056 }
3057
Hanno Becker67bc7c32018-08-06 11:33:50 +01003058 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3059 return( ret );
3060
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003061 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3063 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003064 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003067 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3068 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003069
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003071
3072 return( 0 );
3073}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003074
3075/*
3076 * To be called when the last message of an incoming flight is received.
3077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003079{
3080 /* We won't need to resend that one any more */
3081 ssl_flight_free( ssl->handshake->flight );
3082 ssl->handshake->flight = NULL;
3083 ssl->handshake->cur_msg = NULL;
3084
3085 /* The next incoming flight will start with this msg_seq */
3086 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3087
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003088 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003089 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003090
Hanno Becker0271f962018-08-16 13:23:47 +01003091 /* Clear future message buffering structure. */
3092 ssl_buffering_free( ssl );
3093
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003094 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003095 ssl_set_timer( ssl, 0 );
3096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3098 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003101 }
3102 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003104}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003105
3106/*
3107 * To be called when the last message of an outgoing flight is send.
3108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003110{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003111 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003112 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3115 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003118 }
3119 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003123
Paul Bakker5121ce52009-01-03 21:22:43 +00003124/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003125 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003127
3128/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003129 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003130 *
3131 * - fill in handshake headers
3132 * - update handshake checksum
3133 * - DTLS: save message for resending
3134 * - then pass to the record layer
3135 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003136 * DTLS: except for HelloRequest, messages are only queued, and will only be
3137 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003138 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003139 * Inputs:
3140 * - ssl->out_msglen: 4 + actual handshake message len
3141 * (4 is the size of handshake headers for TLS)
3142 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3143 * - ssl->out_msg + 4: the handshake message body
3144 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003145 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003146 * - ssl->out_msglen: the length of the record contents
3147 * (including handshake headers but excluding record headers)
3148 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003149 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003150int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003151{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003152 int ret;
3153 const size_t hs_len = ssl->out_msglen - 4;
3154 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3157
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003158 /*
3159 * Sanity checks
3160 */
Hanno Becker2c98db22018-08-22 16:05:47 +01003161 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003162 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3163 {
Hanno Becker2c98db22018-08-22 16:05:47 +01003164 /* In SSLv3, the client might send a NoCertificate alert. */
3165#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3166 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3167 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3168 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3169#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3170 {
3171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3173 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003176 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3177 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3178 ssl->handshake == NULL )
3179 {
3180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3182 }
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003186 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003188 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3190 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003191 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003192#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003193
Hanno Beckerb50a2532018-08-06 11:52:54 +01003194 /* Double-check that we did not exceed the bounds
3195 * of the outgoing record buffer.
3196 * This should never fail as the various message
3197 * writing functions must obey the bounds of the
3198 * outgoing record buffer, but better be safe.
3199 *
3200 * Note: We deliberately do not check for the MTU or MFL here.
3201 */
3202 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3203 {
3204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3205 "size %u, maximum %u",
3206 (unsigned) ssl->out_msglen,
3207 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3209 }
3210
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003211 /*
3212 * Fill handshake headers
3213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003216 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3217 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3218 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003220 /*
3221 * DTLS has additional fields in the Handshake layer,
3222 * between the length field and the actual payload:
3223 * uint16 message_seq;
3224 * uint24 fragment_offset;
3225 * uint24 fragment_length;
3226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003228 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003229 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003230 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003231 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003232 {
3233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3234 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003235 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003236 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3238 }
3239
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003240 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003241 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003242
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003243 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003244 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003245 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003246 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3247 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3248 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003249 }
3250 else
3251 {
3252 ssl->out_msg[4] = 0;
3253 ssl->out_msg[5] = 0;
3254 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003255
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003256 /* Handshake hashes are computed without fragmentation,
3257 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003258 memset( ssl->out_msg + 6, 0x00, 3 );
3259 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003262
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003263 /* Update running hashes of hanshake messages seen */
3264 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3265 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 }
3267
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003268 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003270 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker551835d2018-08-22 16:07:59 +01003271 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3272 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003273 {
3274 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003277 return( ret );
3278 }
3279 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003280 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003281#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003282 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003283 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003284 {
3285 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3286 return( ret );
3287 }
3288 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003289
3290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3291
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003292 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003293}
3294
3295/*
3296 * Record layer functions
3297 */
3298
3299/*
3300 * Write current record.
3301 *
3302 * Uses:
3303 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3304 * - ssl->out_msglen: length of the record content (excl headers)
3305 * - ssl->out_msg: record content
3306 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003307int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003308{
3309 int ret, done = 0;
3310 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003311 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003312
3313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003316 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003318 {
3319 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003322 return( ret );
3323 }
3324
3325 len = ssl->out_msglen;
3326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3330 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 ret = mbedtls_ssl_hw_record_write( ssl );
3335 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3338 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003339 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003340
3341 if( ret == 0 )
3342 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003345 if( !done )
3346 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003347 unsigned i;
3348 size_t protected_record_size;
3349
Paul Bakker05ef8352012-05-08 09:17:57 +00003350 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003352 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003353
Hanno Becker19859472018-08-06 09:40:20 +01003354 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003355 ssl->out_len[0] = (unsigned char)( len >> 8 );
3356 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003357
Paul Bakker48916f92012-09-16 19:57:18 +00003358 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003359 {
3360 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003363 return( ret );
3364 }
3365
3366 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003367 ssl->out_len[0] = (unsigned char)( len >> 8 );
3368 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003369 }
3370
Hanno Becker2b1e3542018-08-06 11:19:13 +01003371 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3372
3373#if defined(MBEDTLS_SSL_PROTO_DTLS)
3374 /* In case of DTLS, double-check that we don't exceed
3375 * the remaining space in the datagram. */
3376 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3377 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003378 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003379 if( ret < 0 )
3380 return( ret );
3381
3382 if( protected_record_size > (size_t) ret )
3383 {
3384 /* Should never happen */
3385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3386 }
3387 }
3388#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003391 "version = [%d:%d], msglen = %d",
3392 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3393
Paul Bakker05ef8352012-05-08 09:17:57 +00003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003396 ssl->out_hdr, protected_record_size );
3397
3398 ssl->out_left += protected_record_size;
3399 ssl->out_hdr += protected_record_size;
3400 ssl_update_out_pointers( ssl, ssl->transform_out );
3401
Hanno Becker04484622018-08-06 09:49:38 +01003402 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3403 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3404 break;
3405
3406 /* The loop goes to its end iff the counter is wrapping */
3407 if( i == ssl_ep_len( ssl ) )
3408 {
3409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3410 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 }
3413
Hanno Becker67bc7c32018-08-06 11:33:50 +01003414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3416 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003417 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003418 size_t remaining;
3419 ret = ssl_get_remaining_payload_in_datagram( ssl );
3420 if( ret < 0 )
3421 {
3422 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3423 ret );
3424 return( ret );
3425 }
3426
3427 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003428 if( remaining == 0 )
3429 flush = SSL_FORCE_FLUSH;
3430 else
3431 {
Hanno Becker513815a2018-08-20 11:56:09 +01003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003433 }
3434 }
3435#endif /* MBEDTLS_SSL_PROTO_DTLS */
3436
3437 if( ( flush == SSL_FORCE_FLUSH ) &&
3438 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 return( ret );
3442 }
3443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003445
3446 return( 0 );
3447}
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003450
3451static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3452{
3453 if( ssl->in_msglen < ssl->in_hslen ||
3454 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3455 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3456 {
3457 return( 1 );
3458 }
3459 return( 0 );
3460}
Hanno Becker44650b72018-08-16 12:51:11 +01003461
3462static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3463{
3464 return( ( ssl->in_msg[9] << 16 ) |
3465 ( ssl->in_msg[10] << 8 ) |
3466 ssl->in_msg[11] );
3467}
3468
3469static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3470{
3471 return( ( ssl->in_msg[6] << 16 ) |
3472 ( ssl->in_msg[7] << 8 ) |
3473 ssl->in_msg[8] );
3474}
3475
3476static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3477{
3478 uint32_t msg_len, frag_off, frag_len;
3479
3480 msg_len = ssl_get_hs_total_len( ssl );
3481 frag_off = ssl_get_hs_frag_off( ssl );
3482 frag_len = ssl_get_hs_frag_len( ssl );
3483
3484 if( frag_off > msg_len )
3485 return( -1 );
3486
3487 if( frag_len > msg_len - frag_off )
3488 return( -1 );
3489
3490 if( frag_len + 12 > ssl->in_msglen )
3491 return( -1 );
3492
3493 return( 0 );
3494}
3495
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003496/*
3497 * Mark bits in bitmask (used for DTLS HS reassembly)
3498 */
3499static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3500{
3501 unsigned int start_bits, end_bits;
3502
3503 start_bits = 8 - ( offset % 8 );
3504 if( start_bits != 8 )
3505 {
3506 size_t first_byte_idx = offset / 8;
3507
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003508 /* Special case */
3509 if( len <= start_bits )
3510 {
3511 for( ; len != 0; len-- )
3512 mask[first_byte_idx] |= 1 << ( start_bits - len );
3513
3514 /* Avoid potential issues with offset or len becoming invalid */
3515 return;
3516 }
3517
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003518 offset += start_bits; /* Now offset % 8 == 0 */
3519 len -= start_bits;
3520
3521 for( ; start_bits != 0; start_bits-- )
3522 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3523 }
3524
3525 end_bits = len % 8;
3526 if( end_bits != 0 )
3527 {
3528 size_t last_byte_idx = ( offset + len ) / 8;
3529
3530 len -= end_bits; /* Now len % 8 == 0 */
3531
3532 for( ; end_bits != 0; end_bits-- )
3533 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3534 }
3535
3536 memset( mask + offset / 8, 0xFF, len / 8 );
3537}
3538
3539/*
3540 * Check that bitmask is full
3541 */
3542static int ssl_bitmask_check( unsigned char *mask, size_t len )
3543{
3544 size_t i;
3545
3546 for( i = 0; i < len / 8; i++ )
3547 if( mask[i] != 0xFF )
3548 return( -1 );
3549
3550 for( i = 0; i < len % 8; i++ )
3551 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3552 return( -1 );
3553
3554 return( 0 );
3555}
3556
Hanno Becker56e205e2018-08-16 09:06:12 +01003557/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003558static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003559 unsigned add_bitmap )
Hanno Becker56e205e2018-08-16 09:06:12 +01003560{
3561 size_t alloc_len;
Hanno Becker56e205e2018-08-16 09:06:12 +01003562
3563 alloc_len = 12; /* Handshake header */
3564 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003565
3566 if( add_bitmap )
3567 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003568
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003569 return( alloc_len );
Hanno Becker56e205e2018-08-16 09:06:12 +01003570}
3571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003573
Hanno Becker12555c62018-08-16 12:47:53 +01003574static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3575{
3576 return( ( ssl->in_msg[1] << 16 ) |
3577 ( ssl->in_msg[2] << 8 ) |
3578 ssl->in_msg[3] );
3579}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003580
Simon Butcher99000142016-10-13 17:21:01 +01003581int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003582{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003586 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003588 }
3589
Hanno Becker12555c62018-08-16 12:47:53 +01003590 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003593 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003594 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003598 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003599 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003600 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003601
Hanno Becker44650b72018-08-16 12:51:11 +01003602 if( ssl_check_hs_header( ssl ) != 0 )
3603 {
3604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3605 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3606 }
3607
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003608 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003609 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3610 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3611 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3612 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003613 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003614 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3615 {
3616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3617 recv_msg_seq,
3618 ssl->handshake->in_msg_seq ) );
3619 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3620 }
3621
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003622 /* Retransmit only on last message from previous flight, to avoid
3623 * too many retransmissions.
3624 * Besides, No sane server ever retransmits HelloVerifyRequest */
3625 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003629 "message_seq = %d, start_of_flight = %d",
3630 recv_msg_seq,
3631 ssl->handshake->in_flight_start_seq ) );
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003636 return( ret );
3637 }
3638 }
3639 else
3640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003642 "message_seq = %d, expected = %d",
3643 recv_msg_seq,
3644 ssl->handshake->in_msg_seq ) );
3645 }
3646
Hanno Becker90333da2017-10-10 11:27:13 +01003647 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003648 }
3649 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003650
Hanno Becker6d97ef52018-08-16 13:09:04 +01003651 /* Message reassembly is handled alongside buffering of future
3652 * messages; the commonality is that both handshake fragments and
3653 * future messages cannot be forwarded immediately to the handshake
3654 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003655 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003658 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003659 }
3660 }
3661 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003663 /* With TLS we don't handle fragmentation (for now) */
3664 if( ssl->in_msglen < ssl->in_hslen )
3665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3667 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003668 }
3669
Simon Butcher99000142016-10-13 17:21:01 +01003670 return( 0 );
3671}
3672
3673void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3674{
Hanno Becker0271f962018-08-16 13:23:47 +01003675 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003676
Hanno Becker0271f962018-08-16 13:23:47 +01003677 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003678 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003679 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003680 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003681
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003682 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003685 ssl->handshake != NULL )
3686 {
Hanno Becker0271f962018-08-16 13:23:47 +01003687 unsigned offset;
3688 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003689
Hanno Becker0271f962018-08-16 13:23:47 +01003690 /* Increment handshake sequence number */
3691 hs->in_msg_seq++;
3692
3693 /*
3694 * Clear up handshake buffering and reassembly structure.
3695 */
3696
3697 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003698 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003699
3700 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003701 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3702 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003703 offset++, hs_buf++ )
3704 {
3705 *hs_buf = *(hs_buf + 1);
3706 }
3707
3708 /* Create a fresh last entry */
3709 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003710 }
3711#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003712}
3713
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003714/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003715 * DTLS anti-replay: RFC 6347 4.1.2.6
3716 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003717 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3718 * Bit n is set iff record number in_window_top - n has been seen.
3719 *
3720 * Usually, in_window_top is the last record number seen and the lsb of
3721 * in_window is set. The only exception is the initial state (record number 0
3722 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3725static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003726{
3727 ssl->in_window_top = 0;
3728 ssl->in_window = 0;
3729}
3730
3731static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3732{
3733 return( ( (uint64_t) buf[0] << 40 ) |
3734 ( (uint64_t) buf[1] << 32 ) |
3735 ( (uint64_t) buf[2] << 24 ) |
3736 ( (uint64_t) buf[3] << 16 ) |
3737 ( (uint64_t) buf[4] << 8 ) |
3738 ( (uint64_t) buf[5] ) );
3739}
3740
3741/*
3742 * Return 0 if sequence number is acceptable, -1 otherwise
3743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003745{
3746 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3747 uint64_t bit;
3748
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003749 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003750 return( 0 );
3751
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003752 if( rec_seqnum > ssl->in_window_top )
3753 return( 0 );
3754
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003755 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003756
3757 if( bit >= 64 )
3758 return( -1 );
3759
3760 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3761 return( -1 );
3762
3763 return( 0 );
3764}
3765
3766/*
3767 * Update replay window on new validated record
3768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003770{
3771 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3772
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003773 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003774 return;
3775
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003776 if( rec_seqnum > ssl->in_window_top )
3777 {
3778 /* Update window_top and the contents of the window */
3779 uint64_t shift = rec_seqnum - ssl->in_window_top;
3780
3781 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003782 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003783 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003784 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003785 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003786 ssl->in_window |= 1;
3787 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003788
3789 ssl->in_window_top = rec_seqnum;
3790 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003791 else
3792 {
3793 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003794 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003795
3796 if( bit < 64 ) /* Always true, but be extra sure */
3797 ssl->in_window |= (uint64_t) 1 << bit;
3798 }
3799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003801
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003802#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003803/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003804static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3805
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003806/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003807 * Without any SSL context, check if a datagram looks like a ClientHello with
3808 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003809 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003810 *
3811 * - if cookie is valid, return 0
3812 * - if ClientHello looks superficially valid but cookie is not,
3813 * fill obuf and set olen, then
3814 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3815 * - otherwise return a specific error code
3816 */
3817static int ssl_check_dtls_clihlo_cookie(
3818 mbedtls_ssl_cookie_write_t *f_cookie_write,
3819 mbedtls_ssl_cookie_check_t *f_cookie_check,
3820 void *p_cookie,
3821 const unsigned char *cli_id, size_t cli_id_len,
3822 const unsigned char *in, size_t in_len,
3823 unsigned char *obuf, size_t buf_len, size_t *olen )
3824{
3825 size_t sid_len, cookie_len;
3826 unsigned char *p;
3827
3828 if( f_cookie_write == NULL || f_cookie_check == NULL )
3829 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3830
3831 /*
3832 * Structure of ClientHello with record and handshake headers,
3833 * and expected values. We don't need to check a lot, more checks will be
3834 * done when actually parsing the ClientHello - skipping those checks
3835 * avoids code duplication and does not make cookie forging any easier.
3836 *
3837 * 0-0 ContentType type; copied, must be handshake
3838 * 1-2 ProtocolVersion version; copied
3839 * 3-4 uint16 epoch; copied, must be 0
3840 * 5-10 uint48 sequence_number; copied
3841 * 11-12 uint16 length; (ignored)
3842 *
3843 * 13-13 HandshakeType msg_type; (ignored)
3844 * 14-16 uint24 length; (ignored)
3845 * 17-18 uint16 message_seq; copied
3846 * 19-21 uint24 fragment_offset; copied, must be 0
3847 * 22-24 uint24 fragment_length; (ignored)
3848 *
3849 * 25-26 ProtocolVersion client_version; (ignored)
3850 * 27-58 Random random; (ignored)
3851 * 59-xx SessionID session_id; 1 byte len + sid_len content
3852 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3853 * ...
3854 *
3855 * Minimum length is 61 bytes.
3856 */
3857 if( in_len < 61 ||
3858 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3859 in[3] != 0 || in[4] != 0 ||
3860 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3861 {
3862 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3863 }
3864
3865 sid_len = in[59];
3866 if( sid_len > in_len - 61 )
3867 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3868
3869 cookie_len = in[60 + sid_len];
3870 if( cookie_len > in_len - 60 )
3871 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3872
3873 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3874 cli_id, cli_id_len ) == 0 )
3875 {
3876 /* Valid cookie */
3877 return( 0 );
3878 }
3879
3880 /*
3881 * If we get here, we've got an invalid cookie, let's prepare HVR.
3882 *
3883 * 0-0 ContentType type; copied
3884 * 1-2 ProtocolVersion version; copied
3885 * 3-4 uint16 epoch; copied
3886 * 5-10 uint48 sequence_number; copied
3887 * 11-12 uint16 length; olen - 13
3888 *
3889 * 13-13 HandshakeType msg_type; hello_verify_request
3890 * 14-16 uint24 length; olen - 25
3891 * 17-18 uint16 message_seq; copied
3892 * 19-21 uint24 fragment_offset; copied
3893 * 22-24 uint24 fragment_length; olen - 25
3894 *
3895 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3896 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3897 *
3898 * Minimum length is 28.
3899 */
3900 if( buf_len < 28 )
3901 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3902
3903 /* Copy most fields and adapt others */
3904 memcpy( obuf, in, 25 );
3905 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3906 obuf[25] = 0xfe;
3907 obuf[26] = 0xff;
3908
3909 /* Generate and write actual cookie */
3910 p = obuf + 28;
3911 if( f_cookie_write( p_cookie,
3912 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3913 {
3914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3915 }
3916
3917 *olen = p - obuf;
3918
3919 /* Go back and fill length fields */
3920 obuf[27] = (unsigned char)( *olen - 28 );
3921
3922 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3923 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3924 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3925
3926 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3927 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3928
3929 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3930}
3931
3932/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003933 * Handle possible client reconnect with the same UDP quadruplet
3934 * (RFC 6347 Section 4.2.8).
3935 *
3936 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3937 * that looks like a ClientHello.
3938 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003939 * - if the input looks like a ClientHello without cookies,
3940 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003941 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003942 * - if the input looks like a ClientHello with a valid cookie,
3943 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003944 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003945 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003946 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003947 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003948 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3949 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003950 */
3951static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3952{
3953 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003954 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003955
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003956 ret = ssl_check_dtls_clihlo_cookie(
3957 ssl->conf->f_cookie_write,
3958 ssl->conf->f_cookie_check,
3959 ssl->conf->p_cookie,
3960 ssl->cli_id, ssl->cli_id_len,
3961 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003962 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003963
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003964 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3965
3966 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003967 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003968 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003969 * If the error is permanent we'll catch it later,
3970 * if it's not, then hopefully it'll work next time. */
3971 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3972
3973 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003974 }
3975
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003976 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003977 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003978 /* Got a valid cookie, partially reset context */
3979 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3980 {
3981 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3982 return( ret );
3983 }
3984
3985 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003986 }
3987
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003988 return( ret );
3989}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003990#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003991
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003992/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003993 * ContentType type;
3994 * ProtocolVersion version;
3995 * uint16 epoch; // DTLS only
3996 * uint48 sequence_number; // DTLS only
3997 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003998 *
3999 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004000 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004001 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4002 *
4003 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004004 * 1. proceed with the record if this function returns 0
4005 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4006 * 3. return CLIENT_RECONNECT if this function return that value
4007 * 4. drop the whole datagram if this function returns anything else.
4008 * Point 2 is needed when the peer is resending, and we have already received
4009 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004012{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004013 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 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 +02004016
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004018 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004019 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004023 ssl->in_msgtype,
4024 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004025
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004026 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4028 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4029 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4030 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004033
4034#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004035 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4036 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004037 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4038#endif /* MBEDTLS_SSL_PROTO_DTLS */
4039 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4040 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004043 }
4044
4045 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004046 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4049 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 }
4051
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004052 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4055 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 }
4057
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004058 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004059 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004060 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4063 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004064 }
4065
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004066 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004067 * DTLS-related tests.
4068 * Check epoch before checking length constraint because
4069 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4070 * message gets duplicated before the corresponding Finished message,
4071 * the second ChangeCipherSpec should be discarded because it belongs
4072 * to an old epoch, but not because its length is shorter than
4073 * the minimum record length for packets using the new record transform.
4074 * Note that these two kinds of failures are handled differently,
4075 * as an unexpected record is silently skipped but an invalid
4076 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004077 */
4078#if defined(MBEDTLS_SSL_PROTO_DTLS)
4079 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4080 {
4081 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4082
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004083 /* Check epoch (and sequence number) with DTLS */
4084 if( rec_epoch != ssl->in_epoch )
4085 {
4086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4087 "expected %d, received %d",
4088 ssl->in_epoch, rec_epoch ) );
4089
4090#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4091 /*
4092 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4093 * access the first byte of record content (handshake type), as we
4094 * have an active transform (possibly iv_len != 0), so use the
4095 * fact that the record header len is 13 instead.
4096 */
4097 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4098 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4099 rec_epoch == 0 &&
4100 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4101 ssl->in_left > 13 &&
4102 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4103 {
4104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4105 "from the same port" ) );
4106 return( ssl_handle_possible_reconnect( ssl ) );
4107 }
4108 else
4109#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004110 {
4111 /* Consider buffering the record. */
4112 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4113 {
4114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4115 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4116 }
4117
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004118 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004119 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004120 }
4121
4122#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4123 /* Replay detection only works for the current epoch */
4124 if( rec_epoch == ssl->in_epoch &&
4125 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4126 {
4127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4128 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4129 }
4130#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004131
Hanno Becker52c6dc62017-05-26 16:07:36 +01004132 /* Drop unexpected ApplicationData records,
4133 * except at the beginning of renegotiations */
4134 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4135 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4136#if defined(MBEDTLS_SSL_RENEGOTIATION)
4137 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4138 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4139#endif
4140 )
4141 {
4142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4143 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4144 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004145 }
4146#endif /* MBEDTLS_SSL_PROTO_DTLS */
4147
Hanno Becker52c6dc62017-05-26 16:07:36 +01004148
4149 /* Check length against bounds of the current transform and version */
4150 if( ssl->transform_in == NULL )
4151 {
4152 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004153 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004154 {
4155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4156 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4157 }
4158 }
4159 else
4160 {
4161 if( ssl->in_msglen < ssl->transform_in->minlen )
4162 {
4163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4164 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4165 }
4166
4167#if defined(MBEDTLS_SSL_PROTO_SSL3)
4168 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004169 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004170 {
4171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4172 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4173 }
4174#endif
4175#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4176 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4177 /*
4178 * TLS encrypted messages can have up to 256 bytes of padding
4179 */
4180 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4181 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004182 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004183 {
4184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4185 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4186 }
4187#endif
4188 }
4189
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004190 return( 0 );
4191}
Paul Bakker5121ce52009-01-03 21:22:43 +00004192
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004193/*
4194 * If applicable, decrypt (and decompress) record content
4195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004197{
4198 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4201 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4204 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 ret = mbedtls_ssl_hw_record_read( ssl );
4209 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4212 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004213 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004214
4215 if( ret == 0 )
4216 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004219 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004220 {
4221 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004224 return( ret );
4225 }
4226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004228 ssl->in_msg, ssl->in_msglen );
4229
Angus Grattond8213d02016-05-25 20:56:48 +10004230 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4233 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 }
4235 }
4236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004238 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004240 {
4241 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004244 return( ret );
4245 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004246 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004253 }
4254#endif
4255
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004256 return( 0 );
4257}
4258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004260
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004261/*
4262 * Read a record.
4263 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004264 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4265 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4266 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004267 */
Hanno Becker1097b342018-08-15 14:09:41 +01004268
4269/* Helper functions for mbedtls_ssl_read_record(). */
4270static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004271static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4272static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004273
Hanno Becker40f50842018-08-15 14:48:01 +01004274#if defined(MBEDTLS_SSL_PROTO_DTLS)
4275static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004276static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004277static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004278static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004279static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4280#endif /* MBEDTLS_SSL_PROTO_DTLS */
4281
Hanno Becker327c93b2018-08-15 13:56:18 +01004282int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004283 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284{
4285 int ret;
4286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004288
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004289 if( ssl->keep_current_message == 0 )
4290 {
4291 do {
Simon Butcher99000142016-10-13 17:21:01 +01004292
Hanno Becker26994592018-08-15 14:14:59 +01004293 ret = ssl_consume_current_message( ssl );
4294 if( ret != 0 )
4295 return( ret );
4296
Hanno Beckere74d5562018-08-15 14:26:08 +01004297 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004298 {
Hanno Becker40f50842018-08-15 14:48:01 +01004299#if defined(MBEDTLS_SSL_PROTO_DTLS)
4300 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004301
Hanno Becker40f50842018-08-15 14:48:01 +01004302 /* We only check for buffered messages if the
4303 * current datagram is fully consumed. */
4304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4305 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004306 {
Hanno Becker40f50842018-08-15 14:48:01 +01004307 if( ssl_load_buffered_message( ssl ) == 0 )
4308 have_buffered = 1;
4309 }
4310
4311 if( have_buffered == 0 )
4312#endif /* MBEDTLS_SSL_PROTO_DTLS */
4313 {
4314 ret = ssl_get_next_record( ssl );
4315 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4316 continue;
4317
4318 if( ret != 0 )
4319 {
4320 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4321 return( ret );
4322 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004323 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004324 }
4325
4326 ret = mbedtls_ssl_handle_message_type( ssl );
4327
Hanno Becker40f50842018-08-15 14:48:01 +01004328#if defined(MBEDTLS_SSL_PROTO_DTLS)
4329 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4330 {
4331 /* Buffer future message */
4332 ret = ssl_buffer_message( ssl );
4333 if( ret != 0 )
4334 return( ret );
4335
4336 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4337 }
4338#endif /* MBEDTLS_SSL_PROTO_DTLS */
4339
Hanno Becker90333da2017-10-10 11:27:13 +01004340 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4341 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004342
4343 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004344 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004345 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004346 return( ret );
4347 }
4348
Hanno Becker327c93b2018-08-15 13:56:18 +01004349 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004350 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004351 {
4352 mbedtls_ssl_update_handshake_status( ssl );
4353 }
Simon Butcher99000142016-10-13 17:21:01 +01004354 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004355 else
Simon Butcher99000142016-10-13 17:21:01 +01004356 {
Hanno Becker02f59072018-08-15 14:00:24 +01004357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004358 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004359 }
4360
4361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4362
4363 return( 0 );
4364}
4365
Hanno Becker40f50842018-08-15 14:48:01 +01004366#if defined(MBEDTLS_SSL_PROTO_DTLS)
4367static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4368{
4369 if( ssl->in_left > ssl->next_record_offset )
4370 return( 1 );
4371
4372 return( 0 );
4373}
4374
4375static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4376{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004377 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004378 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004379 int ret = 0;
4380
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004381 if( hs == NULL )
4382 return( -1 );
4383
Hanno Beckere00ae372018-08-20 09:39:42 +01004384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4385
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004386 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4387 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4388 {
4389 /* Check if we have seen a ChangeCipherSpec before.
4390 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004391 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004392 {
4393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4394 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004395 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004396 }
4397
4398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4399 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4400 ssl->in_msglen = 1;
4401 ssl->in_msg[0] = 1;
4402
4403 /* As long as they are equal, the exact value doesn't matter. */
4404 ssl->in_left = 0;
4405 ssl->next_record_offset = 0;
4406
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004407 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004408 goto exit;
4409 }
Hanno Becker37f95322018-08-16 13:55:32 +01004410
4411 /* Debug only */
4412 {
4413 unsigned offset;
4414 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4415 {
4416 hs_buf = &hs->buffering.hs[offset];
4417 if( hs_buf->is_valid == 1 )
4418 {
4419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4420 hs->in_msg_seq + offset,
4421 hs_buf->is_complete ? "fully" : "partitially" ) );
4422 }
4423 }
4424 }
4425
4426 /* Check if we have buffered and/or fully reassembled the
4427 * next handshake message. */
4428 hs_buf = &hs->buffering.hs[0];
4429 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4430 {
4431 /* Synthesize a record containing the buffered HS message. */
4432 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4433 ( hs_buf->data[2] << 8 ) |
4434 hs_buf->data[3];
4435
4436 /* Double-check that we haven't accidentally buffered
4437 * a message that doesn't fit into the input buffer. */
4438 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4442 }
4443
4444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4445 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4446 hs_buf->data, msg_len + 12 );
4447
4448 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4449 ssl->in_hslen = msg_len + 12;
4450 ssl->in_msglen = msg_len + 12;
4451 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4452
4453 ret = 0;
4454 goto exit;
4455 }
4456 else
4457 {
4458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4459 hs->in_msg_seq ) );
4460 }
4461
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004462 ret = -1;
4463
4464exit:
4465
4466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4467 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004468}
4469
Hanno Becker01315ea2018-08-21 17:22:17 +01004470static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004471static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4472 size_t desired )
4473{
4474 int offset;
4475 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4476
Hanno Becker01315ea2018-08-21 17:22:17 +01004477 /* Get rid of future records epoch first, if such exist. */
4478 ssl_free_buffered_record( ssl );
4479
4480 /* Check if we have enough space available now. */
4481 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4482 hs->buffering.total_bytes_buffered ) )
4483 {
4484 return( 0 );
4485 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004486
4487 /* We don't have enough space to buffer the next expected
4488 * handshake message. Remove buffers used for future msgs
4489 * to gain space, starting with the most distant one. */
4490 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4491 offset >= 0; offset-- )
4492 {
4493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4494 offset ) );
4495
Hanno Beckerb309b922018-08-23 13:18:05 +01004496 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004497
4498 /* Check if we have enough space available now. */
4499 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4500 hs->buffering.total_bytes_buffered ) )
4501 {
4502 return( 0 );
4503 }
4504 }
4505
4506 return( -1 );
4507}
4508
Hanno Becker40f50842018-08-15 14:48:01 +01004509static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4510{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004511 int ret = 0;
4512 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4513
4514 if( hs == NULL )
4515 return( 0 );
4516
4517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4518
4519 switch( ssl->in_msgtype )
4520 {
4521 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004523
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004524 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004525 break;
4526
4527 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004528 {
4529 unsigned recv_msg_seq_offset;
4530 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4531 mbedtls_ssl_hs_buffer *hs_buf;
4532 size_t msg_len = ssl->in_hslen - 12;
4533
4534 /* We should never receive an old handshake
4535 * message - double-check nonetheless. */
4536 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4537 {
4538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4540 }
4541
4542 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4543 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4544 {
4545 /* Silently ignore -- message too far in the future */
4546 MBEDTLS_SSL_DEBUG_MSG( 2,
4547 ( "Ignore future HS message with sequence number %u, "
4548 "buffering window %u - %u",
4549 recv_msg_seq, ssl->handshake->in_msg_seq,
4550 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4551
4552 goto exit;
4553 }
4554
4555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4556 recv_msg_seq, recv_msg_seq_offset ) );
4557
4558 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4559
4560 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004561 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004562 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004563 size_t reassembly_buf_sz;
4564
Hanno Becker37f95322018-08-16 13:55:32 +01004565 hs_buf->is_fragmented =
4566 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4567
4568 /* We copy the message back into the input buffer
4569 * after reassembly, so check that it's not too large.
4570 * This is an implementation-specific limitation
4571 * and not one from the standard, hence it is not
4572 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004573 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004574 {
4575 /* Ignore message */
4576 goto exit;
4577 }
4578
Hanno Beckere0b150f2018-08-21 15:51:03 +01004579 /* Check if we have enough space to buffer the message. */
4580 if( hs->buffering.total_bytes_buffered >
4581 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4582 {
4583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4585 }
4586
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004587 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4588 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004589
4590 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4591 hs->buffering.total_bytes_buffered ) )
4592 {
4593 if( recv_msg_seq_offset > 0 )
4594 {
4595 /* If we can't buffer a future message because
4596 * of space limitations -- ignore. */
4597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4598 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4599 (unsigned) hs->buffering.total_bytes_buffered ) );
4600 goto exit;
4601 }
Hanno Beckere1801392018-08-21 16:51:05 +01004602 else
4603 {
4604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4605 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4606 (unsigned) hs->buffering.total_bytes_buffered ) );
4607 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004608
Hanno Beckera02b0b42018-08-21 17:20:27 +01004609 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004610 {
4611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
Hanno Beckere0b150f2018-08-21 15:51:03 +01004612 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4613 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004614 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4615 goto exit;
4616 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004617 }
4618
4619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4620 msg_len ) );
4621
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004622 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4623 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004624 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004625 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004626 goto exit;
4627 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004628 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004629
4630 /* Prepare final header: copy msg_type, length and message_seq,
4631 * then add standardised fragment_offset and fragment_length */
4632 memcpy( hs_buf->data, ssl->in_msg, 6 );
4633 memset( hs_buf->data + 6, 0, 3 );
4634 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4635
4636 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004637
4638 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004639 }
4640 else
4641 {
4642 /* Make sure msg_type and length are consistent */
4643 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4644 {
4645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4646 /* Ignore */
4647 goto exit;
4648 }
4649 }
4650
Hanno Becker4422bbb2018-08-20 09:40:19 +01004651 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004652 {
4653 size_t frag_len, frag_off;
4654 unsigned char * const msg = hs_buf->data + 12;
4655
4656 /*
4657 * Check and copy current fragment
4658 */
4659
4660 /* Validation of header fields already done in
4661 * mbedtls_ssl_prepare_handshake_record(). */
4662 frag_off = ssl_get_hs_frag_off( ssl );
4663 frag_len = ssl_get_hs_frag_len( ssl );
4664
4665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4666 frag_off, frag_len ) );
4667 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4668
4669 if( hs_buf->is_fragmented )
4670 {
4671 unsigned char * const bitmask = msg + msg_len;
4672 ssl_bitmask_set( bitmask, frag_off, frag_len );
4673 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4674 msg_len ) == 0 );
4675 }
4676 else
4677 {
4678 hs_buf->is_complete = 1;
4679 }
4680
4681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4682 hs_buf->is_complete ? "" : "not yet " ) );
4683 }
4684
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004685 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004686 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004687
4688 default:
4689 break;
4690 }
4691
4692exit:
4693
4694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4695 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004696}
4697#endif /* MBEDTLS_SSL_PROTO_DTLS */
4698
Hanno Becker1097b342018-08-15 14:09:41 +01004699static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004700{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004701 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004702 * Consume last content-layer message and potentially
4703 * update in_msglen which keeps track of the contents'
4704 * consumption state.
4705 *
4706 * (1) Handshake messages:
4707 * Remove last handshake message, move content
4708 * and adapt in_msglen.
4709 *
4710 * (2) Alert messages:
4711 * Consume whole record content, in_msglen = 0.
4712 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004713 * (3) Change cipher spec:
4714 * Consume whole record content, in_msglen = 0.
4715 *
4716 * (4) Application data:
4717 * Don't do anything - the record layer provides
4718 * the application data as a stream transport
4719 * and consumes through mbedtls_ssl_read only.
4720 *
4721 */
4722
4723 /* Case (1): Handshake messages */
4724 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004725 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004726 /* Hard assertion to be sure that no application data
4727 * is in flight, as corrupting ssl->in_msglen during
4728 * ssl->in_offt != NULL is fatal. */
4729 if( ssl->in_offt != NULL )
4730 {
4731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4733 }
4734
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004735 /*
4736 * Get next Handshake message in the current record
4737 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004738
Hanno Becker4a810fb2017-05-24 16:27:30 +01004739 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004740 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004741 * current handshake content: If DTLS handshake
4742 * fragmentation is used, that's the fragment
4743 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004744 * size here is faulty and should be changed at
4745 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004746 * (2) While it doesn't seem to cause problems, one
4747 * has to be very careful not to assume that in_hslen
4748 * is always <= in_msglen in a sensible communication.
4749 * Again, it's wrong for DTLS handshake fragmentation.
4750 * The following check is therefore mandatory, and
4751 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004752 * Additionally, ssl->in_hslen might be arbitrarily out of
4753 * bounds after handling a DTLS message with an unexpected
4754 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004755 */
4756 if( ssl->in_hslen < ssl->in_msglen )
4757 {
4758 ssl->in_msglen -= ssl->in_hslen;
4759 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4760 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004761
Hanno Becker4a810fb2017-05-24 16:27:30 +01004762 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4763 ssl->in_msg, ssl->in_msglen );
4764 }
4765 else
4766 {
4767 ssl->in_msglen = 0;
4768 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004769
Hanno Becker4a810fb2017-05-24 16:27:30 +01004770 ssl->in_hslen = 0;
4771 }
4772 /* Case (4): Application data */
4773 else if( ssl->in_offt != NULL )
4774 {
4775 return( 0 );
4776 }
4777 /* Everything else (CCS & Alerts) */
4778 else
4779 {
4780 ssl->in_msglen = 0;
4781 }
4782
Hanno Becker1097b342018-08-15 14:09:41 +01004783 return( 0 );
4784}
4785
Hanno Beckere74d5562018-08-15 14:26:08 +01004786static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4787{
4788 if( ssl->in_msglen > 0 )
4789 return( 1 );
4790
4791 return( 0 );
4792}
4793
Hanno Becker5f066e72018-08-16 14:56:31 +01004794#if defined(MBEDTLS_SSL_PROTO_DTLS)
4795
4796static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4797{
4798 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4799 if( hs == NULL )
4800 return;
4801
Hanno Becker01315ea2018-08-21 17:22:17 +01004802 if( hs->buffering.future_record.data != NULL )
4803 {
4804 hs->buffering.total_bytes_buffered -=
4805 hs->buffering.future_record.len;
4806
4807 mbedtls_free( hs->buffering.future_record.data );
4808 hs->buffering.future_record.data = NULL;
4809 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004810}
4811
4812static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4813{
4814 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4815 unsigned char * rec;
4816 size_t rec_len;
4817 unsigned rec_epoch;
4818
4819 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4820 return( 0 );
4821
4822 if( hs == NULL )
4823 return( 0 );
4824
Hanno Becker5f066e72018-08-16 14:56:31 +01004825 rec = hs->buffering.future_record.data;
4826 rec_len = hs->buffering.future_record.len;
4827 rec_epoch = hs->buffering.future_record.epoch;
4828
4829 if( rec == NULL )
4830 return( 0 );
4831
Hanno Becker4cb782d2018-08-20 11:19:05 +01004832 /* Only consider loading future records if the
4833 * input buffer is empty. */
4834 if( ssl_another_record_in_datagram( ssl ) == 1 )
4835 return( 0 );
4836
Hanno Becker5f066e72018-08-16 14:56:31 +01004837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4838
4839 if( rec_epoch != ssl->in_epoch )
4840 {
4841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4842 goto exit;
4843 }
4844
4845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4846
4847 /* Double-check that the record is not too large */
4848 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4849 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4850 {
4851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4853 }
4854
4855 memcpy( ssl->in_hdr, rec, rec_len );
4856 ssl->in_left = rec_len;
4857 ssl->next_record_offset = 0;
4858
4859 ssl_free_buffered_record( ssl );
4860
4861exit:
4862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4863 return( 0 );
4864}
4865
4866static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4867{
4868 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4869 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004870 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004871
4872 /* Don't buffer future records outside handshakes. */
4873 if( hs == NULL )
4874 return( 0 );
4875
4876 /* Only buffer handshake records (we are only interested
4877 * in Finished messages). */
4878 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4879 return( 0 );
4880
4881 /* Don't buffer more than one future epoch record. */
4882 if( hs->buffering.future_record.data != NULL )
4883 return( 0 );
4884
Hanno Becker01315ea2018-08-21 17:22:17 +01004885 /* Don't buffer record if there's not enough buffering space remaining. */
4886 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4887 hs->buffering.total_bytes_buffered ) )
4888 {
4889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4890 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4891 (unsigned) hs->buffering.total_bytes_buffered ) );
4892 return( 0 );
4893 }
4894
Hanno Becker5f066e72018-08-16 14:56:31 +01004895 /* Buffer record */
4896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4897 ssl->in_epoch + 1 ) );
4898 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4899 rec_hdr_len + ssl->in_msglen );
4900
4901 /* ssl_parse_record_header() only considers records
4902 * of the next epoch as candidates for buffering. */
4903 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004904 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004905
4906 hs->buffering.future_record.data =
4907 mbedtls_calloc( 1, hs->buffering.future_record.len );
4908 if( hs->buffering.future_record.data == NULL )
4909 {
4910 /* If we run out of RAM trying to buffer a
4911 * record from the next epoch, just ignore. */
4912 return( 0 );
4913 }
4914
Hanno Becker01315ea2018-08-21 17:22:17 +01004915 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004916
Hanno Becker01315ea2018-08-21 17:22:17 +01004917 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004918 return( 0 );
4919}
4920
4921#endif /* MBEDTLS_SSL_PROTO_DTLS */
4922
Hanno Beckere74d5562018-08-15 14:26:08 +01004923static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004924{
4925 int ret;
4926
Hanno Becker5f066e72018-08-16 14:56:31 +01004927#if defined(MBEDTLS_SSL_PROTO_DTLS)
4928 /* We might have buffered a future record; if so,
4929 * and if the epoch matches now, load it.
4930 * On success, this call will set ssl->in_left to
4931 * the length of the buffered record, so that
4932 * the calls to ssl_fetch_input() below will
4933 * essentially be no-ops. */
4934 ret = ssl_load_buffered_record( ssl );
4935 if( ret != 0 )
4936 return( ret );
4937#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004942 return( ret );
4943 }
4944
4945 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004948 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4949 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004950 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004951 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4952 {
4953 ret = ssl_buffer_future_record( ssl );
4954 if( ret != 0 )
4955 return( ret );
4956
4957 /* Fall through to handling of unexpected records */
4958 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4959 }
4960
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004961 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4962 {
4963 /* Skip unexpected record (but not whole datagram) */
4964 ssl->next_record_offset = ssl->in_msglen
4965 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004966
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4968 "(header)" ) );
4969 }
4970 else
4971 {
4972 /* Skip invalid record and the rest of the datagram */
4973 ssl->next_record_offset = 0;
4974 ssl->in_left = 0;
4975
4976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4977 "(header)" ) );
4978 }
4979
4980 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004981 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004982 }
4983#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004984 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004985 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004986
4987 /*
4988 * Read and optionally decrypt the message contents
4989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4991 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004994 return( ret );
4995 }
4996
4997 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004999 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005002 if( ssl->next_record_offset < ssl->in_left )
5003 {
5004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5005 }
5006 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005007 else
5008#endif
5009 ssl->in_left = 0;
5010
5011 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005015 {
5016 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5018 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005019 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005020 /* Except when waiting for Finished as a bad mac here
5021 * probably means something went wrong in the handshake
5022 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5023 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5024 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5025 {
5026#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5027 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5028 {
5029 mbedtls_ssl_send_alert_message( ssl,
5030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5031 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5032 }
5033#endif
5034 return( ret );
5035 }
5036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005037#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005038 if( ssl->conf->badmac_limit != 0 &&
5039 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5042 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005043 }
5044#endif
5045
Hanno Becker4a810fb2017-05-24 16:27:30 +01005046 /* As above, invalid records cause
5047 * dismissal of the whole datagram. */
5048
5049 ssl->next_record_offset = 0;
5050 ssl->in_left = 0;
5051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005053 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005054 }
5055
5056 return( ret );
5057 }
5058 else
5059#endif
5060 {
5061 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5063 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 mbedtls_ssl_send_alert_message( ssl,
5066 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5067 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005068 }
5069#endif
5070 return( ret );
5071 }
5072 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005073
Simon Butcher99000142016-10-13 17:21:01 +01005074 return( 0 );
5075}
5076
5077int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5078{
5079 int ret;
5080
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005081 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005082 * Handle particular types of records
5083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005085 {
Simon Butcher99000142016-10-13 17:21:01 +01005086 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5087 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005088 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005089 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005090 }
5091
Hanno Beckere678eaa2018-08-21 14:57:46 +01005092 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005094 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005095 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5097 ssl->in_msglen ) );
5098 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005099 }
5100
Hanno Beckere678eaa2018-08-21 14:57:46 +01005101 if( ssl->in_msg[0] != 1 )
5102 {
5103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5104 ssl->in_msg[0] ) );
5105 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5106 }
5107
5108#if defined(MBEDTLS_SSL_PROTO_DTLS)
5109 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5110 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5111 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5112 {
5113 if( ssl->handshake == NULL )
5114 {
5115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5116 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5117 }
5118
5119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5120 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5121 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005122#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005123 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005126 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005127 if( ssl->in_msglen != 2 )
5128 {
5129 /* Note: Standard allows for more than one 2 byte alert
5130 to be packed in a single message, but Mbed TLS doesn't
5131 currently support this. */
5132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5133 ssl->in_msglen ) );
5134 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5135 }
5136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005138 ssl->in_msg[0], ssl->in_msg[1] ) );
5139
5140 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005141 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005146 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005148 }
5149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5151 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5154 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005155 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005156
5157#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5158 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5159 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5160 {
Hanno Becker90333da2017-10-10 11:27:13 +01005161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005162 /* Will be handled when trying to parse ServerHello */
5163 return( 0 );
5164 }
5165#endif
5166
5167#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5168 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5169 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5170 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5171 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5172 {
5173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5174 /* Will be handled in mbedtls_ssl_parse_certificate() */
5175 return( 0 );
5176 }
5177#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5178
5179 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005180 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005181 }
5182
Hanno Beckerc76c6192017-06-06 10:03:17 +01005183#if defined(MBEDTLS_SSL_PROTO_DTLS)
5184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5185 ssl->handshake != NULL &&
5186 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5187 {
5188 ssl_handshake_wrapup_free_hs_transform( ssl );
5189 }
5190#endif
5191
Paul Bakker5121ce52009-01-03 21:22:43 +00005192 return( 0 );
5193}
5194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005196{
5197 int ret;
5198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5200 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5201 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005202 {
5203 return( ret );
5204 }
5205
5206 return( 0 );
5207}
5208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005210 unsigned char level,
5211 unsigned char message )
5212{
5213 int ret;
5214
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005215 if( ssl == NULL || ssl->conf == NULL )
5216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005222 ssl->out_msglen = 2;
5223 ssl->out_msg[0] = level;
5224 ssl->out_msg[1] = message;
5225
Hanno Becker67bc7c32018-08-06 11:33:50 +01005226 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005229 return( ret );
5230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005232
5233 return( 0 );
5234}
5235
Paul Bakker5121ce52009-01-03 21:22:43 +00005236/*
5237 * Handshake functions
5238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005239#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5240 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5241 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5242 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5243 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5244 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5245 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005246/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5254 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005255 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5256 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005259 ssl->state++;
5260 return( 0 );
5261 }
5262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5264 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005265}
5266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5274 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005275 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5276 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005279 ssl->state++;
5280 return( 0 );
5281 }
5282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005285}
Gilles Peskinef9828522017-05-03 12:28:43 +02005286
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005287#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005288/* Some certificate support -> implement write and parse */
5289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005293 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 const mbedtls_x509_crt *crt;
5295 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5300 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005301 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5302 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005305 ssl->state++;
5306 return( 0 );
5307 }
5308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005310 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005311 {
5312 if( ssl->client_auth == 0 )
5313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005315 ssl->state++;
5316 return( 0 );
5317 }
5318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005320 /*
5321 * If using SSLv3 and got no cert, send an Alert message
5322 * (otherwise an empty Certificate message will be sent).
5323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5325 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 {
5327 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5329 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5330 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 goto write_msg;
5334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#endif /* MBEDTLS_SSL_CLI_C */
5338#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005339 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5344 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005345 }
5346 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005347#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005350
5351 /*
5352 * 0 . 0 handshake type
5353 * 1 . 3 handshake length
5354 * 4 . 6 length of all certs
5355 * 7 . 9 length of cert. 1
5356 * 10 . n-1 peer certificate
5357 * n . n+2 length of cert. 2
5358 * n+3 . ... upper level cert, etc.
5359 */
5360 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005362
Paul Bakker29087132010-03-21 21:03:34 +00005363 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005364 {
5365 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005366 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005369 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005371 }
5372
5373 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5374 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5375 ssl->out_msg[i + 2] = (unsigned char)( n );
5376
5377 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5378 i += n; crt = crt->next;
5379 }
5380
5381 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5382 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5383 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5384
5385 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5387 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005388
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005389#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005390write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005391#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005392
5393 ssl->state++;
5394
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005395 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 return( ret );
5399 }
5400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005402
Paul Bakkered27a042013-04-18 22:46:23 +02005403 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005404}
5405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005409 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005411 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005412 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5417 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005418 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5419 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005422 ssl->state++;
5423 return( 0 );
5424 }
5425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005427 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005428 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5429 {
5430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5431 ssl->state++;
5432 return( 0 );
5433 }
5434
5435#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5436 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5437 authmode = ssl->handshake->sni_authmode;
5438#endif
5439
5440 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5441 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005443 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 ssl->state++;
5446 return( 0 );
5447 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005448#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005449
Hanno Becker327c93b2018-08-15 13:56:18 +01005450 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005452 /* mbedtls_ssl_read_record may have sent an alert already. We
5453 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005455 return( ret );
5456 }
5457
5458 ssl->state++;
5459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460#if defined(MBEDTLS_SSL_SRV_C)
5461#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 /*
5463 * Check if the client sent an empty certificate
5464 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005465 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005468 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5470 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5471 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005474
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005475 /* The client was asked for a certificate but didn't send
5476 one. The client should know what's going on, so we
5477 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005478 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005479 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005480 return( 0 );
5481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005483 }
5484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5488 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005489 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5493 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5494 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5495 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005499 /* The client was asked for a certificate but didn't send
5500 one. The client should know what's going on, so we
5501 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005502 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005503 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005505 else
5506 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 }
5508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5510 MBEDTLS_SSL_PROTO_TLS1_2 */
5511#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005516 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5517 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 }
5520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5522 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005525 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5526 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 }
5529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005531
Paul Bakker5121ce52009-01-03 21:22:43 +00005532 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005534 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005535 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005536
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005537 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5542 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005544 }
5545
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005546 /* In case we tried to reuse a session but it failed */
5547 if( ssl->session_negotiate->peer_cert != NULL )
5548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5550 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005551 }
5552
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005553 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005558 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5559 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005560 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005561 }
5562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005564
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005565 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005566
5567 while( i < ssl->in_hslen )
5568 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005569 if ( i + 3 > ssl->in_hslen ) {
5570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5571 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5572 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5573 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005575 if( ssl->in_msg[i] != 0 )
5576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005578 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5579 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 }
5582
5583 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5584 | (unsigned int) ssl->in_msg[i + 2];
5585 i += 3;
5586
5587 if( n < 128 || i + n > ssl->in_hslen )
5588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005590 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5591 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005593 }
5594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005596 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005597 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005598 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005599 case 0: /*ok*/
5600 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5601 /* Ignore certificate with an unknown algorithm: maybe a
5602 prior certificate was already trusted. */
5603 break;
5604
5605 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5606 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5607 goto crt_parse_der_failed;
5608
5609 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5610 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5611 goto crt_parse_der_failed;
5612
5613 default:
5614 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5615 crt_parse_der_failed:
5616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005618 return( ret );
5619 }
5620
5621 i += n;
5622 }
5623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005625
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005626 /*
5627 * On client, make sure the server cert doesn't change during renego to
5628 * avoid "triple handshake" attack: https://secure-resumption.com/
5629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005633 {
5634 if( ssl->session->peer_cert == NULL )
5635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005637 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5638 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005640 }
5641
5642 if( ssl->session->peer_cert->raw.len !=
5643 ssl->session_negotiate->peer_cert->raw.len ||
5644 memcmp( ssl->session->peer_cert->raw.p,
5645 ssl->session_negotiate->peer_cert->raw.p,
5646 ssl->session->peer_cert->raw.len ) != 0 )
5647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005649 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5650 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005652 }
5653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005655
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005656 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005658 mbedtls_x509_crt *ca_chain;
5659 mbedtls_x509_crl *ca_crl;
5660
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005661#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005662 if( ssl->handshake->sni_ca_chain != NULL )
5663 {
5664 ca_chain = ssl->handshake->sni_ca_chain;
5665 ca_crl = ssl->handshake->sni_ca_crl;
5666 }
5667 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005668#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005669 {
5670 ca_chain = ssl->conf->ca_chain;
5671 ca_crl = ssl->conf->ca_crl;
5672 }
5673
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005674 /*
5675 * Main check: verify certificate
5676 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005677 ret = mbedtls_x509_crt_verify_with_profile(
5678 ssl->session_negotiate->peer_cert,
5679 ca_chain, ca_crl,
5680 ssl->conf->cert_profile,
5681 ssl->hostname,
5682 &ssl->session_negotiate->verify_result,
5683 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005684
5685 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005688 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005689
5690 /*
5691 * Secondary checks: always done, but change 'ret' only if it was 0
5692 */
5693
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005694#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005697
5698 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005700 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005701 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005702 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005705 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005707 }
5708 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005709#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005712 ciphersuite_info,
5713 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005714 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005717 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005719 }
5720
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005721 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5722 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5723 * with details encoded in the verification flags. All other kinds
5724 * of error codes, including those from the user provided f_vrfy
5725 * functions, are treated as fatal and lead to a failure of
5726 * ssl_parse_certificate even if verification was optional. */
5727 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5728 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5729 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5730 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005731 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005732 }
5733
5734 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5735 {
5736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5737 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5738 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005739
5740 if( ret != 0 )
5741 {
5742 /* The certificate may have been rejected for several reasons.
5743 Pick one and send the corresponding alert. Which alert to send
5744 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005745 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5746 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5747 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5748 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5749 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5750 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5751 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5752 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5753 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5754 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5755 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5756 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5757 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5758 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5759 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5760 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5761 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5762 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5763 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5764 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005765 else
5766 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005767 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5768 alert );
5769 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005770
Hanno Beckere6706e62017-05-15 16:05:15 +01005771#if defined(MBEDTLS_DEBUG_C)
5772 if( ssl->session_negotiate->verify_result != 0 )
5773 {
5774 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5775 ssl->session_negotiate->verify_result ) );
5776 }
5777 else
5778 {
5779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5780 }
5781#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 }
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005785
5786 return( ret );
5787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005788#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5789 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5790 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5791 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5792 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5793 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5794 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005797{
5798 int ret;
5799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 ssl->out_msglen = 1;
5804 ssl->out_msg[0] = 1;
5805
Paul Bakker5121ce52009-01-03 21:22:43 +00005806 ssl->state++;
5807
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005808 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 return( ret );
5812 }
5813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005815
5816 return( 0 );
5817}
5818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820{
5821 int ret;
5822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005824
Hanno Becker327c93b2018-08-15 13:56:18 +01005825 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 return( ret );
5829 }
5830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5835 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 }
5838
Hanno Beckere678eaa2018-08-21 14:57:46 +01005839 /* CCS records are only accepted if they have length 1 and content '1',
5840 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005841
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005842 /*
5843 * Switch to our negotiated transform and session parameters for inbound
5844 * data.
5845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005847 ssl->transform_in = ssl->transform_negotiate;
5848 ssl->session_in = ssl->session_negotiate;
5849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005854 ssl_dtls_replay_reset( ssl );
5855#endif
5856
5857 /* Increment epoch */
5858 if( ++ssl->in_epoch == 0 )
5859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005861 /* This is highly unlikely to happen for legitimate reasons, so
5862 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005864 }
5865 }
5866 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005868 memset( ssl->in_ctr, 0, 8 );
5869
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005870 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5873 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005878 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5879 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005881 }
5882 }
5883#endif
5884
Paul Bakker5121ce52009-01-03 21:22:43 +00005885 ssl->state++;
5886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005888
5889 return( 0 );
5890}
5891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5893 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005894{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005895 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5898 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5899 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005900 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005901 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5904#if defined(MBEDTLS_SHA512_C)
5905 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005906 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5907 else
5908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909#if defined(MBEDTLS_SHA256_C)
5910 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005911 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005912 else
5913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005917 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005918 }
Paul Bakker380da532012-04-18 16:10:25 +00005919}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5924 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005925 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5926 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005927#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5929#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005930 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005933 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005936}
5937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005939 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5942 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005943 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5944 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5947#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005948 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005951 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005954}
5955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5957 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5958static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005959 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005960{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005961 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5962 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005963}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005964#endif
Paul Bakker380da532012-04-18 16:10:25 +00005965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5967#if defined(MBEDTLS_SHA256_C)
5968static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005969 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005970{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005971 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005972}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005973#endif
Paul Bakker380da532012-04-18 16:10:25 +00005974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975#if defined(MBEDTLS_SHA512_C)
5976static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005977 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005978{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005979 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005980}
Paul Bakker769075d2012-11-24 11:26:46 +01005981#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005985static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005987{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005988 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005989 mbedtls_md5_context md5;
5990 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005991
Paul Bakker5121ce52009-01-03 21:22:43 +00005992 unsigned char padbuf[48];
5993 unsigned char md5sum[16];
5994 unsigned char sha1sum[20];
5995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005997 if( !session )
5998 session = ssl->session;
5999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006001
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006002 mbedtls_md5_init( &md5 );
6003 mbedtls_sha1_init( &sha1 );
6004
6005 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6006 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006007
6008 /*
6009 * SSLv3:
6010 * hash =
6011 * MD5( master + pad2 +
6012 * MD5( handshake + sender + master + pad1 ) )
6013 * + SHA1( master + pad2 +
6014 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 */
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006018 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6019 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006020#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006023 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6024 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006025#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006028 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006029
Paul Bakker1ef83d62012-04-11 12:09:53 +00006030 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006032 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6033 mbedtls_md5_update_ret( &md5, session->master, 48 );
6034 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6035 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006036
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006037 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6038 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6039 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6040 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006041
Paul Bakker1ef83d62012-04-11 12:09:53 +00006042 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006043
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006044 mbedtls_md5_starts_ret( &md5 );
6045 mbedtls_md5_update_ret( &md5, session->master, 48 );
6046 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6047 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6048 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006049
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006050 mbedtls_sha1_starts_ret( &sha1 );
6051 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6052 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6053 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6054 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006057
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006058 mbedtls_md5_free( &md5 );
6059 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006060
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006061 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6062 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6063 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006070static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006072{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006073 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006074 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006075 mbedtls_md5_context md5;
6076 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006077 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006080 if( !session )
6081 session = ssl->session;
6082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006084
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006085 mbedtls_md5_init( &md5 );
6086 mbedtls_sha1_init( &sha1 );
6087
6088 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6089 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006090
Paul Bakker1ef83d62012-04-11 12:09:53 +00006091 /*
6092 * TLSv1:
6093 * hash = PRF( master, finished_label,
6094 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6095 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006098 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6099 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006100#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006103 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6104 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006105#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006108 ? "client finished"
6109 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006110
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006111 mbedtls_md5_finish_ret( &md5, padbuf );
6112 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006113
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006114 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006115 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006118
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006119 mbedtls_md5_free( &md5 );
6120 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006121
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006122 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6129#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006130static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006132{
6133 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006134 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006135 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006136 unsigned char padbuf[32];
6137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006139 if( !session )
6140 session = ssl->session;
6141
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006142 mbedtls_sha256_init( &sha256 );
6143
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006145
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006146 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006147
6148 /*
6149 * TLSv1.2:
6150 * hash = PRF( master, finished_label,
6151 * Hash( handshake ) )[0.11]
6152 */
6153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154#if !defined(MBEDTLS_SHA256_ALT)
6155 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006156 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006157#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006160 ? "client finished"
6161 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006162
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006163 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006164
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006165 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006166 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006169
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006170 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006171
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006172 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006179static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006181{
6182 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006183 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006184 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006185 unsigned char padbuf[48];
6186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006188 if( !session )
6189 session = ssl->session;
6190
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006191 mbedtls_sha512_init( &sha512 );
6192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006194
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006195 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006196
6197 /*
6198 * TLSv1.2:
6199 * hash = PRF( master, finished_label,
6200 * Hash( handshake ) )[0.11]
6201 */
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006204 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6205 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006206#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006209 ? "client finished"
6210 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006211
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006212 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006213
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006214 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006215 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006218
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006219 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006220
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006221 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225#endif /* MBEDTLS_SHA512_C */
6226#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006231
6232 /*
6233 * Free our handshake params
6234 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006235 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006237 ssl->handshake = NULL;
6238
6239 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006240 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006241 */
6242 if( ssl->transform )
6243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 mbedtls_ssl_transform_free( ssl->transform );
6245 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006246 }
6247 ssl->transform = ssl->transform_negotiate;
6248 ssl->transform_negotiate = NULL;
6249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006251}
6252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006254{
6255 int resume = ssl->handshake->resume;
6256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259#if defined(MBEDTLS_SSL_RENEGOTIATION)
6260 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006263 ssl->renego_records_seen = 0;
6264 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006265#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006266
6267 /*
6268 * Free the previous session and switch in the current one
6269 */
Paul Bakker0a597072012-09-25 21:55:46 +00006270 if( ssl->session )
6271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006273 /* RFC 7366 3.1: keep the EtM state */
6274 ssl->session_negotiate->encrypt_then_mac =
6275 ssl->session->encrypt_then_mac;
6276#endif
6277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 mbedtls_ssl_session_free( ssl->session );
6279 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006280 }
6281 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006282 ssl->session_negotiate = NULL;
6283
Paul Bakker0a597072012-09-25 21:55:46 +00006284 /*
6285 * Add cache entry
6286 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006287 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006288 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006289 resume == 0 )
6290 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006291 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006293 }
Paul Bakker0a597072012-09-25 21:55:46 +00006294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006296 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006297 ssl->handshake->flight != NULL )
6298 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006299 /* Cancel handshake timer */
6300 ssl_set_timer( ssl, 0 );
6301
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006302 /* Keep last flight around in case we need to resend it:
6303 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006305 }
6306 else
6307#endif
6308 ssl_handshake_wrapup_free_hs_transform( ssl );
6309
Paul Bakker48916f92012-09-16 19:57:18 +00006310 ssl->state++;
6311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006313}
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006316{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006317 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006320
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006321 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006322
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006323 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006324
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006325 /*
6326 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6327 * may define some other value. Currently (early 2016), no defined
6328 * ciphersuite does this (and this is unlikely to change as activity has
6329 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006334 ssl->verify_data_len = hash_len;
6335 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006336#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006337
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6340 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006341
6342 /*
6343 * In case of session resuming, invert the client and server
6344 * ChangeCipherSpec messages order.
6345 */
Paul Bakker0a597072012-09-25 21:55:46 +00006346 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006349 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006351#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006353 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006355#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 }
6357 else
6358 ssl->state++;
6359
Paul Bakker48916f92012-09-16 19:57:18 +00006360 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006361 * Switch to our negotiated transform and session parameters for outbound
6362 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006367 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006368 {
6369 unsigned char i;
6370
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006371 /* Remember current epoch settings for resending */
6372 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006373 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006374
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006375 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006376 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006377
6378 /* Increment epoch */
6379 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006380 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006381 break;
6382
6383 /* The loop goes to its end iff the counter is wrapping */
6384 if( i == 0 )
6385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6387 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006388 }
6389 }
6390 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006392 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006393
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006394 ssl->transform_out = ssl->transform_negotiate;
6395 ssl->session_out = ssl->session_negotiate;
6396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6398 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6403 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006404 }
6405 }
6406#endif
6407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006409 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006411#endif
6412
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006413 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006414 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006416 return( ret );
6417 }
6418
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006419#if defined(MBEDTLS_SSL_PROTO_DTLS)
6420 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6421 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6422 {
6423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6424 return( ret );
6425 }
6426#endif
6427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006429
6430 return( 0 );
6431}
6432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006434#define SSL_MAX_HASH_LEN 36
6435#else
6436#define SSL_MAX_HASH_LEN 12
6437#endif
6438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006440{
Paul Bakker23986e52011-04-24 08:57:21 +00006441 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006442 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006443 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006446
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006447 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006448
Hanno Becker327c93b2018-08-15 13:56:18 +01006449 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006452 return( ret );
6453 }
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006458 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6459 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006461 }
6462
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006463 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#if defined(MBEDTLS_SSL_PROTO_SSL3)
6465 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006466 hash_len = 36;
6467 else
6468#endif
6469 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6472 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006475 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6476 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006478 }
6479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006481 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6485 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 }
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006490 ssl->verify_data_len = hash_len;
6491 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006492#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006493
Paul Bakker0a597072012-09-25 21:55:46 +00006494 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006497 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006501 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 }
6505 else
6506 ssl->state++;
6507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006509 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006511#endif
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006514
6515 return( 0 );
6516}
6517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6523 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6524 mbedtls_md5_init( &handshake->fin_md5 );
6525 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006526 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6527 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006528#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6530#if defined(MBEDTLS_SHA256_C)
6531 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006532 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#if defined(MBEDTLS_SHA512_C)
6535 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006536 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006537#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006539
6540 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006541
6542#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6543 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6544 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6545#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547#if defined(MBEDTLS_DHM_C)
6548 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006549#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550#if defined(MBEDTLS_ECDH_C)
6551 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006552#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006553#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006554 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006555#if defined(MBEDTLS_SSL_CLI_C)
6556 handshake->ecjpake_cache = NULL;
6557 handshake->ecjpake_cache_len = 0;
6558#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006559#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006560
6561#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6562 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6563#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006564}
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6571 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 mbedtls_md_init( &transform->md_ctx_enc );
6574 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006575}
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006580}
6581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006583{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006584 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006585 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006587 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006589 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006590 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006591
6592 /*
6593 * Either the pointers are now NULL or cleared properly and can be freed.
6594 * Now allocate missing structures.
6595 */
6596 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006597 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006598 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006599 }
Paul Bakker48916f92012-09-16 19:57:18 +00006600
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006601 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006602 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006603 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006604 }
Paul Bakker48916f92012-09-16 19:57:18 +00006605
Paul Bakker82788fb2014-10-20 13:59:19 +02006606 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006607 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006608 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006609 }
Paul Bakker48916f92012-09-16 19:57:18 +00006610
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006611 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006612 if( ssl->handshake == NULL ||
6613 ssl->transform_negotiate == NULL ||
6614 ssl->session_negotiate == NULL )
6615 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618 mbedtls_free( ssl->handshake );
6619 mbedtls_free( ssl->transform_negotiate );
6620 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006621
6622 ssl->handshake = NULL;
6623 ssl->transform_negotiate = NULL;
6624 ssl->session_negotiate = NULL;
6625
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006626 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006627 }
6628
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006629 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006631 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006632 ssl_handshake_params_init( ssl->handshake );
6633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006635 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6636 {
6637 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006638
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006639 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6640 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6641 else
6642 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006643
6644 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006645 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006646#endif
6647
Paul Bakker48916f92012-09-16 19:57:18 +00006648 return( 0 );
6649}
6650
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006651#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006652/* Dummy cookie callbacks for defaults */
6653static int ssl_cookie_write_dummy( void *ctx,
6654 unsigned char **p, unsigned char *end,
6655 const unsigned char *cli_id, size_t cli_id_len )
6656{
6657 ((void) ctx);
6658 ((void) p);
6659 ((void) end);
6660 ((void) cli_id);
6661 ((void) cli_id_len);
6662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006664}
6665
6666static int ssl_cookie_check_dummy( void *ctx,
6667 const unsigned char *cookie, size_t cookie_len,
6668 const unsigned char *cli_id, size_t cli_id_len )
6669{
6670 ((void) ctx);
6671 ((void) cookie);
6672 ((void) cookie_len);
6673 ((void) cli_id);
6674 ((void) cli_id_len);
6675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006677}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006678#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006679
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006680/* Once ssl->out_hdr as the address of the beginning of the
6681 * next outgoing record is set, deduce the other pointers.
6682 *
6683 * Note: For TLS, we save the implicit record sequence number
6684 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6685 * and the caller has to make sure there's space for this.
6686 */
6687
6688static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6689 mbedtls_ssl_transform *transform )
6690{
6691#if defined(MBEDTLS_SSL_PROTO_DTLS)
6692 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6693 {
6694 ssl->out_ctr = ssl->out_hdr + 3;
6695 ssl->out_len = ssl->out_hdr + 11;
6696 ssl->out_iv = ssl->out_hdr + 13;
6697 }
6698 else
6699#endif
6700 {
6701 ssl->out_ctr = ssl->out_hdr - 8;
6702 ssl->out_len = ssl->out_hdr + 3;
6703 ssl->out_iv = ssl->out_hdr + 5;
6704 }
6705
6706 /* Adjust out_msg to make space for explicit IV, if used. */
6707 if( transform != NULL &&
6708 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6709 {
6710 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6711 }
6712 else
6713 ssl->out_msg = ssl->out_iv;
6714}
6715
6716/* Once ssl->in_hdr as the address of the beginning of the
6717 * next incoming record is set, deduce the other pointers.
6718 *
6719 * Note: For TLS, we save the implicit record sequence number
6720 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6721 * and the caller has to make sure there's space for this.
6722 */
6723
6724static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6725 mbedtls_ssl_transform *transform )
6726{
6727#if defined(MBEDTLS_SSL_PROTO_DTLS)
6728 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6729 {
6730 ssl->in_ctr = ssl->in_hdr + 3;
6731 ssl->in_len = ssl->in_hdr + 11;
6732 ssl->in_iv = ssl->in_hdr + 13;
6733 }
6734 else
6735#endif
6736 {
6737 ssl->in_ctr = ssl->in_hdr - 8;
6738 ssl->in_len = ssl->in_hdr + 3;
6739 ssl->in_iv = ssl->in_hdr + 5;
6740 }
6741
6742 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6743 if( transform != NULL &&
6744 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6745 {
6746 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6747 }
6748 else
6749 ssl->in_msg = ssl->in_iv;
6750}
6751
Paul Bakker5121ce52009-01-03 21:22:43 +00006752/*
6753 * Initialize an SSL context
6754 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006755void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6756{
6757 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6758}
6759
6760/*
6761 * Setup an SSL context
6762 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006763
6764static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6765{
6766 /* Set the incoming and outgoing record pointers. */
6767#if defined(MBEDTLS_SSL_PROTO_DTLS)
6768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6769 {
6770 ssl->out_hdr = ssl->out_buf;
6771 ssl->in_hdr = ssl->in_buf;
6772 }
6773 else
6774#endif /* MBEDTLS_SSL_PROTO_DTLS */
6775 {
6776 ssl->out_hdr = ssl->out_buf + 8;
6777 ssl->in_hdr = ssl->in_buf + 8;
6778 }
6779
6780 /* Derive other internal pointers. */
6781 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6782 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6783}
6784
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006785int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006786 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006787{
Paul Bakker48916f92012-09-16 19:57:18 +00006788 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006789
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006790 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006791
6792 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006793 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006794 */
Angus Grattond8213d02016-05-25 20:56:48 +10006795 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6796 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006797 {
Angus Grattond8213d02016-05-25 20:56:48 +10006798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6799 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6800 }
6801
6802 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6803 if( ssl->out_buf == NULL )
6804 {
6805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006807 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006808 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006809 }
6810
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006811 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006812
Paul Bakker48916f92012-09-16 19:57:18 +00006813 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6814 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006815
6816 return( 0 );
6817}
6818
6819/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006820 * Reset an initialized and used SSL context for re-use while retaining
6821 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006822 *
6823 * If partial is non-zero, keep data in the input buffer and client ID.
6824 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006825 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006826static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006827{
Paul Bakker48916f92012-09-16 19:57:18 +00006828 int ret;
6829
Hanno Becker7e772132018-08-10 12:38:21 +01006830#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6831 !defined(MBEDTLS_SSL_SRV_C)
6832 ((void) partial);
6833#endif
6834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006836
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006837 /* Cancel any possibly running timer */
6838 ssl_set_timer( ssl, 0 );
6839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SSL_RENEGOTIATION)
6841 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006842 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006843
6844 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6846 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006849
Paul Bakker7eb013f2011-10-06 12:37:39 +00006850 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006851 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006852
6853 ssl->in_msgtype = 0;
6854 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006856 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006857 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006860 ssl_dtls_replay_reset( ssl );
6861#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006862
6863 ssl->in_hslen = 0;
6864 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006865
6866 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006867
6868 ssl->out_msgtype = 0;
6869 ssl->out_msglen = 0;
6870 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6872 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006873 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006874#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006875
Hanno Becker19859472018-08-06 09:40:20 +01006876 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6877
Paul Bakker48916f92012-09-16 19:57:18 +00006878 ssl->transform_in = NULL;
6879 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006880
Hanno Becker78640902018-08-13 16:35:15 +01006881 ssl->session_in = NULL;
6882 ssl->session_out = NULL;
6883
Angus Grattond8213d02016-05-25 20:56:48 +10006884 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006885
6886#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006887 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006888#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6889 {
6890 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006891 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006892 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6895 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6898 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6901 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006902 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006903 }
6904#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006905
Paul Bakker48916f92012-09-16 19:57:18 +00006906 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 mbedtls_ssl_transform_free( ssl->transform );
6909 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006910 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006911 }
Paul Bakker48916f92012-09-16 19:57:18 +00006912
Paul Bakkerc0463502013-02-14 11:19:38 +01006913 if( ssl->session )
6914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915 mbedtls_ssl_session_free( ssl->session );
6916 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006917 ssl->session = NULL;
6918 }
6919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006921 ssl->alpn_chosen = NULL;
6922#endif
6923
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006924#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006925#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006926 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006927#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006928 {
6929 mbedtls_free( ssl->cli_id );
6930 ssl->cli_id = NULL;
6931 ssl->cli_id_len = 0;
6932 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006933#endif
6934
Paul Bakker48916f92012-09-16 19:57:18 +00006935 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6936 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006937
6938 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006939}
6940
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006941/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006942 * Reset an initialized and used SSL context for re-use while retaining
6943 * all application-set variables, function pointers and data.
6944 */
6945int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6946{
6947 return( ssl_session_reset_int( ssl, 0 ) );
6948}
6949
6950/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 * SSL set accessors
6952 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006953void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006954{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006955 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006956}
6957
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006958void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006959{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006960 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006961}
6962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006964void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006965{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006966 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006967}
6968#endif
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006971void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006973 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006974}
6975#endif
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01006978
6979void mbedtls_ssl_conf_datagram_packing( mbedtls_ssl_context *ssl,
6980 unsigned allow_packing )
6981{
6982 ssl->disable_datagram_packing = !allow_packing;
6983}
6984
6985void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
6986 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006988 conf->hs_timeout_min = min;
6989 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006990}
6991#endif
6992
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006993void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006995 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006996}
6997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006999void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007000 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007001 void *p_vrfy )
7002{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007003 conf->f_vrfy = f_vrfy;
7004 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007007
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007008void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007009 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007010 void *p_rng )
7011{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007012 conf->f_rng = f_rng;
7013 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007014}
7015
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007016void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007017 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007018 void *p_dbg )
7019{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007020 conf->f_dbg = f_dbg;
7021 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007022}
7023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007025 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007026 mbedtls_ssl_send_t *f_send,
7027 mbedtls_ssl_recv_t *f_recv,
7028 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007029{
7030 ssl->p_bio = p_bio;
7031 ssl->f_send = f_send;
7032 ssl->f_recv = f_recv;
7033 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007034}
7035
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007036#if defined(MBEDTLS_SSL_PROTO_DTLS)
7037void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7038{
7039 ssl->mtu = mtu;
7040}
7041#endif
7042
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007043void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007044{
7045 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007046}
7047
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007048void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7049 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007050 mbedtls_ssl_set_timer_t *f_set_timer,
7051 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007052{
7053 ssl->p_timer = p_timer;
7054 ssl->f_set_timer = f_set_timer;
7055 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007056
7057 /* Make sure we start with no timer running */
7058 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007059}
7060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007062void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007063 void *p_cache,
7064 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7065 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007066{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007067 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007068 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007069 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073#if defined(MBEDTLS_SSL_CLI_C)
7074int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007075{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007076 int ret;
7077
7078 if( ssl == NULL ||
7079 session == NULL ||
7080 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007081 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007084 }
7085
7086 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7087 return( ret );
7088
Paul Bakker0a597072012-09-25 21:55:46 +00007089 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007090
7091 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007095void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007096 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007098 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7099 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7100 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7101 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007102}
7103
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007104void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007105 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007106 int major, int minor )
7107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007109 return;
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007112 return;
7113
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007114 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007115}
7116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007118void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007119 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007120{
7121 conf->cert_profile = profile;
7122}
7123
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007124/* Append a new keycert entry to a (possibly empty) list */
7125static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7126 mbedtls_x509_crt *cert,
7127 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007128{
niisato8ee24222018-06-25 19:05:48 +09007129 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007130
niisato8ee24222018-06-25 19:05:48 +09007131 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7132 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007133 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007134
niisato8ee24222018-06-25 19:05:48 +09007135 new_cert->cert = cert;
7136 new_cert->key = key;
7137 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007138
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007139 /* Update head is the list was null, else add to the end */
7140 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007141 {
niisato8ee24222018-06-25 19:05:48 +09007142 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007143 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007144 else
7145 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007146 mbedtls_ssl_key_cert *cur = *head;
7147 while( cur->next != NULL )
7148 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007149 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007150 }
7151
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007152 return( 0 );
7153}
7154
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007155int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007156 mbedtls_x509_crt *own_cert,
7157 mbedtls_pk_context *pk_key )
7158{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007159 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007160}
7161
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007162void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007163 mbedtls_x509_crt *ca_chain,
7164 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007165{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007166 conf->ca_chain = ca_chain;
7167 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007170
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007171#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7172int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7173 mbedtls_x509_crt *own_cert,
7174 mbedtls_pk_context *pk_key )
7175{
7176 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7177 own_cert, pk_key ) );
7178}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007179
7180void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7181 mbedtls_x509_crt *ca_chain,
7182 mbedtls_x509_crl *ca_crl )
7183{
7184 ssl->handshake->sni_ca_chain = ca_chain;
7185 ssl->handshake->sni_ca_crl = ca_crl;
7186}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007187
7188void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7189 int authmode )
7190{
7191 ssl->handshake->sni_authmode = authmode;
7192}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007193#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7194
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007195#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007196/*
7197 * Set EC J-PAKE password for current handshake
7198 */
7199int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7200 const unsigned char *pw,
7201 size_t pw_len )
7202{
7203 mbedtls_ecjpake_role role;
7204
Janos Follath8eb64132016-06-03 15:40:57 +01007205 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7207
7208 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7209 role = MBEDTLS_ECJPAKE_SERVER;
7210 else
7211 role = MBEDTLS_ECJPAKE_CLIENT;
7212
7213 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7214 role,
7215 MBEDTLS_MD_SHA256,
7216 MBEDTLS_ECP_DP_SECP256R1,
7217 pw, pw_len ) );
7218}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007219#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007222int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007223 const unsigned char *psk, size_t psk_len,
7224 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007225{
Paul Bakker6db455e2013-09-18 17:29:31 +02007226 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007231
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007232 /* Identity len will be encoded on two bytes */
7233 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007234 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007235 {
7236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7237 }
7238
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007239 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007240 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007241 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007242
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007243 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007244 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007245 conf->psk_len = 0;
7246 }
7247 if( conf->psk_identity != NULL )
7248 {
7249 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007250 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007251 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007252 }
7253
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007254 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7255 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007256 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007257 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007258 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007259 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007260 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007261 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007262 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007263
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007264 conf->psk_len = psk_len;
7265 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007266
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007267 memcpy( conf->psk, psk, conf->psk_len );
7268 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007269
7270 return( 0 );
7271}
7272
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007273int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7274 const unsigned char *psk, size_t psk_len )
7275{
7276 if( psk == NULL || ssl->handshake == NULL )
7277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7278
7279 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7281
7282 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007283 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007284 mbedtls_platform_zeroize( ssl->handshake->psk,
7285 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007286 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007287 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007288 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007289
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007290 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007291 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007292
7293 ssl->handshake->psk_len = psk_len;
7294 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7295
7296 return( 0 );
7297}
7298
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007299void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007301 size_t),
7302 void *p_psk )
7303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007304 conf->f_psk = f_psk;
7305 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007308
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007309#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007310
7311#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007312int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007313{
7314 int ret;
7315
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007316 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7317 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7318 {
7319 mbedtls_mpi_free( &conf->dhm_P );
7320 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007321 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007323
7324 return( 0 );
7325}
Hanno Becker470a8c42017-10-04 15:28:46 +01007326#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007327
Hanno Beckera90658f2017-10-04 15:29:08 +01007328int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7329 const unsigned char *dhm_P, size_t P_len,
7330 const unsigned char *dhm_G, size_t G_len )
7331{
7332 int ret;
7333
7334 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7335 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7336 {
7337 mbedtls_mpi_free( &conf->dhm_P );
7338 mbedtls_mpi_free( &conf->dhm_G );
7339 return( ret );
7340 }
7341
7342 return( 0 );
7343}
Paul Bakker5121ce52009-01-03 21:22:43 +00007344
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007345int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007346{
7347 int ret;
7348
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007349 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7350 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7351 {
7352 mbedtls_mpi_free( &conf->dhm_P );
7353 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007354 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007355 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007356
7357 return( 0 );
7358}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007359#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007360
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007361#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7362/*
7363 * Set the minimum length for Diffie-Hellman parameters
7364 */
7365void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7366 unsigned int bitlen )
7367{
7368 conf->dhm_min_bitlen = bitlen;
7369}
7370#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7371
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007372#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007373/*
7374 * Set allowed/preferred hashes for handshake signatures
7375 */
7376void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7377 const int *hashes )
7378{
7379 conf->sig_hashes = hashes;
7380}
Hanno Becker947194e2017-04-07 13:25:49 +01007381#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007382
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007383#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007384/*
7385 * Set the allowed elliptic curves
7386 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007387void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007388 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007389{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007390 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007391}
Hanno Becker947194e2017-04-07 13:25:49 +01007392#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007393
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007394#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007396{
Hanno Becker947194e2017-04-07 13:25:49 +01007397 /* Initialize to suppress unnecessary compiler warning */
7398 size_t hostname_len = 0;
7399
7400 /* Check if new hostname is valid before
7401 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007402 if( hostname != NULL )
7403 {
7404 hostname_len = strlen( hostname );
7405
7406 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7408 }
7409
7410 /* Now it's clear that we will overwrite the old hostname,
7411 * so we can free it safely */
7412
7413 if( ssl->hostname != NULL )
7414 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007415 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007416 mbedtls_free( ssl->hostname );
7417 }
7418
7419 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007420
Paul Bakker5121ce52009-01-03 21:22:43 +00007421 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007422 {
7423 ssl->hostname = NULL;
7424 }
7425 else
7426 {
7427 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007428 if( ssl->hostname == NULL )
7429 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007430
Hanno Becker947194e2017-04-07 13:25:49 +01007431 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007432
Hanno Becker947194e2017-04-07 13:25:49 +01007433 ssl->hostname[hostname_len] = '\0';
7434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007435
7436 return( 0 );
7437}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007438#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007439
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007440#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007441void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007443 const unsigned char *, size_t),
7444 void *p_sni )
7445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007446 conf->f_sni = f_sni;
7447 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007452int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007453{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007454 size_t cur_len, tot_len;
7455 const char **p;
7456
7457 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007458 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7459 * MUST NOT be truncated."
7460 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007461 */
7462 tot_len = 0;
7463 for( p = protos; *p != NULL; p++ )
7464 {
7465 cur_len = strlen( *p );
7466 tot_len += cur_len;
7467
7468 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007470 }
7471
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007472 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007473
7474 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007475}
7476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007478{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007479 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007482
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007483void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007484{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007485 conf->max_major_ver = major;
7486 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007487}
7488
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007489void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007491 conf->min_major_ver = major;
7492 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007493}
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007496void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007497{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007498 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007499}
7500#endif
7501
Janos Follath088ce432017-04-10 12:42:31 +01007502#if defined(MBEDTLS_SSL_SRV_C)
7503void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7504 char cert_req_ca_list )
7505{
7506 conf->cert_req_ca_list = cert_req_ca_list;
7507}
7508#endif
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007511void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007512{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007513 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007514}
7515#endif
7516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007518void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007519{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007520 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007521}
7522#endif
7523
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007524#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007525void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007527 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007528}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007529#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007532int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007535 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007538 }
7539
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007540 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007541
7542 return( 0 );
7543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007547void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007549 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007554void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007555{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007556 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007557}
7558#endif
7559
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007560void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007561{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007562 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007563}
7564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007566void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007568 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007569}
7570
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007571void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007573 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007574}
7575
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007576void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007577 const unsigned char period[8] )
7578{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007579 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007584#if defined(MBEDTLS_SSL_CLI_C)
7585void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007586{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007587 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007588}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007589#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007590
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007591#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007592void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7593 mbedtls_ssl_ticket_write_t *f_ticket_write,
7594 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7595 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007596{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007597 conf->f_ticket_write = f_ticket_write;
7598 conf->f_ticket_parse = f_ticket_parse;
7599 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007600}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007603
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007604#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7605void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7606 mbedtls_ssl_export_keys_t *f_export_keys,
7607 void *p_export_keys )
7608{
7609 conf->f_export_keys = f_export_keys;
7610 conf->p_export_keys = p_export_keys;
7611}
7612#endif
7613
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007614#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007615void mbedtls_ssl_conf_async_private_cb(
7616 mbedtls_ssl_config *conf,
7617 mbedtls_ssl_async_sign_t *f_async_sign,
7618 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7619 mbedtls_ssl_async_resume_t *f_async_resume,
7620 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007621 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007622{
7623 conf->f_async_sign_start = f_async_sign;
7624 conf->f_async_decrypt_start = f_async_decrypt;
7625 conf->f_async_resume = f_async_resume;
7626 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007627 conf->p_async_config_data = async_config_data;
7628}
7629
Gilles Peskine8f97af72018-04-26 11:46:10 +02007630void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7631{
7632 return( conf->p_async_config_data );
7633}
7634
Gilles Peskine1febfef2018-04-30 11:54:39 +02007635void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007636{
7637 if( ssl->handshake == NULL )
7638 return( NULL );
7639 else
7640 return( ssl->handshake->user_async_ctx );
7641}
7642
Gilles Peskine1febfef2018-04-30 11:54:39 +02007643void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007644 void *ctx )
7645{
7646 if( ssl->handshake != NULL )
7647 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007648}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007649#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007650
Paul Bakker5121ce52009-01-03 21:22:43 +00007651/*
7652 * SSL get accessors
7653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007655{
7656 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7657}
7658
Hanno Becker8b170a02017-10-10 11:51:19 +01007659int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7660{
7661 /*
7662 * Case A: We're currently holding back
7663 * a message for further processing.
7664 */
7665
7666 if( ssl->keep_current_message == 1 )
7667 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007669 return( 1 );
7670 }
7671
7672 /*
7673 * Case B: Further records are pending in the current datagram.
7674 */
7675
7676#if defined(MBEDTLS_SSL_PROTO_DTLS)
7677 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7678 ssl->in_left > ssl->next_record_offset )
7679 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007680 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007681 return( 1 );
7682 }
7683#endif /* MBEDTLS_SSL_PROTO_DTLS */
7684
7685 /*
7686 * Case C: A handshake message is being processed.
7687 */
7688
Hanno Becker8b170a02017-10-10 11:51:19 +01007689 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7690 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007692 return( 1 );
7693 }
7694
7695 /*
7696 * Case D: An application data message is being processed
7697 */
7698 if( ssl->in_offt != NULL )
7699 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007701 return( 1 );
7702 }
7703
7704 /*
7705 * In all other cases, the rest of the message can be dropped.
7706 * As in ssl_read_record_layer, this needs to be adapted if
7707 * we implement support for multiple alerts in single records.
7708 */
7709
7710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7711 return( 0 );
7712}
7713
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007714uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007715{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007716 if( ssl->session != NULL )
7717 return( ssl->session->verify_result );
7718
7719 if( ssl->session_negotiate != NULL )
7720 return( ssl->session_negotiate->verify_result );
7721
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007722 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007723}
7724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007726{
Paul Bakker926c8e42013-03-06 10:23:34 +01007727 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007728 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007731}
7732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007734{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007736 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007737 {
7738 switch( ssl->minor_ver )
7739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007741 return( "DTLSv1.0" );
7742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007744 return( "DTLSv1.2" );
7745
7746 default:
7747 return( "unknown (DTLS)" );
7748 }
7749 }
7750#endif
7751
Paul Bakker43ca69c2011-01-15 17:35:19 +00007752 switch( ssl->minor_ver )
7753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007755 return( "SSLv3.0" );
7756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007758 return( "TLSv1.0" );
7759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007761 return( "TLSv1.1" );
7762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007764 return( "TLSv1.2" );
7765
Paul Bakker43ca69c2011-01-15 17:35:19 +00007766 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007767 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007768 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007769}
7770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007772{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007773 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007775 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007776
Hanno Becker78640902018-08-13 16:35:15 +01007777 if( transform == NULL )
7778 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780#if defined(MBEDTLS_ZLIB_SUPPORT)
7781 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7782 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007783#endif
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 case MBEDTLS_MODE_GCM:
7788 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007789 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007791 transform_expansion = transform->minlen;
7792 break;
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007795
7796 block_size = mbedtls_cipher_get_block_size(
7797 &transform->cipher_ctx_enc );
7798
7799#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7800 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7801 {
7802 /* Expansion due to addition of
7803 * - MAC
7804 * - CBC padding (theoretically up to 256 bytes, but
7805 * we never use more than block_size)
7806 * - explicit IV
7807 */
7808 transform_expansion = transform->maclen + 2 * block_size;
7809 }
7810 else
7811#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7812 {
7813 /* No explicit IV prior to TLS 1.1. */
7814 transform_expansion = transform->maclen + block_size;
7815 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007816 break;
7817
7818 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007821 }
7822
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007823 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007824}
7825
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007826#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7827size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7828{
7829 size_t max_len;
7830
7831 /*
7832 * Assume mfl_code is correct since it was checked when set
7833 */
Angus Grattond8213d02016-05-25 20:56:48 +10007834 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007835
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007836 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007837 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007838 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007839 {
Angus Grattond8213d02016-05-25 20:56:48 +10007840 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007841 }
7842
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007843 /* During a handshake, use the value being negotiated */
7844 if( ssl->session_negotiate != NULL &&
7845 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7846 {
7847 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7848 }
7849
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007850 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007851}
7852#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7853
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007854#if defined(MBEDTLS_SSL_PROTO_DTLS)
7855static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7856{
7857 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7858 return( ssl->mtu );
7859
7860 if( ssl->mtu == 0 )
7861 return( ssl->handshake->mtu );
7862
7863 return( ssl->mtu < ssl->handshake->mtu ?
7864 ssl->mtu : ssl->handshake->mtu );
7865}
7866#endif /* MBEDTLS_SSL_PROTO_DTLS */
7867
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007868int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7869{
7870 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7871
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007872#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7873 !defined(MBEDTLS_SSL_PROTO_DTLS)
7874 (void) ssl;
7875#endif
7876
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007877#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7878 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7879
7880 if( max_len > mfl )
7881 max_len = mfl;
7882#endif
7883
7884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007885 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007886 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007887 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007888 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7889 const size_t overhead = (size_t) ret;
7890
7891 if( ret < 0 )
7892 return( ret );
7893
7894 if( mtu <= overhead )
7895 {
7896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7897 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7898 }
7899
7900 if( max_len > mtu - overhead )
7901 max_len = mtu - overhead;
7902 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007904
Hanno Becker0defedb2018-08-10 12:35:02 +01007905#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7906 !defined(MBEDTLS_SSL_PROTO_DTLS)
7907 ((void) ssl);
7908#endif
7909
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007910 return( (int) max_len );
7911}
7912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_X509_CRT_PARSE_C)
7914const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007915{
7916 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007917 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007918
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007919 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923#if defined(MBEDTLS_SSL_CLI_C)
7924int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007925{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007926 if( ssl == NULL ||
7927 dst == NULL ||
7928 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007929 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007932 }
7933
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007934 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007937
Paul Bakker5121ce52009-01-03 21:22:43 +00007938/*
Paul Bakker1961b702013-01-25 14:49:24 +01007939 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007944
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007945 if( ssl == NULL || ssl->conf == NULL )
7946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007951#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007953 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007955#endif
7956
Paul Bakker1961b702013-01-25 14:49:24 +01007957 return( ret );
7958}
7959
7960/*
7961 * Perform the SSL handshake
7962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007964{
7965 int ret = 0;
7966
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007967 if( ssl == NULL || ssl->conf == NULL )
7968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007975
7976 if( ret != 0 )
7977 break;
7978 }
7979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007981
7982 return( ret );
7983}
7984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985#if defined(MBEDTLS_SSL_RENEGOTIATION)
7986#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007987/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007988 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007991{
7992 int ret;
7993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007995
7996 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7998 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007999
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008000 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008001 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008003 return( ret );
8004 }
8005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008007
8008 return( 0 );
8009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008011
8012/*
8013 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014 * - any side: calling mbedtls_ssl_renegotiate(),
8015 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8016 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008017 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008018 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008022{
8023 int ret;
8024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008026
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008027 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8028 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008029
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008030 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8031 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008033 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008035 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008036 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008037 ssl->handshake->out_msg_seq = 1;
8038 else
8039 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008040 }
8041#endif
8042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8044 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008049 return( ret );
8050 }
8051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008053
8054 return( 0 );
8055}
8056
8057/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008058 * Renegotiate current connection on client,
8059 * or request renegotiation on server
8060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008064
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008065 if( ssl == NULL || ssl->conf == NULL )
8066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008069 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008070 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008076
8077 /* Did we already try/start sending HelloRequest? */
8078 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008080
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008081 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008082 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008086 /*
8087 * On client, either start the renegotiation process or,
8088 * if already in progress, continue the handshake
8089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008094
8095 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008098 return( ret );
8099 }
8100 }
8101 else
8102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008106 return( ret );
8107 }
8108 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008110
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008111 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008112}
8113
8114/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008115 * Check record counters and renegotiate if they're above the limit.
8116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008118{
Andres AG2196c7f2016-12-15 17:01:16 +00008119 size_t ep_len = ssl_ep_len( ssl );
8120 int in_ctr_cmp;
8121 int out_ctr_cmp;
8122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8124 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008125 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008126 {
8127 return( 0 );
8128 }
8129
Andres AG2196c7f2016-12-15 17:01:16 +00008130 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8131 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008132 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008133 ssl->conf->renego_period + ep_len, 8 - ep_len );
8134
8135 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008136 {
8137 return( 0 );
8138 }
8139
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008144
8145/*
8146 * Receive application data decrypted from the SSL layer
8147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008149{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008150 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008151 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008152
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008153 if( ssl == NULL || ssl->conf == NULL )
8154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008159 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008162 return( ret );
8163
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008164 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008166 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008167 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008168 return( ret );
8169 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008170 }
8171#endif
8172
Hanno Becker4a810fb2017-05-24 16:27:30 +01008173 /*
8174 * Check if renegotiation is necessary and/or handshake is
8175 * in process. If yes, perform/continue, and fall through
8176 * if an unexpected packet is received while the client
8177 * is waiting for the ServerHello.
8178 *
8179 * (There is no equivalent to the last condition on
8180 * the server-side as it is not treated as within
8181 * a handshake while waiting for the ClientHello
8182 * after a renegotiation request.)
8183 */
8184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008186 ret = ssl_check_ctr_renegotiate( ssl );
8187 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8188 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008191 return( ret );
8192 }
8193#endif
8194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008198 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8199 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008202 return( ret );
8203 }
8204 }
8205
Hanno Beckere41158b2017-10-23 13:30:32 +01008206 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008207 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008208 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008209 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008210 if( ssl->f_get_timer != NULL &&
8211 ssl->f_get_timer( ssl->p_timer ) == -1 )
8212 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008213 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008214 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008215
Hanno Becker327c93b2018-08-15 13:56:18 +01008216 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008217 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008218 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8219 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008220
Hanno Becker4a810fb2017-05-24 16:27:30 +01008221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8222 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008223 }
8224
8225 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008227 {
8228 /*
8229 * OpenSSL sends empty messages to randomize the IV
8230 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008231 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008234 return( 0 );
8235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008237 return( ret );
8238 }
8239 }
8240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008244
Hanno Becker4a810fb2017-05-24 16:27:30 +01008245 /*
8246 * - For client-side, expect SERVER_HELLO_REQUEST.
8247 * - For server-side, expect CLIENT_HELLO.
8248 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8249 */
8250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008252 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008254 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008257
8258 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008261 {
8262 continue;
8263 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008266 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008267#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008268
Hanno Becker4a810fb2017-05-24 16:27:30 +01008269#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008274
8275 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008278 {
8279 continue;
8280 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008281#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008283 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008284#endif /* MBEDTLS_SSL_SRV_C */
8285
Hanno Becker21df7f92017-10-17 11:03:26 +01008286#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008287 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008288 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8289 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8290 ssl->conf->allow_legacy_renegotiation ==
8291 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8292 {
8293 /*
8294 * Accept renegotiation request
8295 */
Paul Bakker48916f92012-09-16 19:57:18 +00008296
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008297 /* DTLS clients need to know renego is server-initiated */
8298#if defined(MBEDTLS_SSL_PROTO_DTLS)
8299 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8300 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8301 {
8302 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8303 }
8304#endif
8305 ret = ssl_start_renegotiation( ssl );
8306 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8307 ret != 0 )
8308 {
8309 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8310 return( ret );
8311 }
8312 }
8313 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008314#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008315 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008316 /*
8317 * Refuse renegotiation
8318 */
8319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_SSL_PROTO_SSL3)
8323 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008324 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008325 /* SSLv3 does not have a "no_renegotiation" warning, so
8326 we send a fatal alert and abort the connection. */
8327 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8328 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8329 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008330 }
8331 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8333#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8334 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8335 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8338 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8339 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008340 {
8341 return( ret );
8342 }
Paul Bakker48916f92012-09-16 19:57:18 +00008343 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8346 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008350 }
Paul Bakker48916f92012-09-16 19:57:18 +00008351 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008352
Hanno Becker90333da2017-10-10 11:27:13 +01008353 /* At this point, we don't know whether the renegotiation has been
8354 * completed or not. The cases to consider are the following:
8355 * 1) The renegotiation is complete. In this case, no new record
8356 * has been read yet.
8357 * 2) The renegotiation is incomplete because the client received
8358 * an application data record while awaiting the ServerHello.
8359 * 3) The renegotiation is incomplete because the client received
8360 * a non-handshake, non-application data message while awaiting
8361 * the ServerHello.
8362 * In each of these case, looping will be the proper action:
8363 * - For 1), the next iteration will read a new record and check
8364 * if it's application data.
8365 * - For 2), the loop condition isn't satisfied as application data
8366 * is present, hence continue is the same as break
8367 * - For 3), the loop condition is satisfied and read_record
8368 * will re-deliver the message that was held back by the client
8369 * when expecting the ServerHello.
8370 */
8371 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008372 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008373#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008375 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008376 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008377 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008378 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008381 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008383 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008384 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8389 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008392 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008393 }
8394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8398 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008399 }
8400
8401 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008402
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008403 /* We're going to return something now, cancel timer,
8404 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008406 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008407
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008409 /* If we requested renego but received AppData, resend HelloRequest.
8410 * Do it now, after setting in_offt, to avoid taking this branch
8411 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008413 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008415 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008416 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008419 return( ret );
8420 }
8421 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008423#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008424 }
8425
8426 n = ( len < ssl->in_msglen )
8427 ? len : ssl->in_msglen;
8428
8429 memcpy( buf, ssl->in_offt, n );
8430 ssl->in_msglen -= n;
8431
8432 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008433 {
8434 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008435 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008436 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008437 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008438 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008439 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008440 /* more data available */
8441 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008445
Paul Bakker23986e52011-04-24 08:57:21 +00008446 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008447}
8448
8449/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008450 * Send application data to be encrypted by the SSL layer, taking care of max
8451 * fragment length and buffer size.
8452 *
8453 * According to RFC 5246 Section 6.2.1:
8454 *
8455 * Zero-length fragments of Application data MAY be sent as they are
8456 * potentially useful as a traffic analysis countermeasure.
8457 *
8458 * Therefore, it is possible that the input message length is 0 and the
8459 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008460 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008461static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008462 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008463{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008464 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8465 const size_t max_len = (size_t) ret;
8466
8467 if( ret < 0 )
8468 {
8469 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8470 return( ret );
8471 }
8472
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008473 if( len > max_len )
8474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008476 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008479 "maximum fragment length: %d > %d",
8480 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008482 }
8483 else
8484#endif
8485 len = max_len;
8486 }
Paul Bakker887bd502011-06-08 13:10:54 +00008487
Paul Bakker5121ce52009-01-03 21:22:43 +00008488 if( ssl->out_left != 0 )
8489 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008490 /*
8491 * The user has previously tried to send the data and
8492 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8493 * written. In this case, we expect the high-level write function
8494 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008499 return( ret );
8500 }
8501 }
Paul Bakker887bd502011-06-08 13:10:54 +00008502 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008503 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008504 /*
8505 * The user is trying to send a message the first time, so we need to
8506 * copy the data into the internal buffers and setup the data structure
8507 * to keep track of partial writes
8508 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008509 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008511 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008512
Hanno Becker67bc7c32018-08-06 11:33:50 +01008513 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008516 return( ret );
8517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008518 }
8519
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008520 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008521}
8522
8523/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008524 * Write application data, doing 1/n-1 splitting if necessary.
8525 *
8526 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008527 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008528 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008531static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008532 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008533{
8534 int ret;
8535
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008536 if( ssl->conf->cbc_record_splitting ==
8537 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008538 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8540 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8541 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008542 {
8543 return( ssl_write_real( ssl, buf, len ) );
8544 }
8545
8546 if( ssl->split_done == 0 )
8547 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008548 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008549 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008550 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008551 }
8552
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008553 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8554 return( ret );
8555 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008556
8557 return( ret + 1 );
8558}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008560
8561/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008562 * Write application data (public-facing wrapper)
8563 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008564int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008565{
8566 int ret;
8567
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008569
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008570 if( ssl == NULL || ssl->conf == NULL )
8571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8572
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008573#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008574 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8575 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008576 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008577 return( ret );
8578 }
8579#endif
8580
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008581 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008582 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008583 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008584 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008586 return( ret );
8587 }
8588 }
8589
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008590#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008591 ret = ssl_write_split( ssl, buf, len );
8592#else
8593 ret = ssl_write_real( ssl, buf, len );
8594#endif
8595
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008597
8598 return( ret );
8599}
8600
8601/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008602 * Notify the peer that the connection is being closed
8603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008605{
8606 int ret;
8607
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008608 if( ssl == NULL || ssl->conf == NULL )
8609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008612
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008613 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8619 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8620 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008623 return( ret );
8624 }
8625 }
8626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008628
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008629 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008630}
8631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008633{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008634 if( transform == NULL )
8635 return;
8636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008638 deflateEnd( &transform->ctx_deflate );
8639 inflateEnd( &transform->ctx_inflate );
8640#endif
8641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8643 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 mbedtls_md_free( &transform->md_ctx_enc );
8646 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008647
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008648 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008649}
8650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651#if defined(MBEDTLS_X509_CRT_PARSE_C)
8652static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008655
8656 while( cur != NULL )
8657 {
8658 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008660 cur = next;
8661 }
8662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008664
Hanno Becker0271f962018-08-16 13:23:47 +01008665#if defined(MBEDTLS_SSL_PROTO_DTLS)
8666
8667static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8668{
8669 unsigned offset;
8670 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8671
8672 if( hs == NULL )
8673 return;
8674
8675 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008676 ssl_buffering_free_slot( ssl, offset );
8677}
8678
8679static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8680 uint8_t slot )
8681{
8682 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8683 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008684
8685 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8686 return;
8687
Hanno Beckere605b192018-08-21 15:59:07 +01008688 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008689 {
Hanno Beckere605b192018-08-21 15:59:07 +01008690 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8691 mbedtls_free( hs_buf->data );
8692 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008693 }
8694}
8695
8696#endif /* MBEDTLS_SSL_PROTO_DTLS */
8697
Gilles Peskine9b562d52018-04-25 20:32:43 +02008698void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008699{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008700 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8701
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008702 if( handshake == NULL )
8703 return;
8704
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008705#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8706 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8707 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008708 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008709 handshake->async_in_progress = 0;
8710 }
8711#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8712
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008713#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8714 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8715 mbedtls_md5_free( &handshake->fin_md5 );
8716 mbedtls_sha1_free( &handshake->fin_sha1 );
8717#endif
8718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8719#if defined(MBEDTLS_SHA256_C)
8720 mbedtls_sha256_free( &handshake->fin_sha256 );
8721#endif
8722#if defined(MBEDTLS_SHA512_C)
8723 mbedtls_sha512_free( &handshake->fin_sha512 );
8724#endif
8725#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727#if defined(MBEDTLS_DHM_C)
8728 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730#if defined(MBEDTLS_ECDH_C)
8731 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008732#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008733#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008734 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008735#if defined(MBEDTLS_SSL_CLI_C)
8736 mbedtls_free( handshake->ecjpake_cache );
8737 handshake->ecjpake_cache = NULL;
8738 handshake->ecjpake_cache_len = 0;
8739#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008740#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008741
Janos Follath4ae5c292016-02-10 11:27:43 +00008742#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8743 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008744 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008746#endif
8747
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008748#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8749 if( handshake->psk != NULL )
8750 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008751 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008752 mbedtls_free( handshake->psk );
8753 }
8754#endif
8755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8757 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008758 /*
8759 * Free only the linked list wrapper, not the keys themselves
8760 * since the belong to the SNI callback
8761 */
8762 if( handshake->sni_key_cert != NULL )
8763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008765
8766 while( cur != NULL )
8767 {
8768 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008770 cur = next;
8771 }
8772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775#if defined(MBEDTLS_SSL_PROTO_DTLS)
8776 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008777 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008778 ssl_buffering_free( ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01008779 ssl_free_buffered_record( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008780#endif
8781
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008782 mbedtls_platform_zeroize( handshake,
8783 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008784}
8785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008787{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008788 if( session == NULL )
8789 return;
8790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008792 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 mbedtls_x509_crt_free( session->peer_cert );
8795 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008796 }
Paul Bakkered27a042013-04-18 22:46:23 +02008797#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008798
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008799#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008801#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008802
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008803 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008804}
8805
Paul Bakker5121ce52009-01-03 21:22:43 +00008806/*
8807 * Free an SSL context
8808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008810{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008811 if( ssl == NULL )
8812 return;
8813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008815
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008816 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008817 {
Angus Grattond8213d02016-05-25 20:56:48 +10008818 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008820 }
8821
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008822 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008823 {
Angus Grattond8213d02016-05-25 20:56:48 +10008824 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008826 }
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008829 if( ssl->compress_buf != NULL )
8830 {
Angus Grattond8213d02016-05-25 20:56:48 +10008831 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008833 }
8834#endif
8835
Paul Bakker48916f92012-09-16 19:57:18 +00008836 if( ssl->transform )
8837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 mbedtls_ssl_transform_free( ssl->transform );
8839 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008840 }
8841
8842 if( ssl->handshake )
8843 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008844 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8846 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848 mbedtls_free( ssl->handshake );
8849 mbedtls_free( ssl->transform_negotiate );
8850 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008851 }
8852
Paul Bakkerc0463502013-02-14 11:19:38 +01008853 if( ssl->session )
8854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 mbedtls_ssl_session_free( ssl->session );
8856 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008857 }
8858
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008859#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008860 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008861 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008862 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008864 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008865#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8868 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8871 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008872 }
8873#endif
8874
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008875#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008877#endif
8878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008880
Paul Bakker86f04f42013-02-14 11:20:09 +01008881 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008882 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008883}
8884
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008885/*
8886 * Initialze mbedtls_ssl_config
8887 */
8888void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8889{
8890 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8891}
8892
Simon Butcherc97b6972015-12-27 23:48:17 +00008893#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008894static int ssl_preset_default_hashes[] = {
8895#if defined(MBEDTLS_SHA512_C)
8896 MBEDTLS_MD_SHA512,
8897 MBEDTLS_MD_SHA384,
8898#endif
8899#if defined(MBEDTLS_SHA256_C)
8900 MBEDTLS_MD_SHA256,
8901 MBEDTLS_MD_SHA224,
8902#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008903#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008904 MBEDTLS_MD_SHA1,
8905#endif
8906 MBEDTLS_MD_NONE
8907};
Simon Butcherc97b6972015-12-27 23:48:17 +00008908#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008909
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008910static int ssl_preset_suiteb_ciphersuites[] = {
8911 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8912 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8913 0
8914};
8915
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008916#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008917static int ssl_preset_suiteb_hashes[] = {
8918 MBEDTLS_MD_SHA256,
8919 MBEDTLS_MD_SHA384,
8920 MBEDTLS_MD_NONE
8921};
8922#endif
8923
8924#if defined(MBEDTLS_ECP_C)
8925static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8926 MBEDTLS_ECP_DP_SECP256R1,
8927 MBEDTLS_ECP_DP_SECP384R1,
8928 MBEDTLS_ECP_DP_NONE
8929};
8930#endif
8931
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008932/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008933 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008934 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008935int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008936 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008937{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008938#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008939 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008940#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008941
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008942 /* Use the functions here so that they are covered in tests,
8943 * but otherwise access member directly for efficiency */
8944 mbedtls_ssl_conf_endpoint( conf, endpoint );
8945 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008946
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008947 /*
8948 * Things that are common to all presets
8949 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008950#if defined(MBEDTLS_SSL_CLI_C)
8951 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8952 {
8953 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8954#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8955 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8956#endif
8957 }
8958#endif
8959
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008960#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008961 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008962#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008963
8964#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8965 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8966#endif
8967
8968#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8969 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8970#endif
8971
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008972#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8973 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8974#endif
8975
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008976#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008977 conf->f_cookie_write = ssl_cookie_write_dummy;
8978 conf->f_cookie_check = ssl_cookie_check_dummy;
8979#endif
8980
8981#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8982 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8983#endif
8984
Janos Follath088ce432017-04-10 12:42:31 +01008985#if defined(MBEDTLS_SSL_SRV_C)
8986 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8987#endif
8988
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008989#if defined(MBEDTLS_SSL_PROTO_DTLS)
8990 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8991 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8992#endif
8993
8994#if defined(MBEDTLS_SSL_RENEGOTIATION)
8995 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008996 memset( conf->renego_period, 0x00, 2 );
8997 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008998#endif
8999
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009000#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9001 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9002 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009003 const unsigned char dhm_p[] =
9004 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9005 const unsigned char dhm_g[] =
9006 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9007
Hanno Beckera90658f2017-10-04 15:29:08 +01009008 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9009 dhm_p, sizeof( dhm_p ),
9010 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009011 {
9012 return( ret );
9013 }
9014 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009015#endif
9016
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009017 /*
9018 * Preset-specific defaults
9019 */
9020 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009021 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009022 /*
9023 * NSA Suite B
9024 */
9025 case MBEDTLS_SSL_PRESET_SUITEB:
9026 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9027 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9028 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9029 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9030
9031 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9032 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9033 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9034 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9035 ssl_preset_suiteb_ciphersuites;
9036
9037#if defined(MBEDTLS_X509_CRT_PARSE_C)
9038 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009039#endif
9040
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009041#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009042 conf->sig_hashes = ssl_preset_suiteb_hashes;
9043#endif
9044
9045#if defined(MBEDTLS_ECP_C)
9046 conf->curve_list = ssl_preset_suiteb_curves;
9047#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009048 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009049
9050 /*
9051 * Default
9052 */
9053 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009054 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9055 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9056 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9057 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9058 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9059 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9060 MBEDTLS_SSL_MIN_MINOR_VERSION :
9061 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009062 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9063 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9064
9065#if defined(MBEDTLS_SSL_PROTO_DTLS)
9066 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9067 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9068#endif
9069
9070 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9071 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9072 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9073 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9074 mbedtls_ssl_list_ciphersuites();
9075
9076#if defined(MBEDTLS_X509_CRT_PARSE_C)
9077 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9078#endif
9079
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009080#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009081 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009082#endif
9083
9084#if defined(MBEDTLS_ECP_C)
9085 conf->curve_list = mbedtls_ecp_grp_id_list();
9086#endif
9087
9088#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9089 conf->dhm_min_bitlen = 1024;
9090#endif
9091 }
9092
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009093 return( 0 );
9094}
9095
9096/*
9097 * Free mbedtls_ssl_config
9098 */
9099void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9100{
9101#if defined(MBEDTLS_DHM_C)
9102 mbedtls_mpi_free( &conf->dhm_P );
9103 mbedtls_mpi_free( &conf->dhm_G );
9104#endif
9105
9106#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9107 if( conf->psk != NULL )
9108 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009109 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009110 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009111 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009112 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009113 }
9114
9115 if( conf->psk_identity != NULL )
9116 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009117 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009118 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009119 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009120 conf->psk_identity_len = 0;
9121 }
9122#endif
9123
9124#if defined(MBEDTLS_X509_CRT_PARSE_C)
9125 ssl_key_cert_free( conf->key_cert );
9126#endif
9127
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009128 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009129}
9130
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009131#if defined(MBEDTLS_PK_C) && \
9132 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009133/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138#if defined(MBEDTLS_RSA_C)
9139 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9140 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#if defined(MBEDTLS_ECDSA_C)
9143 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9144 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009147}
9148
Hanno Becker7e5437a2017-04-28 17:15:26 +01009149unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9150{
9151 switch( type ) {
9152 case MBEDTLS_PK_RSA:
9153 return( MBEDTLS_SSL_SIG_RSA );
9154 case MBEDTLS_PK_ECDSA:
9155 case MBEDTLS_PK_ECKEY:
9156 return( MBEDTLS_SSL_SIG_ECDSA );
9157 default:
9158 return( MBEDTLS_SSL_SIG_ANON );
9159 }
9160}
9161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009163{
9164 switch( sig )
9165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166#if defined(MBEDTLS_RSA_C)
9167 case MBEDTLS_SSL_SIG_RSA:
9168 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170#if defined(MBEDTLS_ECDSA_C)
9171 case MBEDTLS_SSL_SIG_ECDSA:
9172 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009173#endif
9174 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009176 }
9177}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009178#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009179
Hanno Becker7e5437a2017-04-28 17:15:26 +01009180#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9181 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9182
9183/* Find an entry in a signature-hash set matching a given hash algorithm. */
9184mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9185 mbedtls_pk_type_t sig_alg )
9186{
9187 switch( sig_alg )
9188 {
9189 case MBEDTLS_PK_RSA:
9190 return( set->rsa );
9191 case MBEDTLS_PK_ECDSA:
9192 return( set->ecdsa );
9193 default:
9194 return( MBEDTLS_MD_NONE );
9195 }
9196}
9197
9198/* Add a signature-hash-pair to a signature-hash set */
9199void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9200 mbedtls_pk_type_t sig_alg,
9201 mbedtls_md_type_t md_alg )
9202{
9203 switch( sig_alg )
9204 {
9205 case MBEDTLS_PK_RSA:
9206 if( set->rsa == MBEDTLS_MD_NONE )
9207 set->rsa = md_alg;
9208 break;
9209
9210 case MBEDTLS_PK_ECDSA:
9211 if( set->ecdsa == MBEDTLS_MD_NONE )
9212 set->ecdsa = md_alg;
9213 break;
9214
9215 default:
9216 break;
9217 }
9218}
9219
9220/* Allow exactly one hash algorithm for each signature. */
9221void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9222 mbedtls_md_type_t md_alg )
9223{
9224 set->rsa = md_alg;
9225 set->ecdsa = md_alg;
9226}
9227
9228#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9229 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9230
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009231/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009232 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009235{
9236 switch( hash )
9237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238#if defined(MBEDTLS_MD5_C)
9239 case MBEDTLS_SSL_HASH_MD5:
9240 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009241#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242#if defined(MBEDTLS_SHA1_C)
9243 case MBEDTLS_SSL_HASH_SHA1:
9244 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246#if defined(MBEDTLS_SHA256_C)
9247 case MBEDTLS_SSL_HASH_SHA224:
9248 return( MBEDTLS_MD_SHA224 );
9249 case MBEDTLS_SSL_HASH_SHA256:
9250 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009252#if defined(MBEDTLS_SHA512_C)
9253 case MBEDTLS_SSL_HASH_SHA384:
9254 return( MBEDTLS_MD_SHA384 );
9255 case MBEDTLS_SSL_HASH_SHA512:
9256 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009257#endif
9258 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009260 }
9261}
9262
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009263/*
9264 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9265 */
9266unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9267{
9268 switch( md )
9269 {
9270#if defined(MBEDTLS_MD5_C)
9271 case MBEDTLS_MD_MD5:
9272 return( MBEDTLS_SSL_HASH_MD5 );
9273#endif
9274#if defined(MBEDTLS_SHA1_C)
9275 case MBEDTLS_MD_SHA1:
9276 return( MBEDTLS_SSL_HASH_SHA1 );
9277#endif
9278#if defined(MBEDTLS_SHA256_C)
9279 case MBEDTLS_MD_SHA224:
9280 return( MBEDTLS_SSL_HASH_SHA224 );
9281 case MBEDTLS_MD_SHA256:
9282 return( MBEDTLS_SSL_HASH_SHA256 );
9283#endif
9284#if defined(MBEDTLS_SHA512_C)
9285 case MBEDTLS_MD_SHA384:
9286 return( MBEDTLS_SSL_HASH_SHA384 );
9287 case MBEDTLS_MD_SHA512:
9288 return( MBEDTLS_SSL_HASH_SHA512 );
9289#endif
9290 default:
9291 return( MBEDTLS_SSL_HASH_NONE );
9292 }
9293}
9294
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009295#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009296/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009297 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009298 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009299 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009300int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009303
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009304 if( ssl->conf->curve_list == NULL )
9305 return( -1 );
9306
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009307 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009308 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009309 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009310
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009311 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009312}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009313#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009314
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009315#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009316/*
9317 * Check if a hash proposed by the peer is in our list.
9318 * Return 0 if we're willing to use it, -1 otherwise.
9319 */
9320int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9321 mbedtls_md_type_t md )
9322{
9323 const int *cur;
9324
9325 if( ssl->conf->sig_hashes == NULL )
9326 return( -1 );
9327
9328 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9329 if( *cur == (int) md )
9330 return( 0 );
9331
9332 return( -1 );
9333}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009334#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336#if defined(MBEDTLS_X509_CRT_PARSE_C)
9337int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9338 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009339 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009340 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009341{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009342 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009344 int usage = 0;
9345#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009347 const char *ext_oid;
9348 size_t ext_len;
9349#endif
9350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9352 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009353 ((void) cert);
9354 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009355 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009356#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9359 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009360 {
9361 /* Server part of the key exchange */
9362 switch( ciphersuite->key_exchange )
9363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 case MBEDTLS_KEY_EXCHANGE_RSA:
9365 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009366 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009367 break;
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9370 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9371 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9372 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009373 break;
9374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9376 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009377 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009378 break;
9379
9380 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381 case MBEDTLS_KEY_EXCHANGE_NONE:
9382 case MBEDTLS_KEY_EXCHANGE_PSK:
9383 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9384 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009385 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009386 usage = 0;
9387 }
9388 }
9389 else
9390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9392 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009393 }
9394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009396 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009397 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009398 ret = -1;
9399 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009400#else
9401 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9405 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9408 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009409 }
9410 else
9411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9413 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009414 }
9415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009417 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009418 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009419 ret = -1;
9420 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009422
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009423 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009426
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009427/*
9428 * Convert version numbers to/from wire format
9429 * and, for DTLS, to/from TLS equivalent.
9430 *
9431 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009432 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009433 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9434 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009437 unsigned char ver[2] )
9438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439#if defined(MBEDTLS_SSL_PROTO_DTLS)
9440 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009443 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9444
9445 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9446 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9447 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009448 else
9449#else
9450 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009451#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009452 {
9453 ver[0] = (unsigned char) major;
9454 ver[1] = (unsigned char) minor;
9455 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009456}
9457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009459 const unsigned char ver[2] )
9460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461#if defined(MBEDTLS_SSL_PROTO_DTLS)
9462 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009463 {
9464 *major = 255 - ver[0] + 2;
9465 *minor = 255 - ver[1] + 1;
9466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009468 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9469 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009470 else
9471#else
9472 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009473#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009474 {
9475 *major = ver[0];
9476 *minor = ver[1];
9477 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009478}
9479
Simon Butcher99000142016-10-13 17:21:01 +01009480int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9481{
9482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9483 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9484 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9485
9486 switch( md )
9487 {
9488#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9489#if defined(MBEDTLS_MD5_C)
9490 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009491 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009492#endif
9493#if defined(MBEDTLS_SHA1_C)
9494 case MBEDTLS_SSL_HASH_SHA1:
9495 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9496 break;
9497#endif
9498#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9499#if defined(MBEDTLS_SHA512_C)
9500 case MBEDTLS_SSL_HASH_SHA384:
9501 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9502 break;
9503#endif
9504#if defined(MBEDTLS_SHA256_C)
9505 case MBEDTLS_SSL_HASH_SHA256:
9506 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9507 break;
9508#endif
9509 default:
9510 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9511 }
9512
9513 return 0;
9514#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9515 (void) ssl;
9516 (void) md;
9517
9518 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9519#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9520}
9521
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009522#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9523 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9524int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9525 unsigned char *output,
9526 unsigned char *data, size_t data_len )
9527{
9528 int ret = 0;
9529 mbedtls_md5_context mbedtls_md5;
9530 mbedtls_sha1_context mbedtls_sha1;
9531
9532 mbedtls_md5_init( &mbedtls_md5 );
9533 mbedtls_sha1_init( &mbedtls_sha1 );
9534
9535 /*
9536 * digitally-signed struct {
9537 * opaque md5_hash[16];
9538 * opaque sha_hash[20];
9539 * };
9540 *
9541 * md5_hash
9542 * MD5(ClientHello.random + ServerHello.random
9543 * + ServerParams);
9544 * sha_hash
9545 * SHA(ClientHello.random + ServerHello.random
9546 * + ServerParams);
9547 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009548 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009549 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009551 goto exit;
9552 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009553 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009554 ssl->handshake->randbytes, 64 ) ) != 0 )
9555 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009557 goto exit;
9558 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009559 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009560 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009562 goto exit;
9563 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009564 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009565 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009567 goto exit;
9568 }
9569
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009570 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009571 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009573 goto exit;
9574 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009575 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009576 ssl->handshake->randbytes, 64 ) ) != 0 )
9577 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009579 goto exit;
9580 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009581 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009582 data_len ) ) != 0 )
9583 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009585 goto exit;
9586 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009587 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009588 output + 16 ) ) != 0 )
9589 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009591 goto exit;
9592 }
9593
9594exit:
9595 mbedtls_md5_free( &mbedtls_md5 );
9596 mbedtls_sha1_free( &mbedtls_sha1 );
9597
9598 if( ret != 0 )
9599 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9600 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9601
9602 return( ret );
9603
9604}
9605#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9606 MBEDTLS_SSL_PROTO_TLS1_1 */
9607
9608#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9609 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9610int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009611 unsigned char *hash, size_t *hashlen,
9612 unsigned char *data, size_t data_len,
9613 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009614{
9615 int ret = 0;
9616 mbedtls_md_context_t ctx;
9617 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009618 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009619
9620 mbedtls_md_init( &ctx );
9621
9622 /*
9623 * digitally-signed struct {
9624 * opaque client_random[32];
9625 * opaque server_random[32];
9626 * ServerDHParams params;
9627 * };
9628 */
9629 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9630 {
9631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9632 goto exit;
9633 }
9634 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9635 {
9636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9637 goto exit;
9638 }
9639 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9640 {
9641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9642 goto exit;
9643 }
9644 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9645 {
9646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9647 goto exit;
9648 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009649 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009650 {
9651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9652 goto exit;
9653 }
9654
9655exit:
9656 mbedtls_md_free( &ctx );
9657
9658 if( ret != 0 )
9659 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9660 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9661
9662 return( ret );
9663}
9664#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9665 MBEDTLS_SSL_PROTO_TLS1_2 */
9666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667#endif /* MBEDTLS_SSL_TLS_C */