blob: 0703b6a7bff8897d38b1c3cbf88886cc14518095 [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;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100150
151 /* By the standard (RFC 6066 Sect. 4), the MFL extension
152 * only limits the maximum record payload size, so in theory
153 * we would be allowed to pack multiple records of payload size
154 * MFL into a single datagram. However, this would mean that there's
155 * no way to explicitly communicate MTU restrictions to the peer.
156 *
157 * The following reduction of max_len makes sure that we never
158 * write datagrams larger than MFL + Record Expansion Overhead.
159 */
160 if( max_len <= ssl->out_left )
161 return( 0 );
162
163 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100164#endif
165
166 ret = ssl_get_remaining_space_in_datagram( ssl );
167 if( ret < 0 )
168 return( ret );
169 remaining = (size_t) ret;
170
171 ret = mbedtls_ssl_get_record_expansion( ssl );
172 if( ret < 0 )
173 return( ret );
174 expansion = (size_t) ret;
175
176 if( remaining <= expansion )
177 return( 0 );
178
179 remaining -= expansion;
180 if( remaining >= max_len )
181 remaining = max_len;
182
183 return( (int) remaining );
184}
185
Hanno Becker0271f962018-08-16 13:23:47 +0100186static void ssl_buffering_free( mbedtls_ssl_context *ssl );
187
Hanno Beckere605b192018-08-21 15:59:07 +0100188static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
189 uint8_t slot );
190
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200191/*
192 * Double the retransmit timeout value, within the allowed range,
193 * returning -1 if the maximum value has already been reached.
194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200196{
197 uint32_t new_timeout;
198
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200199 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200 return( -1 );
201
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200202 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
203 * in the following way: after the initial transmission and a first
204 * retransmission, back off to a temporary estimated MTU of 508 bytes.
205 * This value is guaranteed to be deliverable (if not guaranteed to be
206 * delivered) of any compliant IPv4 (and IPv6) network, and should work
207 * on most non-IP stacks too. */
208 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
209 ssl->handshake->mtu = 508;
210
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211 new_timeout = 2 * ssl->handshake->retransmit_timeout;
212
213 /* Avoid arithmetic overflow and range overflow */
214 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200215 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200216 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200217 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200218 }
219
220 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200222 ssl->handshake->retransmit_timeout ) );
223
224 return( 0 );
225}
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200228{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200229 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200231 ssl->handshake->retransmit_timeout ) );
232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200236/*
237 * Convert max_fragment_length codes to length.
238 * RFC 6066 says:
239 * enum{
240 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
241 * } MaxFragmentLength;
242 * and we add 0 -> extension unused
243 */
Angus Grattond8213d02016-05-25 20:56:48 +1000244static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200245{
Angus Grattond8213d02016-05-25 20:56:48 +1000246 switch( mfl )
247 {
248 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
249 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
250 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
251 return 512;
252 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
253 return 1024;
254 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
255 return 2048;
256 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
257 return 4096;
258 default:
259 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
260 }
261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200263
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200264#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_ssl_session_free( dst );
268 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200271 if( src->peer_cert != NULL )
272 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200273 int ret;
274
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200275 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200276 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200277 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200282 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 dst->peer_cert = NULL;
286 return( ret );
287 }
288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200290
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200291#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200292 if( src->ticket != NULL )
293 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200294 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200295 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200296 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200297
298 memcpy( dst->ticket, src->ticket, src->ticket_len );
299 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
302 return( 0 );
303}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200304#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
307int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200308 const unsigned char *key_enc, const unsigned char *key_dec,
309 size_t keylen,
310 const unsigned char *iv_enc, const unsigned char *iv_dec,
311 size_t ivlen,
312 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200313 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
315int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
316int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
317int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
318int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
319#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000320
Paul Bakker5121ce52009-01-03 21:22:43 +0000321/*
322 * Key material generation
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200325static int ssl3_prf( const unsigned char *secret, size_t slen,
326 const char *label,
327 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000328 unsigned char *dstbuf, size_t dlen )
329{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100330 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000331 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200332 mbedtls_md5_context md5;
333 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000334 unsigned char padding[16];
335 unsigned char sha1sum[20];
336 ((void)label);
337
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200338 mbedtls_md5_init( &md5 );
339 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200340
Paul Bakker5f70b252012-09-13 14:23:06 +0000341 /*
342 * SSLv3:
343 * block =
344 * MD5( secret + SHA1( 'A' + secret + random ) ) +
345 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
346 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
347 * ...
348 */
349 for( i = 0; i < dlen / 16; i++ )
350 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200351 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100353 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100354 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100355 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100356 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100357 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100358 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100359 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100360 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100361 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100362 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000363
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100364 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100365 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100366 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100367 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100368 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100369 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100370 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100371 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000372 }
373
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100374exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200375 mbedtls_md5_free( &md5 );
376 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000377
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500378 mbedtls_platform_zeroize( padding, sizeof( padding ) );
379 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000380
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100381 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200386static int tls1_prf( const unsigned char *secret, size_t slen,
387 const char *label,
388 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000389 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000390{
Paul Bakker23986e52011-04-24 08:57:21 +0000391 size_t nb, hs;
392 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200393 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 unsigned char tmp[128];
395 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 const mbedtls_md_info_t *md_info;
397 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100398 int ret;
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 hs = ( slen + 1 ) / 2;
406 S1 = secret;
407 S2 = secret + slen - hs;
408
409 nb = strlen( label );
410 memcpy( tmp + 20, label, nb );
411 memcpy( tmp + 20 + nb, random, rlen );
412 nb += rlen;
413
414 /*
415 * First compute P_md5(secret,label+random)[0..dlen]
416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100421 return( ret );
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
424 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
425 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 for( i = 0; i < dlen; i += 16 )
428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_md_hmac_reset ( &md_ctx );
430 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
431 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_md_hmac_reset ( &md_ctx );
434 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
435 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
438
439 for( j = 0; j < k; j++ )
440 dstbuf[i + j] = h_i[j];
441 }
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100444
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 /*
446 * XOR out with P_sha1(secret,label+random)[0..dlen]
447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100452 return( ret );
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
455 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
456 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458 for( i = 0; i < dlen; i += 20 )
459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_md_hmac_reset ( &md_ctx );
461 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
462 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_md_hmac_reset ( &md_ctx );
465 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
466 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
469
470 for( j = 0; j < k; j++ )
471 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
472 }
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100475
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500476 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
477 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479 return( 0 );
480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
484static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100485 const unsigned char *secret, size_t slen,
486 const char *label,
487 const unsigned char *random, size_t rlen,
488 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000489{
490 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100491 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000492 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
494 const mbedtls_md_info_t *md_info;
495 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100496 int ret;
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100504
505 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000507
508 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100509 memcpy( tmp + md_len, label, nb );
510 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000511 nb += rlen;
512
513 /*
514 * Compute P_<hash>(secret, label + random)[0..dlen]
515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100517 return( ret );
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
520 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
521 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100522
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100523 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_hmac_reset ( &md_ctx );
526 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
527 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_md_hmac_reset ( &md_ctx );
530 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
531 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000532
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100533 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000534
535 for( j = 0; j < k; j++ )
536 dstbuf[i + j] = h_i[j];
537 }
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100540
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500541 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
542 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000543
544 return( 0 );
545}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100548static int tls_prf_sha256( const unsigned char *secret, size_t slen,
549 const char *label,
550 const unsigned char *random, size_t rlen,
551 unsigned char *dstbuf, size_t dlen )
552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100554 label, random, rlen, dstbuf, dlen ) );
555}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200559static int tls_prf_sha384( const unsigned char *secret, size_t slen,
560 const char *label,
561 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000562 unsigned char *dstbuf, size_t dlen )
563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100565 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_SHA512_C */
568#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
573 defined(MBEDTLS_SSL_PROTO_TLS1_1)
574static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200575#endif
Paul Bakker380da532012-04-18 16:10:25 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_SSL_PROTO_SSL3)
578static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
579static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200580#endif
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
583static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
584static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200585#endif
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
588#if defined(MBEDTLS_SHA256_C)
589static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
590static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
591static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200592#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_SHA512_C)
595static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
596static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
597static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000602{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200603 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 unsigned char keyblk[256];
606 unsigned char *key1;
607 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100608 unsigned char *mac_enc;
609 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000610 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200611 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 const mbedtls_cipher_info_t *cipher_info;
613 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_ssl_session *session = ssl->session_negotiate;
616 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
617 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100622 if( cipher_info == NULL )
623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100625 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100627 }
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100630 if( md_info == NULL )
631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100633 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100635 }
636
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000638 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#if defined(MBEDTLS_SSL_PROTO_SSL3)
641 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000642 {
Paul Bakker48916f92012-09-16 19:57:18 +0000643 handshake->tls_prf = ssl3_prf;
644 handshake->calc_verify = ssl_calc_verify_ssl;
645 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200647 else
648#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
650 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651 {
Paul Bakker48916f92012-09-16 19:57:18 +0000652 handshake->tls_prf = tls1_prf;
653 handshake->calc_verify = ssl_calc_verify_tls;
654 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000655 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200656 else
657#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
659#if defined(MBEDTLS_SHA512_C)
660 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
661 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662 {
Paul Bakker48916f92012-09-16 19:57:18 +0000663 handshake->tls_prf = tls_prf_sha384;
664 handshake->calc_verify = ssl_calc_verify_tls_sha384;
665 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000667 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_SHA256_C)
670 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671 {
Paul Bakker48916f92012-09-16 19:57:18 +0000672 handshake->tls_prf = tls_prf_sha256;
673 handshake->calc_verify = ssl_calc_verify_tls_sha256;
674 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000675 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200676 else
677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200682 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000683
684 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 * SSLv3:
686 * master =
687 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
688 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
689 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200690 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200691 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 * master = PRF( premaster, "master secret", randbytes )[0..47]
693 */
Paul Bakker0a597072012-09-25 21:55:46 +0000694 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000697 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
700 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200701 {
702 unsigned char session_hash[48];
703 size_t hash_len;
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200706
707 ssl->handshake->calc_verify( ssl, session_hash );
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
710 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200713 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200715 {
716 hash_len = 48;
717 }
718 else
719#endif
720 hash_len = 32;
721 }
722 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200724 hash_len = 36;
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200727
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100728 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
729 "extended master secret",
730 session_hash, hash_len,
731 session->master, 48 );
732 if( ret != 0 )
733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100735 return( ret );
736 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200737
738 }
739 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200740#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100741 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
742 "master secret",
743 handshake->randbytes, 64,
744 session->master, 48 );
745 if( ret != 0 )
746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100748 return( ret );
749 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200750
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500751 mbedtls_platform_zeroize( handshake->premaster,
752 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 }
754 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757 /*
758 * Swap the client and server random values.
759 */
Paul Bakker48916f92012-09-16 19:57:18 +0000760 memcpy( tmp, handshake->randbytes, 64 );
761 memcpy( handshake->randbytes, tmp + 32, 32 );
762 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500763 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
765 /*
766 * SSLv3:
767 * key block =
768 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
769 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
770 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
771 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
772 * ...
773 *
774 * TLSv1:
775 * key block = PRF( master, "key expansion", randbytes )
776 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100777 ret = handshake->tls_prf( session->master, 48, "key expansion",
778 handshake->randbytes, 64, keyblk, 256 );
779 if( ret != 0 )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100782 return( ret );
783 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
786 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
787 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
788 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
789 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500791 mbedtls_platform_zeroize( handshake->randbytes,
792 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
794 /*
795 * Determine the appropriate key, IV and MAC length.
796 */
Paul Bakker68884e32013-01-07 18:20:04 +0100797
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200798 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200801 cipher_info->mode == MBEDTLS_MODE_CCM ||
802 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000803 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200804 size_t taglen, explicit_ivlen;
805
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200806 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000807 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200808
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200809 /* All modes haves 96-bit IVs;
810 * GCM and CCM has 4 implicit and 8 explicit bytes
811 * ChachaPoly has all 12 bytes implicit
812 */
Paul Bakker68884e32013-01-07 18:20:04 +0100813 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200814 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
815 transform->fixed_ivlen = 12;
816 else
817 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200818
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200819 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
820 taglen = transform->ciphersuite_info->flags &
821 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
822
823
824 /* Minimum length of encrypted record */
825 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
826 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100827 }
828 else
829 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200830 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
832 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200835 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100836 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200838 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000839 mac_key_len = mbedtls_md_get_size( md_info );
840 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200843 /*
844 * If HMAC is to be truncated, we shall keep the leftmost bytes,
845 * (rfc 6066 page 13 or rfc 2104 section 4),
846 * so we only need to adjust the length here.
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000851
852#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
853 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000854 * HMAC implementation which also truncates the key
855 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000856 mac_key_len = transform->maclen;
857#endif
858 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200860
861 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100862 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200864 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200866 transform->minlen = transform->maclen;
867 else
Paul Bakker68884e32013-01-07 18:20:04 +0100868 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200869 /*
870 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100871 * 1. if EtM is in use: one block plus MAC
872 * otherwise: * first multiple of blocklen greater than maclen
873 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
876 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100877 {
878 transform->minlen = transform->maclen
879 + cipher_info->block_size;
880 }
881 else
882#endif
883 {
884 transform->minlen = transform->maclen
885 + cipher_info->block_size
886 - transform->maclen % cipher_info->block_size;
887 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
890 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
891 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200892 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100893 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
896 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
897 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200898 {
899 transform->minlen += transform->ivlen;
900 }
901 else
902#endif
903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200906 }
Paul Bakker68884e32013-01-07 18:20:04 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908 }
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000911 transform->keylen, transform->minlen, transform->ivlen,
912 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
914 /*
915 * Finally setup the cipher contexts, IVs and MAC secrets.
916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200918 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000920 key1 = keyblk + mac_key_len * 2;
921 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Paul Bakker68884e32013-01-07 18:20:04 +0100923 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000924 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000926 /*
927 * This is not used in TLS v1.1.
928 */
Paul Bakker48916f92012-09-16 19:57:18 +0000929 iv_copy_len = ( transform->fixed_ivlen ) ?
930 transform->fixed_ivlen : transform->ivlen;
931 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
932 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000933 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934 }
935 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#endif /* MBEDTLS_SSL_CLI_C */
937#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000940 key1 = keyblk + mac_key_len * 2 + transform->keylen;
941 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
Hanno Becker81c7b182017-11-09 18:39:33 +0000943 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100944 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000946 /*
947 * This is not used in TLS v1.1.
948 */
Paul Bakker48916f92012-09-16 19:57:18 +0000949 iv_copy_len = ( transform->fixed_ivlen ) ?
950 transform->fixed_ivlen : transform->ivlen;
951 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
952 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000953 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100955 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100960 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_SSL_PROTO_SSL3)
963 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100964 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000965 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100969 }
970
Hanno Becker81c7b182017-11-09 18:39:33 +0000971 memcpy( transform->mac_enc, mac_enc, mac_key_len );
972 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100973 }
974 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#endif /* MBEDTLS_SSL_PROTO_SSL3 */
976#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
977 defined(MBEDTLS_SSL_PROTO_TLS1_2)
978 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100979 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100980 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
981 For AEAD-based ciphersuites, there is nothing to do here. */
982 if( mac_key_len != 0 )
983 {
984 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
985 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
986 }
Paul Bakker68884e32013-01-07 18:20:04 +0100987 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200988 else
989#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
992 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200993 }
Paul Bakker68884e32013-01-07 18:20:04 +0100994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
996 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000997 {
998 int ret = 0;
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001003 transform->iv_enc, transform->iv_dec,
1004 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001005 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001006 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1009 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001010 }
1011 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001013
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001014#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1015 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001016 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001017 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1018 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001019 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001020 iv_copy_len );
1021 }
1022#endif
1023
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001024 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001025 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001028 return( ret );
1029 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001030
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001031 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001032 cipher_info ) ) != 0 )
1033 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001035 return( ret );
1036 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001039 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001043 return( ret );
1044 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001047 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001051 return( ret );
1052 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_CIPHER_MODE_CBC)
1055 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1058 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001061 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001062 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1065 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001068 return( ret );
1069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001073 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001076 // Initialize compression
1077 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001079 {
Paul Bakker16770332013-10-11 09:59:44 +02001080 if( ssl->compress_buf == NULL )
1081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001083 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001084 if( ssl->compress_buf == NULL )
1085 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001087 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001088 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001089 }
1090 }
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001093
Paul Bakker48916f92012-09-16 19:57:18 +00001094 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1095 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001096
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001097 if( deflateInit( &transform->ctx_deflate,
1098 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001099 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1102 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001103 }
1104 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
1109 return( 0 );
1110}
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_SSL_PROTO_SSL3)
1113void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001115 mbedtls_md5_context md5;
1116 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 unsigned char pad_1[48];
1118 unsigned char pad_2[48];
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001122 mbedtls_md5_init( &md5 );
1123 mbedtls_sha1_init( &sha1 );
1124
1125 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1126 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
Paul Bakker380da532012-04-18 16:10:25 +00001128 memset( pad_1, 0x36, 48 );
1129 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001131 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1132 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1133 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001135 mbedtls_md5_starts_ret( &md5 );
1136 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1137 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1138 mbedtls_md5_update_ret( &md5, hash, 16 );
1139 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001141 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1142 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1143 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001144
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001145 mbedtls_sha1_starts_ret( &sha1 );
1146 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1147 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1148 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1149 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001153
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001154 mbedtls_md5_free( &md5 );
1155 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001156
Paul Bakker380da532012-04-18 16:10:25 +00001157 return;
1158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1162void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001163{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001164 mbedtls_md5_context md5;
1165 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001168
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001169 mbedtls_md5_init( &md5 );
1170 mbedtls_sha1_init( &sha1 );
1171
1172 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1173 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001174
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001175 mbedtls_md5_finish_ret( &md5, hash );
1176 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001180
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001181 mbedtls_md5_free( &md5 );
1182 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001183
Paul Bakker380da532012-04-18 16:10:25 +00001184 return;
1185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1189#if defined(MBEDTLS_SHA256_C)
1190void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001191{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001192 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001193
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001194 mbedtls_sha256_init( &sha256 );
1195
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001197
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001198 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001199 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001203
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001204 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001205
Paul Bakker380da532012-04-18 16:10:25 +00001206 return;
1207}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_SHA512_C)
1211void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001212{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001213 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001214
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001215 mbedtls_sha512_init( &sha512 );
1216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001218
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001219 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001220 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001224
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001225 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001226
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 return;
1228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#endif /* MBEDTLS_SHA512_C */
1230#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1233int 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 +02001234{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001235 unsigned char *p = ssl->handshake->premaster;
1236 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001237 const unsigned char *psk = ssl->conf->psk;
1238 size_t psk_len = ssl->conf->psk_len;
1239
1240 /* If the psk callback was called, use its result */
1241 if( ssl->handshake->psk != NULL )
1242 {
1243 psk = ssl->handshake->psk;
1244 psk_len = ssl->handshake->psk_len;
1245 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001246
1247 /*
1248 * PMS = struct {
1249 * opaque other_secret<0..2^16-1>;
1250 * opaque psk<0..2^16-1>;
1251 * };
1252 * with "other_secret" depending on the particular key exchange
1253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1255 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001256 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001257 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001259
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001260 *(p++) = (unsigned char)( psk_len >> 8 );
1261 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001262
1263 if( end < p || (size_t)( end - p ) < psk_len )
1264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1265
1266 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001267 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001268 }
1269 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1271#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1272 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001273 {
1274 /*
1275 * other_secret already set by the ClientKeyExchange message,
1276 * and is 48 bytes long
1277 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001278 if( end - p < 2 )
1279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1280
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001281 *p++ = 0;
1282 *p++ = 48;
1283 p += 48;
1284 }
1285 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1287#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1288 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001289 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001290 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001291 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001292
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001293 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001295 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001296 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001299 return( ret );
1300 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001301 *(p++) = (unsigned char)( len >> 8 );
1302 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001303 p += len;
1304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001306 }
1307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1309#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1310 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001311 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001312 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001313 size_t zlen;
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001316 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001317 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001320 return( ret );
1321 }
1322
1323 *(p++) = (unsigned char)( zlen >> 8 );
1324 *(p++) = (unsigned char)( zlen );
1325 p += zlen;
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001328 }
1329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001334 }
1335
1336 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001337 if( end - p < 2 )
1338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001339
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001340 *(p++) = (unsigned char)( psk_len >> 8 );
1341 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001342
1343 if( end < p || (size_t)( end - p ) < psk_len )
1344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1345
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001346 memcpy( p, psk, psk_len );
1347 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001348
1349 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1350
1351 return( 0 );
1352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001356/*
1357 * SSLv3.0 MAC functions
1358 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001359#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001360static void ssl_mac( mbedtls_md_context_t *md_ctx,
1361 const unsigned char *secret,
1362 const unsigned char *buf, size_t len,
1363 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001364 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365{
1366 unsigned char header[11];
1367 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001368 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1370 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001371
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001372 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001374 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001375 else
Paul Bakker68884e32013-01-07 18:20:04 +01001376 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
1378 memcpy( header, ctr, 8 );
1379 header[ 8] = (unsigned char) type;
1380 header[ 9] = (unsigned char)( len >> 8 );
1381 header[10] = (unsigned char)( len );
1382
Paul Bakker68884e32013-01-07 18:20:04 +01001383 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 mbedtls_md_starts( md_ctx );
1385 mbedtls_md_update( md_ctx, secret, md_size );
1386 mbedtls_md_update( md_ctx, padding, padlen );
1387 mbedtls_md_update( md_ctx, header, 11 );
1388 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001389 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Paul Bakker68884e32013-01-07 18:20:04 +01001391 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_md_starts( md_ctx );
1393 mbedtls_md_update( md_ctx, secret, md_size );
1394 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001395 mbedtls_md_update( md_ctx, out, md_size );
1396 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1401 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001402 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001403#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001404#endif
1405
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001406/* The function below is only used in the Lucky 13 counter-measure in
1407 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1408#if defined(SSL_SOME_MODES_USE_MAC) && \
1409 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1410 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1411 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1412/* This function makes sure every byte in the memory region is accessed
1413 * (in ascending addresses order) */
1414static void ssl_read_memory( unsigned char *p, size_t len )
1415{
1416 unsigned char acc = 0;
1417 volatile unsigned char force;
1418
1419 for( ; len != 0; p++, len-- )
1420 acc ^= *p;
1421
1422 force = acc;
1423 (void) force;
1424}
1425#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1426
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001427/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001433 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001437 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001441 }
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001446 ssl->out_msg, ssl->out_msglen );
1447
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001449 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001451#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 if( mode == MBEDTLS_MODE_STREAM ||
1453 ( mode == MBEDTLS_MODE_CBC
1454#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1455 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001456#endif
1457 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_PROTO_SSL3)
1460 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001461 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001462 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001463
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001464 ssl_mac( &ssl->transform_out->md_ctx_enc,
1465 ssl->transform_out->mac_enc,
1466 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001467 ssl->out_ctr, ssl->out_msgtype,
1468 mac );
1469
1470 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001471 }
1472 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1475 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1476 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001477 {
Hanno Becker992b6872017-11-09 18:57:39 +00001478 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1481 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1482 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1483 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001484 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001485 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001487
1488 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001489 }
1490 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001491#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001495 }
1496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001498 ssl->out_msg + ssl->out_msglen,
1499 ssl->transform_out->maclen );
1500
1501 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001502 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001503 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001504#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001505
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001506 /*
1507 * Encrypt
1508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1510 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001512 int ret;
1513 size_t olen = 0;
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 "including %d bytes of padding",
1517 ssl->out_msglen, 0 ) );
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001520 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001521 ssl->transform_out->ivlen,
1522 ssl->out_msg, ssl->out_msglen,
1523 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001526 return( ret );
1527 }
1528
1529 if( ssl->out_msglen != olen )
1530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 }
Paul Bakker68884e32013-01-07 18:20:04 +01001535 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001537#if defined(MBEDTLS_GCM_C) || \
1538 defined(MBEDTLS_CCM_C) || \
1539 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001541 mode == MBEDTLS_MODE_CCM ||
1542 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001543 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001544 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001545 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001546 unsigned char *enc_msg;
1547 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001548 unsigned char iv[12];
1549 mbedtls_ssl_transform *transform = ssl->transform_out;
1550 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001552 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001553
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001554 /*
1555 * Prepare additional authenticated data
1556 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001557 memcpy( add_data, ssl->out_ctr, 8 );
1558 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001560 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001561 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1562 add_data[12] = ssl->out_msglen & 0xFF;
1563
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001564 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001565
Paul Bakker68884e32013-01-07 18:20:04 +01001566 /*
1567 * Generate IV
1568 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001569 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1570 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001571 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001572 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1573 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1574 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1575
1576 }
1577 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1578 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001579 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001580 unsigned char i;
1581
1582 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1583
1584 for( i = 0; i < 8; i++ )
1585 iv[i+4] ^= ssl->out_ctr[i];
1586 }
1587 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001588 {
1589 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001592 }
1593
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001594 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1595 iv, transform->ivlen );
1596 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1597 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001598
Paul Bakker68884e32013-01-07 18:20:04 +01001599 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001600 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001601 */
1602 enc_msg = ssl->out_msg;
1603 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001604 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001607 "including 0 bytes of padding",
1608 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001609
Paul Bakker68884e32013-01-07 18:20:04 +01001610 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001611 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001612 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001613 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1614 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001615 add_data, 13,
1616 enc_msg, enc_msglen,
1617 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001618 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001621 return( ret );
1622 }
1623
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001624 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1627 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001628 }
1629
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001630 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001631 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001634 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1637#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001638 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001641 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001643 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644
Paul Bakker48916f92012-09-16 19:57:18 +00001645 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1646 ssl->transform_out->ivlen;
1647 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 padlen = 0;
1649
1650 for( i = 0; i <= padlen; i++ )
1651 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1652
1653 ssl->out_msglen += padlen + 1;
1654
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001655 enc_msglen = ssl->out_msglen;
1656 enc_msg = ssl->out_msg;
1657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001659 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001660 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1661 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001664 {
1665 /*
1666 * Generate IV
1667 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001668 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001669 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001670 if( ret != 0 )
1671 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001672
Paul Bakker92be97b2013-01-02 17:30:03 +01001673 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001674 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001675
1676 /*
1677 * Fix pointer positions and message length with added IV
1678 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001679 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001680 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001681 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001686 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001687 ssl->out_msglen, ssl->transform_out->ivlen,
1688 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001691 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001692 ssl->transform_out->ivlen,
1693 enc_msg, enc_msglen,
1694 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001697 return( ret );
1698 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001699
Paul Bakkercca5b812013-08-31 17:40:26 +02001700 if( enc_msglen != olen )
1701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001704 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1707 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001708 {
1709 /*
1710 * Save IV in SSL3 and TLS1
1711 */
1712 memcpy( ssl->transform_out->iv_enc,
1713 ssl->transform_out->cipher_ctx_enc.iv,
1714 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001715 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001716#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001719 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001720 {
1721 /*
1722 * MAC(MAC_write_key, seq_num +
1723 * TLSCipherText.type +
1724 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001725 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726 * IV + // except for TLS 1.0
1727 * ENC(content + padding + padding_length));
1728 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001729 unsigned char pseudo_hdr[13];
1730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001732
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001733 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1734 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001735 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1736 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1741 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001742 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001744 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001746
1747 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001748 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001749 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001752 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001754 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1757 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001758 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001760 /* Make extra sure authentication was performed, exactly once */
1761 if( auth_done != 1 )
1762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1764 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765 }
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
1769 return( 0 );
1770}
1771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001775 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001776#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001777 size_t padlen = 0, correct = 1;
1778#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001782 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001786 }
1787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001789
Paul Bakker48916f92012-09-16 19:57:18 +00001790 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001793 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 }
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1798 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001799 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001800 int ret;
1801 size_t olen = 0;
1802
Paul Bakker68884e32013-01-07 18:20:04 +01001803 padlen = 0;
1804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001806 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001807 ssl->transform_in->ivlen,
1808 ssl->in_msg, ssl->in_msglen,
1809 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001812 return( ret );
1813 }
1814
1815 if( ssl->in_msglen != olen )
1816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001819 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001820 }
Paul Bakker68884e32013-01-07 18:20:04 +01001821 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001823#if defined(MBEDTLS_GCM_C) || \
1824 defined(MBEDTLS_CCM_C) || \
1825 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001827 mode == MBEDTLS_MODE_CCM ||
1828 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001829 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001830 int ret;
1831 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001832 unsigned char *dec_msg;
1833 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001834 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001835 unsigned char iv[12];
1836 mbedtls_ssl_transform *transform = ssl->transform_in;
1837 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001839 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001840
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001841 /*
1842 * Compute and update sizes
1843 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001844 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001847 "+ taglen (%d)", ssl->in_msglen,
1848 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001850 }
1851 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1852
Paul Bakker68884e32013-01-07 18:20:04 +01001853 dec_msg = ssl->in_msg;
1854 dec_msg_result = ssl->in_msg;
1855 ssl->in_msglen = dec_msglen;
1856
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001857 /*
1858 * Prepare additional authenticated data
1859 */
Paul Bakker68884e32013-01-07 18:20:04 +01001860 memcpy( add_data, ssl->in_ctr, 8 );
1861 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001863 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001864 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1865 add_data[12] = ssl->in_msglen & 0xFF;
1866
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001867 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001868
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001869 /*
1870 * Prepare IV
1871 */
1872 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1873 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001874 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001875 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1876 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001878 }
1879 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1880 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001881 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001882 unsigned char i;
1883
1884 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1885
1886 for( i = 0; i < 8; i++ )
1887 iv[i+4] ^= ssl->in_ctr[i];
1888 }
1889 else
1890 {
1891 /* Reminder if we ever add an AEAD mode with a different size */
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1894 }
1895
1896 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001898
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001899 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001900 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001903 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001904 add_data, 13,
1905 dec_msg, dec_msglen,
1906 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001907 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1912 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001913
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001914 return( ret );
1915 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001916 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001917
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001918 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001922 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001923 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1926#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001927 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 {
Paul Bakker45829992013-01-03 14:52:21 +01001930 /*
1931 * Decrypt and check the padding
1932 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001933 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001934 unsigned char *dec_msg;
1935 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001936 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001937 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001938 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001939
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 /*
Paul Bakker45829992013-01-03 14:52:21 +01001941 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1944 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001945 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001946#endif
Paul Bakker45829992013-01-03 14:52:21 +01001947
1948 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1949 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001952 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1953 ssl->transform_in->ivlen,
1954 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001956 }
1957
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001958 dec_msglen = ssl->in_msglen;
1959 dec_msg = ssl->in_msg;
1960 dec_msg_result = ssl->in_msg;
1961
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001962 /*
1963 * Authenticate before decrypt if enabled
1964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1966 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001967 {
Hanno Becker992b6872017-11-09 18:57:39 +00001968 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001969 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001972
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001973 dec_msglen -= ssl->transform_in->maclen;
1974 ssl->in_msglen -= ssl->transform_in->maclen;
1975
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001976 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1977 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1978 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1979 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1984 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001985 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001986 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001990 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001991 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001992 ssl->transform_in->maclen );
1993
Hanno Becker992b6872017-11-09 18:57:39 +00001994 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1995 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002000 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002001 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002002 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002004
2005 /*
2006 * Check length sanity
2007 */
2008 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002011 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002013 }
2014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002016 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002017 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002020 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002021 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002022 dec_msglen -= ssl->transform_in->ivlen;
2023 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002024
Paul Bakker48916f92012-09-16 19:57:18 +00002025 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002026 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002027 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002031 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002032 ssl->transform_in->ivlen,
2033 dec_msg, dec_msglen,
2034 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002037 return( ret );
2038 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002039
Paul Bakkercca5b812013-08-31 17:40:26 +02002040 if( dec_msglen != olen )
2041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2043 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002044 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2047 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002048 {
2049 /*
2050 * Save IV in SSL3 and TLS1
2051 */
2052 memcpy( ssl->transform_in->iv_dec,
2053 ssl->transform_in->cipher_ctx_dec.iv,
2054 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
2058 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002059
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002060 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002061 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_SSL_DEBUG_ALL)
2064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002065 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002066#endif
Paul Bakker45829992013-01-03 14:52:21 +01002067 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002068 correct = 0;
2069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#if defined(MBEDTLS_SSL_PROTO_SSL3)
2072 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 {
Paul Bakker48916f92012-09-16 19:57:18 +00002074 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_SSL_DEBUG_ALL)
2077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002079 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002080#endif
Paul Bakker45829992013-01-03 14:52:21 +01002081 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 }
2083 }
2084 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2086#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2087 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2088 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 {
2090 /*
Paul Bakker45829992013-01-03 14:52:21 +01002091 * TLSv1+: always check the padding up to the first failure
2092 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002094 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002095 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002096 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002097
Paul Bakker956c9e02013-12-19 14:42:28 +01002098 /*
2099 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002100 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002101 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002102 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002103 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002104 *
2105 * In both cases we reset padding_idx to a safe value (0) to
2106 * prevent out-of-buffer reads.
2107 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002108 correct &= ( padlen <= ssl->in_msglen );
2109 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002110 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002111
2112 padding_idx *= correct;
2113
Angus Grattonb512bc12018-06-19 15:57:50 +10002114 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002115 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002116 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002117 pad_count += real_count *
2118 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2119 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002120
2121 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002124 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002126#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002127 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002129 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2131 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2134 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002135 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002136
2137 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002141 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002146
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002147#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002151
2152 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002153 * Authenticate if not done yet.
2154 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002156#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002157 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002158 {
Hanno Becker992b6872017-11-09 18:57:39 +00002159 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002160
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002161 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002163 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2164 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_SSL_PROTO_SSL3)
2167 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002168 {
2169 ssl_mac( &ssl->transform_in->md_ctx_dec,
2170 ssl->transform_in->mac_dec,
2171 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002172 ssl->in_ctr, ssl->in_msgtype,
2173 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002174 }
2175 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2177#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2178 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2179 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002180 {
2181 /*
2182 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002183 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002184 *
2185 * Known timing attacks:
2186 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2187 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002188 * To compensate for different timings for the MAC calculation
2189 * depending on how much padding was removed (which is determined
2190 * by padlen), process extra_run more blocks through the hash
2191 * function.
2192 *
2193 * The formula in the paper is
2194 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2195 * where L1 is the size of the header plus the decrypted message
2196 * plus CBC padding and L2 is the size of the header plus the
2197 * decrypted message. This is for an underlying hash function
2198 * with 64-byte blocks.
2199 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2200 * correctly. We round down instead of up, so -56 is the correct
2201 * value for our calculations instead of -55.
2202 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002203 * Repeat the formula rather than defining a block_size variable.
2204 * This avoids requiring division by a variable at runtime
2205 * (which would be marginally less efficient and would require
2206 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002207 */
2208 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002209
2210 /*
2211 * The next two sizes are the minimum and maximum values of
2212 * in_msglen over all padlen values.
2213 *
2214 * They're independent of padlen, since we previously did
2215 * in_msglen -= padlen.
2216 *
2217 * Note that max_len + maclen is never more than the buffer
2218 * length, as we previously did in_msglen -= maclen too.
2219 */
2220 const size_t max_len = ssl->in_msglen + padlen;
2221 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2222
Gilles Peskine20b44082018-05-29 14:06:49 +02002223 switch( ssl->transform_in->ciphersuite_info->mac )
2224 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002225#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2226 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002227 case MBEDTLS_MD_MD5:
2228 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002229 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002230 /* 8 bytes of message size, 64-byte compression blocks */
2231 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2232 ( 13 + ssl->in_msglen + 8 ) / 64;
2233 break;
2234#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002235#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002236 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002237 /* 16 bytes of message size, 128-byte compression blocks */
2238 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2239 ( 13 + ssl->in_msglen + 16 ) / 128;
2240 break;
2241#endif
2242 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2245 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002246
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002247 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2250 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2251 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2252 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002254 /* Make sure we access everything even when padlen > 0. This
2255 * makes the synchronisation requirements for just-in-time
2256 * Prime+Probe attacks much tighter and hopefully impractical. */
2257 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002258 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002259
2260 /* Call mbedtls_md_process at least once due to cache attacks
2261 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002262 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002266
2267 /* Make sure we access all the memory that could contain the MAC,
2268 * before we check it in the next code block. This makes the
2269 * synchronisation requirements for just-in-time Prime+Probe
2270 * attacks much tighter and hopefully impractical. */
2271 ssl_read_memory( ssl->in_msg + min_len,
2272 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002273 }
2274 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2276 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002282#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002283 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2284 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2285 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
Hanno Becker992b6872017-11-09 18:57:39 +00002288 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2289 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#if defined(MBEDTLS_SSL_DEBUG_ALL)
2292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002293#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002294 correct = 0;
2295 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002296 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002298 /*
2299 * Finally check the correct flag
2300 */
2301 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002303 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002304#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002305
2306 /* Make extra sure authentication was performed, exactly once */
2307 if( auth_done != 1 )
2308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
2313 if( ssl->in_msglen == 0 )
2314 {
Angus Gratton34817922018-06-19 15:58:22 +10002315#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2316 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2317 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2318 {
2319 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2321 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2322 }
2323#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2324
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 ssl->nb_zero++;
2326
2327 /*
2328 * Three or more empty messages may be a DoS attack
2329 * (excessive CPU consumption).
2330 */
2331 if( ssl->nb_zero > 3 )
2332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 }
2337 }
2338 else
2339 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002342 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002343 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002344 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002345 }
2346 else
2347#endif
2348 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002349 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002350 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2351 if( ++ssl->in_ctr[i - 1] != 0 )
2352 break;
2353
2354 /* The loop goes to its end iff the counter is wrapping */
2355 if( i == ssl_ep_len( ssl ) )
2356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2358 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002359 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002360 }
2361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
2364 return( 0 );
2365}
2366
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002367#undef MAC_NONE
2368#undef MAC_PLAINTEXT
2369#undef MAC_CIPHERTEXT
2370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002372/*
2373 * Compression/decompression functions
2374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002376{
2377 int ret;
2378 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002379 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002381 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002384
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002385 if( len_pre == 0 )
2386 return( 0 );
2387
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 memcpy( msg_pre, ssl->out_msg, len_pre );
2389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391 ssl->out_msglen ) );
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394 ssl->out_msg, ssl->out_msglen );
2395
Paul Bakker48916f92012-09-16 19:57:18 +00002396 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2397 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2398 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002399 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002400
Paul Bakker48916f92012-09-16 19:57:18 +00002401 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002402 if( ret != Z_OK )
2403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2405 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002406 }
2407
Angus Grattond8213d02016-05-25 20:56:48 +10002408 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002409 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002412 ssl->out_msglen ) );
2413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415 ssl->out_msg, ssl->out_msglen );
2416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418
2419 return( 0 );
2420}
2421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002423{
2424 int ret;
2425 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002426 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002428 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002431
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002432 if( len_pre == 0 )
2433 return( 0 );
2434
Paul Bakker2770fbd2012-07-03 13:30:23 +00002435 memcpy( msg_pre, ssl->in_msg, len_pre );
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438 ssl->in_msglen ) );
2439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002441 ssl->in_msg, ssl->in_msglen );
2442
Paul Bakker48916f92012-09-16 19:57:18 +00002443 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2444 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2445 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002446 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002447 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002448
Paul Bakker48916f92012-09-16 19:57:18 +00002449 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002450 if( ret != Z_OK )
2451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2453 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002454 }
2455
Angus Grattond8213d02016-05-25 20:56:48 +10002456 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002457 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002460 ssl->in_msglen ) );
2461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002463 ssl->in_msg, ssl->in_msglen );
2464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002466
2467 return( 0 );
2468}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2472static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_PROTO_DTLS)
2475static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002476{
2477 /* If renegotiation is not enforced, retransmit until we would reach max
2478 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002479 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002480 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002481 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002482 unsigned char doublings = 1;
2483
2484 while( ratio != 0 )
2485 {
2486 ++doublings;
2487 ratio >>= 1;
2488 }
2489
2490 if( ++ssl->renego_records_seen > doublings )
2491 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002493 return( 0 );
2494 }
2495 }
2496
2497 return( ssl_write_hello_request( ssl ) );
2498}
2499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002501
Paul Bakker5121ce52009-01-03 21:22:43 +00002502/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002503 * Fill the input message buffer by appending data to it.
2504 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002505 *
2506 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2507 * available (from this read and/or a previous one). Otherwise, an error code
2508 * is returned (possibly EOF or WANT_READ).
2509 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002510 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2511 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2512 * since we always read a whole datagram at once.
2513 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002514 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002515 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002518{
Paul Bakker23986e52011-04-24 08:57:21 +00002519 int ret;
2520 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002524 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002527 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002529 }
2530
Angus Grattond8213d02016-05-25 20:56:48 +10002531 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002535 }
2536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002540 uint32_t timeout;
2541
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002542 /* Just to be sure */
2543 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2544 {
2545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2546 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2548 }
2549
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002550 /*
2551 * The point is, we need to always read a full datagram at once, so we
2552 * sometimes read more then requested, and handle the additional data.
2553 * It could be the rest of the current record (while fetching the
2554 * header) and/or some other records in the same datagram.
2555 */
2556
2557 /*
2558 * Move to the next record in the already read datagram if applicable
2559 */
2560 if( ssl->next_record_offset != 0 )
2561 {
2562 if( ssl->in_left < ssl->next_record_offset )
2563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002566 }
2567
2568 ssl->in_left -= ssl->next_record_offset;
2569
2570 if( ssl->in_left != 0 )
2571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002573 ssl->next_record_offset ) );
2574 memmove( ssl->in_hdr,
2575 ssl->in_hdr + ssl->next_record_offset,
2576 ssl->in_left );
2577 }
2578
2579 ssl->next_record_offset = 0;
2580 }
2581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002584
2585 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002586 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002587 */
2588 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002591 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002592 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002593
2594 /*
2595 * A record can't be split accross datagrams. If we need to read but
2596 * are not at the beginning of a new record, the caller did something
2597 * wrong.
2598 */
2599 if( ssl->in_left != 0 )
2600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002603 }
2604
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002605 /*
2606 * Don't even try to read if time's out already.
2607 * This avoids by-passing the timer when repeatedly receiving messages
2608 * that will end up being dropped.
2609 */
2610 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002611 {
2612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002613 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002614 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002615 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002616 {
Angus Grattond8213d02016-05-25 20:56:48 +10002617 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002620 timeout = ssl->handshake->retransmit_timeout;
2621 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002622 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002625
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002626 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002627 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2628 timeout );
2629 else
2630 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002633
2634 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002636 }
2637
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002638 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002641 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002644 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002645 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002648 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002649 }
2650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002654 return( ret );
2655 }
2656
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002657 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002660 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002662 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002663 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002666 return( ret );
2667 }
2668
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002669 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002670 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002672 }
2673
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 if( ret < 0 )
2675 return( ret );
2676
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002677 ssl->in_left = ret;
2678 }
2679 else
2680#endif
2681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002683 ssl->in_left, nb_want ) );
2684
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002685 while( ssl->in_left < nb_want )
2686 {
2687 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002688
2689 if( ssl_check_timer( ssl ) != 0 )
2690 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2691 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002692 {
2693 if( ssl->f_recv_timeout != NULL )
2694 {
2695 ret = ssl->f_recv_timeout( ssl->p_bio,
2696 ssl->in_hdr + ssl->in_left, len,
2697 ssl->conf->read_timeout );
2698 }
2699 else
2700 {
2701 ret = ssl->f_recv( ssl->p_bio,
2702 ssl->in_hdr + ssl->in_left, len );
2703 }
2704 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002707 ssl->in_left, nb_want ) );
2708 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002709
2710 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002712
2713 if( ret < 0 )
2714 return( ret );
2715
mohammad160352aecb92018-03-28 23:41:40 -07002716 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002717 {
Darryl Green11999bb2018-03-13 15:22:58 +00002718 MBEDTLS_SSL_DEBUG_MSG( 1,
2719 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002720 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2722 }
2723
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002724 ssl->in_left += ret;
2725 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
2730 return( 0 );
2731}
2732
2733/*
2734 * Flush any data not yet written
2735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002737{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002738 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002739 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002743 if( ssl->f_send == NULL )
2744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002746 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002748 }
2749
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002750 /* Avoid incrementing counter if data is flushed */
2751 if( ssl->out_left == 0 )
2752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002754 return( 0 );
2755 }
2756
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 while( ssl->out_left > 0 )
2758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2760 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Hanno Becker2b1e3542018-08-06 11:19:13 +01002762 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002763 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
2767 if( ret <= 0 )
2768 return( ret );
2769
mohammad160352aecb92018-03-28 23:41:40 -07002770 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002771 {
Darryl Green11999bb2018-03-13 15:22:58 +00002772 MBEDTLS_SSL_DEBUG_MSG( 1,
2773 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002774 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002775 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2776 }
2777
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 ssl->out_left -= ret;
2779 }
2780
Hanno Becker2b1e3542018-08-06 11:19:13 +01002781#if defined(MBEDTLS_SSL_PROTO_DTLS)
2782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2783 {
2784 ssl->out_hdr = ssl->out_buf;
2785 }
2786 else
2787#endif
2788 {
2789 ssl->out_hdr = ssl->out_buf + 8;
2790 }
2791 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
2795 return( 0 );
2796}
2797
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002798/*
2799 * Functions to handle the DTLS retransmission state machine
2800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002802/*
2803 * Append current handshake message to current outgoing flight
2804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002806{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2809 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2810 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002811
2812 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002813 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002814 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002818 }
2819
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002820 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002824 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825 }
2826
2827 /* Copy current handshake message with headers */
2828 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2829 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002830 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831 msg->next = NULL;
2832
2833 /* Append to the current flight */
2834 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002835 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002836 else
2837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002839 while( cur->next != NULL )
2840 cur = cur->next;
2841 cur->next = msg;
2842 }
2843
Hanno Becker3b235902018-08-06 09:54:53 +01002844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002845 return( 0 );
2846}
2847
2848/*
2849 * Free the current flight of handshake messages
2850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 mbedtls_ssl_flight_item *cur = flight;
2854 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002855
2856 while( cur != NULL )
2857 {
2858 next = cur->next;
2859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860 mbedtls_free( cur->p );
2861 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002862
2863 cur = next;
2864 }
2865}
2866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2868static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002869#endif
2870
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002871/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002872 * Swap transform_out and out_ctr with the alternative ones
2873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002877 unsigned char tmp_out_ctr[8];
2878
2879 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002882 return;
2883 }
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002886
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002887 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002888 tmp_transform = ssl->transform_out;
2889 ssl->transform_out = ssl->handshake->alt_transform_out;
2890 ssl->handshake->alt_transform_out = tmp_transform;
2891
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002892 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002893 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2894 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002895 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002896
2897 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002898 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2901 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2906 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002907 }
2908 }
2909#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002910}
2911
2912/*
2913 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002914 */
2915int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2916{
2917 int ret = 0;
2918
2919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2920
2921 ret = mbedtls_ssl_flight_transmit( ssl );
2922
2923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2924
2925 return( ret );
2926}
2927
2928/*
2929 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002930 *
2931 * Need to remember the current message in case flush_output returns
2932 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002933 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002934 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002935int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002936{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002937 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002941 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002943
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002944 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002945 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002946 ssl_swap_epochs( ssl );
2947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002949 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002950
2951 while( ssl->handshake->cur_msg != NULL )
2952 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002953 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002954 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002955
Hanno Beckere1dcb032018-08-17 16:47:58 +01002956 int const is_finished =
2957 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2958 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2959
Hanno Becker04da1892018-08-14 13:22:10 +01002960 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2961 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2962
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002963 /* Swap epochs before sending Finished: we can't do it after
2964 * sending ChangeCipherSpec, in case write returns WANT_READ.
2965 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002966 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002967 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002969 ssl_swap_epochs( ssl );
2970 }
2971
Hanno Becker67bc7c32018-08-06 11:33:50 +01002972 ret = ssl_get_remaining_payload_in_datagram( ssl );
2973 if( ret < 0 )
2974 return( ret );
2975 max_frag_len = (size_t) ret;
2976
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002977 /* CCS is copied as is, while HS messages may need fragmentation */
2978 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2979 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002980 if( max_frag_len == 0 )
2981 {
2982 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2983 return( ret );
2984
2985 continue;
2986 }
2987
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002988 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002989 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002990 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002991
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002992 /* Update position inside current message */
2993 ssl->handshake->cur_msg_p += cur->len;
2994 }
2995 else
2996 {
2997 const unsigned char * const p = ssl->handshake->cur_msg_p;
2998 const size_t hs_len = cur->len - 12;
2999 const size_t frag_off = p - ( cur->p + 12 );
3000 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003001 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003002
Hanno Beckere1dcb032018-08-17 16:47:58 +01003003 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003004 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003005 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003006 ssl_swap_epochs( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003007
3008 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3009 return( ret );
3010
3011 continue;
3012 }
3013 max_hs_frag_len = max_frag_len - 12;
3014
3015 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3016 max_hs_frag_len : rem_len;
3017
3018 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003019 {
3020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003021 (unsigned) cur_hs_frag_len,
3022 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003023 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003024
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003025 /* Messages are stored with handshake headers as if not fragmented,
3026 * copy beginning of headers then fill fragmentation fields.
3027 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3028 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003029
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003030 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3031 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3032 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3033
Hanno Becker67bc7c32018-08-06 11:33:50 +01003034 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3035 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3036 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003037
3038 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3039
Hanno Becker3f7b9732018-08-28 09:53:25 +01003040 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003041 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3042 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003043 ssl->out_msgtype = cur->type;
3044
3045 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003046 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003047 }
3048
3049 /* If done with the current message move to the next one if any */
3050 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3051 {
3052 if( cur->next != NULL )
3053 {
3054 ssl->handshake->cur_msg = cur->next;
3055 ssl->handshake->cur_msg_p = cur->next->p + 12;
3056 }
3057 else
3058 {
3059 ssl->handshake->cur_msg = NULL;
3060 ssl->handshake->cur_msg_p = NULL;
3061 }
3062 }
3063
3064 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003065 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003068 return( ret );
3069 }
3070 }
3071
Hanno Becker67bc7c32018-08-06 11:33:50 +01003072 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3073 return( ret );
3074
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003075 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3077 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003078 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003081 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3082 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003083
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003085
3086 return( 0 );
3087}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003088
3089/*
3090 * To be called when the last message of an incoming flight is received.
3091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003093{
3094 /* We won't need to resend that one any more */
3095 ssl_flight_free( ssl->handshake->flight );
3096 ssl->handshake->flight = NULL;
3097 ssl->handshake->cur_msg = NULL;
3098
3099 /* The next incoming flight will start with this msg_seq */
3100 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3101
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003102 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003103 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003104
Hanno Becker0271f962018-08-16 13:23:47 +01003105 /* Clear future message buffering structure. */
3106 ssl_buffering_free( ssl );
3107
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003108 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003109 ssl_set_timer( ssl, 0 );
3110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3112 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003115 }
3116 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003118}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003119
3120/*
3121 * To be called when the last message of an outgoing flight is send.
3122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003124{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003125 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003126 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3129 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003132 }
3133 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003137
Paul Bakker5121ce52009-01-03 21:22:43 +00003138/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003139 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003141
3142/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003143 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003144 *
3145 * - fill in handshake headers
3146 * - update handshake checksum
3147 * - DTLS: save message for resending
3148 * - then pass to the record layer
3149 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003150 * DTLS: except for HelloRequest, messages are only queued, and will only be
3151 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003152 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003153 * Inputs:
3154 * - ssl->out_msglen: 4 + actual handshake message len
3155 * (4 is the size of handshake headers for TLS)
3156 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3157 * - ssl->out_msg + 4: the handshake message body
3158 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003159 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003160 * - ssl->out_msglen: the length of the record contents
3161 * (including handshake headers but excluding record headers)
3162 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003164int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003165{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003166 int ret;
3167 const size_t hs_len = ssl->out_msglen - 4;
3168 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3171
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003172 /*
3173 * Sanity checks
3174 */
Hanno Becker2c98db22018-08-22 16:05:47 +01003175 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003176 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3177 {
Hanno Becker2c98db22018-08-22 16:05:47 +01003178 /* In SSLv3, the client might send a NoCertificate alert. */
3179#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3180 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3181 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3182 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3183#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3184 {
3185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3187 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003190 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3191 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3192 ssl->handshake == NULL )
3193 {
3194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3196 }
3197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003202 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003205 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003207
Hanno Beckerb50a2532018-08-06 11:52:54 +01003208 /* Double-check that we did not exceed the bounds
3209 * of the outgoing record buffer.
3210 * This should never fail as the various message
3211 * writing functions must obey the bounds of the
3212 * outgoing record buffer, but better be safe.
3213 *
3214 * Note: We deliberately do not check for the MTU or MFL here.
3215 */
3216 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3217 {
3218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3219 "size %u, maximum %u",
3220 (unsigned) ssl->out_msglen,
3221 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3223 }
3224
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003225 /*
3226 * Fill handshake headers
3227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003230 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3231 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3232 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003234 /*
3235 * DTLS has additional fields in the Handshake layer,
3236 * between the length field and the actual payload:
3237 * uint16 message_seq;
3238 * uint24 fragment_offset;
3239 * uint24 fragment_length;
3240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003243 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003244 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003245 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003246 {
3247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3248 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003249 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003250 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3252 }
3253
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003254 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003255 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003256
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003257 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003258 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003259 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003260 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3261 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3262 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003263 }
3264 else
3265 {
3266 ssl->out_msg[4] = 0;
3267 ssl->out_msg[5] = 0;
3268 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003269
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003270 /* Handshake hashes are computed without fragmentation,
3271 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003272 memset( ssl->out_msg + 6, 0x00, 3 );
3273 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003274 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003276
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003277 /* Update running hashes of hanshake messages seen */
3278 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3279 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 }
3281
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003282 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker551835d2018-08-22 16:07:59 +01003285 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3286 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003287 {
3288 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003291 return( ret );
3292 }
3293 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003294 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003295#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003296 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003297 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003298 {
3299 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3300 return( ret );
3301 }
3302 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003303
3304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3305
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003306 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003307}
3308
3309/*
3310 * Record layer functions
3311 */
3312
3313/*
3314 * Write current record.
3315 *
3316 * Uses:
3317 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3318 * - ssl->out_msglen: length of the record content (excl headers)
3319 * - ssl->out_msg: record content
3320 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003321int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003322{
3323 int ret, done = 0;
3324 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003325 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003326
3327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003330 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003332 {
3333 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003336 return( ret );
3337 }
3338
3339 len = ssl->out_msglen;
3340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3344 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 ret = mbedtls_ssl_hw_record_write( ssl );
3349 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3352 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003353 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003354
3355 if( ret == 0 )
3356 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003359 if( !done )
3360 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003361 unsigned i;
3362 size_t protected_record_size;
3363
Paul Bakker05ef8352012-05-08 09:17:57 +00003364 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003366 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003367
Hanno Becker19859472018-08-06 09:40:20 +01003368 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003369 ssl->out_len[0] = (unsigned char)( len >> 8 );
3370 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003371
Paul Bakker48916f92012-09-16 19:57:18 +00003372 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003373 {
3374 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003377 return( ret );
3378 }
3379
3380 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003381 ssl->out_len[0] = (unsigned char)( len >> 8 );
3382 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003383 }
3384
Hanno Becker2b1e3542018-08-06 11:19:13 +01003385 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3386
3387#if defined(MBEDTLS_SSL_PROTO_DTLS)
3388 /* In case of DTLS, double-check that we don't exceed
3389 * the remaining space in the datagram. */
3390 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3391 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003392 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003393 if( ret < 0 )
3394 return( ret );
3395
3396 if( protected_record_size > (size_t) ret )
3397 {
3398 /* Should never happen */
3399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3400 }
3401 }
3402#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003405 "version = [%d:%d], msglen = %d",
3406 ssl->out_hdr[0], ssl->out_hdr[1],
3407 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003410 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003411
3412 ssl->out_left += protected_record_size;
3413 ssl->out_hdr += protected_record_size;
3414 ssl_update_out_pointers( ssl, ssl->transform_out );
3415
Hanno Becker04484622018-08-06 09:49:38 +01003416 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3417 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3418 break;
3419
3420 /* The loop goes to its end iff the counter is wrapping */
3421 if( i == ssl_ep_len( ssl ) )
3422 {
3423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3424 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 }
3427
Hanno Becker67bc7c32018-08-06 11:33:50 +01003428#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003429 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3430 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003431 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003432 size_t remaining;
3433 ret = ssl_get_remaining_payload_in_datagram( ssl );
3434 if( ret < 0 )
3435 {
3436 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3437 ret );
3438 return( ret );
3439 }
3440
3441 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003442 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003443 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003444 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003445 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003446 else
3447 {
Hanno Becker513815a2018-08-20 11:56:09 +01003448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003449 }
3450 }
3451#endif /* MBEDTLS_SSL_PROTO_DTLS */
3452
3453 if( ( flush == SSL_FORCE_FLUSH ) &&
3454 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 return( ret );
3458 }
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003461
3462 return( 0 );
3463}
3464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003466
3467static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3468{
3469 if( ssl->in_msglen < ssl->in_hslen ||
3470 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3471 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3472 {
3473 return( 1 );
3474 }
3475 return( 0 );
3476}
Hanno Becker44650b72018-08-16 12:51:11 +01003477
3478static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context *ssl )
3479{
3480 return( ( ssl->in_msg[9] << 16 ) |
3481 ( ssl->in_msg[10] << 8 ) |
3482 ssl->in_msg[11] );
3483}
3484
3485static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context *ssl )
3486{
3487 return( ( ssl->in_msg[6] << 16 ) |
3488 ( ssl->in_msg[7] << 8 ) |
3489 ssl->in_msg[8] );
3490}
3491
3492static int ssl_check_hs_header( mbedtls_ssl_context *ssl )
3493{
3494 uint32_t msg_len, frag_off, frag_len;
3495
3496 msg_len = ssl_get_hs_total_len( ssl );
3497 frag_off = ssl_get_hs_frag_off( ssl );
3498 frag_len = ssl_get_hs_frag_len( ssl );
3499
3500 if( frag_off > msg_len )
3501 return( -1 );
3502
3503 if( frag_len > msg_len - frag_off )
3504 return( -1 );
3505
3506 if( frag_len + 12 > ssl->in_msglen )
3507 return( -1 );
3508
3509 return( 0 );
3510}
3511
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003512/*
3513 * Mark bits in bitmask (used for DTLS HS reassembly)
3514 */
3515static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3516{
3517 unsigned int start_bits, end_bits;
3518
3519 start_bits = 8 - ( offset % 8 );
3520 if( start_bits != 8 )
3521 {
3522 size_t first_byte_idx = offset / 8;
3523
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003524 /* Special case */
3525 if( len <= start_bits )
3526 {
3527 for( ; len != 0; len-- )
3528 mask[first_byte_idx] |= 1 << ( start_bits - len );
3529
3530 /* Avoid potential issues with offset or len becoming invalid */
3531 return;
3532 }
3533
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003534 offset += start_bits; /* Now offset % 8 == 0 */
3535 len -= start_bits;
3536
3537 for( ; start_bits != 0; start_bits-- )
3538 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3539 }
3540
3541 end_bits = len % 8;
3542 if( end_bits != 0 )
3543 {
3544 size_t last_byte_idx = ( offset + len ) / 8;
3545
3546 len -= end_bits; /* Now len % 8 == 0 */
3547
3548 for( ; end_bits != 0; end_bits-- )
3549 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3550 }
3551
3552 memset( mask + offset / 8, 0xFF, len / 8 );
3553}
3554
3555/*
3556 * Check that bitmask is full
3557 */
3558static int ssl_bitmask_check( unsigned char *mask, size_t len )
3559{
3560 size_t i;
3561
3562 for( i = 0; i < len / 8; i++ )
3563 if( mask[i] != 0xFF )
3564 return( -1 );
3565
3566 for( i = 0; i < len % 8; i++ )
3567 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3568 return( -1 );
3569
3570 return( 0 );
3571}
3572
Hanno Becker56e205e2018-08-16 09:06:12 +01003573/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003574static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003575 unsigned add_bitmap )
Hanno Becker56e205e2018-08-16 09:06:12 +01003576{
3577 size_t alloc_len;
Hanno Becker56e205e2018-08-16 09:06:12 +01003578
3579 alloc_len = 12; /* Handshake header */
3580 alloc_len += msg_len; /* Content buffer */
Hanno Beckerd07df862018-08-16 09:14:58 +01003581
3582 if( add_bitmap )
3583 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Hanno Becker56e205e2018-08-16 09:06:12 +01003584
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003585 return( alloc_len );
Hanno Becker56e205e2018-08-16 09:06:12 +01003586}
3587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003589
Hanno Becker12555c62018-08-16 12:47:53 +01003590static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context *ssl )
3591{
3592 return( ( ssl->in_msg[1] << 16 ) |
3593 ( ssl->in_msg[2] << 8 ) |
3594 ssl->in_msg[3] );
3595}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003596
Simon Butcher99000142016-10-13 17:21:01 +01003597int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003602 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003604 }
3605
Hanno Becker12555c62018-08-16 12:47:53 +01003606 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003609 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003610 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003614 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003615 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003616 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003617
Hanno Becker44650b72018-08-16 12:51:11 +01003618 if( ssl_check_hs_header( ssl ) != 0 )
3619 {
3620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3621 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3622 }
3623
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003624 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003625 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3626 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3627 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3628 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003629 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003630 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3631 {
3632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3633 recv_msg_seq,
3634 ssl->handshake->in_msg_seq ) );
3635 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3636 }
3637
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003638 /* Retransmit only on last message from previous flight, to avoid
3639 * too many retransmissions.
3640 * Besides, No sane server ever retransmits HelloVerifyRequest */
3641 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003645 "message_seq = %d, start_of_flight = %d",
3646 recv_msg_seq,
3647 ssl->handshake->in_flight_start_seq ) );
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003652 return( ret );
3653 }
3654 }
3655 else
3656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003658 "message_seq = %d, expected = %d",
3659 recv_msg_seq,
3660 ssl->handshake->in_msg_seq ) );
3661 }
3662
Hanno Becker90333da2017-10-10 11:27:13 +01003663 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003664 }
3665 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003666
Hanno Becker6d97ef52018-08-16 13:09:04 +01003667 /* Message reassembly is handled alongside buffering of future
3668 * messages; the commonality is that both handshake fragments and
3669 * future messages cannot be forwarded immediately to the handshake
3670 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003671 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003674 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003675 }
3676 }
3677 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003679 /* With TLS we don't handle fragmentation (for now) */
3680 if( ssl->in_msglen < ssl->in_hslen )
3681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3683 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003684 }
3685
Simon Butcher99000142016-10-13 17:21:01 +01003686 return( 0 );
3687}
3688
3689void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3690{
Hanno Becker0271f962018-08-16 13:23:47 +01003691 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003692
Hanno Becker0271f962018-08-16 13:23:47 +01003693 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003694 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003695 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003696 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003697
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003698 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003700 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003701 ssl->handshake != NULL )
3702 {
Hanno Becker0271f962018-08-16 13:23:47 +01003703 unsigned offset;
3704 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003705
Hanno Becker0271f962018-08-16 13:23:47 +01003706 /* Increment handshake sequence number */
3707 hs->in_msg_seq++;
3708
3709 /*
3710 * Clear up handshake buffering and reassembly structure.
3711 */
3712
3713 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003714 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003715
3716 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003717 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3718 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003719 offset++, hs_buf++ )
3720 {
3721 *hs_buf = *(hs_buf + 1);
3722 }
3723
3724 /* Create a fresh last entry */
3725 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003726 }
3727#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003728}
3729
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003730/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003731 * DTLS anti-replay: RFC 6347 4.1.2.6
3732 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003733 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3734 * Bit n is set iff record number in_window_top - n has been seen.
3735 *
3736 * Usually, in_window_top is the last record number seen and the lsb of
3737 * in_window is set. The only exception is the initial state (record number 0
3738 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3741static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003742{
3743 ssl->in_window_top = 0;
3744 ssl->in_window = 0;
3745}
3746
3747static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3748{
3749 return( ( (uint64_t) buf[0] << 40 ) |
3750 ( (uint64_t) buf[1] << 32 ) |
3751 ( (uint64_t) buf[2] << 24 ) |
3752 ( (uint64_t) buf[3] << 16 ) |
3753 ( (uint64_t) buf[4] << 8 ) |
3754 ( (uint64_t) buf[5] ) );
3755}
3756
3757/*
3758 * Return 0 if sequence number is acceptable, -1 otherwise
3759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003761{
3762 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3763 uint64_t bit;
3764
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003765 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003766 return( 0 );
3767
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003768 if( rec_seqnum > ssl->in_window_top )
3769 return( 0 );
3770
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003771 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003772
3773 if( bit >= 64 )
3774 return( -1 );
3775
3776 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3777 return( -1 );
3778
3779 return( 0 );
3780}
3781
3782/*
3783 * Update replay window on new validated record
3784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003786{
3787 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3788
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003789 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003790 return;
3791
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003792 if( rec_seqnum > ssl->in_window_top )
3793 {
3794 /* Update window_top and the contents of the window */
3795 uint64_t shift = rec_seqnum - ssl->in_window_top;
3796
3797 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003798 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003799 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003800 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003801 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003802 ssl->in_window |= 1;
3803 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003804
3805 ssl->in_window_top = rec_seqnum;
3806 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003807 else
3808 {
3809 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003810 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003811
3812 if( bit < 64 ) /* Always true, but be extra sure */
3813 ssl->in_window |= (uint64_t) 1 << bit;
3814 }
3815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003817
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003818#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003819/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003820static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3821
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003822/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003823 * Without any SSL context, check if a datagram looks like a ClientHello with
3824 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003825 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003826 *
3827 * - if cookie is valid, return 0
3828 * - if ClientHello looks superficially valid but cookie is not,
3829 * fill obuf and set olen, then
3830 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3831 * - otherwise return a specific error code
3832 */
3833static int ssl_check_dtls_clihlo_cookie(
3834 mbedtls_ssl_cookie_write_t *f_cookie_write,
3835 mbedtls_ssl_cookie_check_t *f_cookie_check,
3836 void *p_cookie,
3837 const unsigned char *cli_id, size_t cli_id_len,
3838 const unsigned char *in, size_t in_len,
3839 unsigned char *obuf, size_t buf_len, size_t *olen )
3840{
3841 size_t sid_len, cookie_len;
3842 unsigned char *p;
3843
3844 if( f_cookie_write == NULL || f_cookie_check == NULL )
3845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3846
3847 /*
3848 * Structure of ClientHello with record and handshake headers,
3849 * and expected values. We don't need to check a lot, more checks will be
3850 * done when actually parsing the ClientHello - skipping those checks
3851 * avoids code duplication and does not make cookie forging any easier.
3852 *
3853 * 0-0 ContentType type; copied, must be handshake
3854 * 1-2 ProtocolVersion version; copied
3855 * 3-4 uint16 epoch; copied, must be 0
3856 * 5-10 uint48 sequence_number; copied
3857 * 11-12 uint16 length; (ignored)
3858 *
3859 * 13-13 HandshakeType msg_type; (ignored)
3860 * 14-16 uint24 length; (ignored)
3861 * 17-18 uint16 message_seq; copied
3862 * 19-21 uint24 fragment_offset; copied, must be 0
3863 * 22-24 uint24 fragment_length; (ignored)
3864 *
3865 * 25-26 ProtocolVersion client_version; (ignored)
3866 * 27-58 Random random; (ignored)
3867 * 59-xx SessionID session_id; 1 byte len + sid_len content
3868 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3869 * ...
3870 *
3871 * Minimum length is 61 bytes.
3872 */
3873 if( in_len < 61 ||
3874 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3875 in[3] != 0 || in[4] != 0 ||
3876 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3877 {
3878 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3879 }
3880
3881 sid_len = in[59];
3882 if( sid_len > in_len - 61 )
3883 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3884
3885 cookie_len = in[60 + sid_len];
3886 if( cookie_len > in_len - 60 )
3887 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3888
3889 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3890 cli_id, cli_id_len ) == 0 )
3891 {
3892 /* Valid cookie */
3893 return( 0 );
3894 }
3895
3896 /*
3897 * If we get here, we've got an invalid cookie, let's prepare HVR.
3898 *
3899 * 0-0 ContentType type; copied
3900 * 1-2 ProtocolVersion version; copied
3901 * 3-4 uint16 epoch; copied
3902 * 5-10 uint48 sequence_number; copied
3903 * 11-12 uint16 length; olen - 13
3904 *
3905 * 13-13 HandshakeType msg_type; hello_verify_request
3906 * 14-16 uint24 length; olen - 25
3907 * 17-18 uint16 message_seq; copied
3908 * 19-21 uint24 fragment_offset; copied
3909 * 22-24 uint24 fragment_length; olen - 25
3910 *
3911 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3912 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3913 *
3914 * Minimum length is 28.
3915 */
3916 if( buf_len < 28 )
3917 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3918
3919 /* Copy most fields and adapt others */
3920 memcpy( obuf, in, 25 );
3921 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3922 obuf[25] = 0xfe;
3923 obuf[26] = 0xff;
3924
3925 /* Generate and write actual cookie */
3926 p = obuf + 28;
3927 if( f_cookie_write( p_cookie,
3928 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3929 {
3930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3931 }
3932
3933 *olen = p - obuf;
3934
3935 /* Go back and fill length fields */
3936 obuf[27] = (unsigned char)( *olen - 28 );
3937
3938 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3939 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3940 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3941
3942 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3943 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3944
3945 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3946}
3947
3948/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003949 * Handle possible client reconnect with the same UDP quadruplet
3950 * (RFC 6347 Section 4.2.8).
3951 *
3952 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3953 * that looks like a ClientHello.
3954 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003955 * - if the input looks like a ClientHello without cookies,
3956 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003957 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003958 * - if the input looks like a ClientHello with a valid cookie,
3959 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003960 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003961 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003962 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003963 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003964 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3965 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003966 */
3967static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3968{
3969 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003970 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003971
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003972 ret = ssl_check_dtls_clihlo_cookie(
3973 ssl->conf->f_cookie_write,
3974 ssl->conf->f_cookie_check,
3975 ssl->conf->p_cookie,
3976 ssl->cli_id, ssl->cli_id_len,
3977 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003978 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003979
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003980 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3981
3982 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003983 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003984 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003985 * If the error is permanent we'll catch it later,
3986 * if it's not, then hopefully it'll work next time. */
3987 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3988
3989 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003990 }
3991
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003992 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003993 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003994 /* Got a valid cookie, partially reset context */
3995 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3996 {
3997 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3998 return( ret );
3999 }
4000
4001 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004002 }
4003
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004004 return( ret );
4005}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004006#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004007
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004008/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004009 * ContentType type;
4010 * ProtocolVersion version;
4011 * uint16 epoch; // DTLS only
4012 * uint48 sequence_number; // DTLS only
4013 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004014 *
4015 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004016 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004017 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4018 *
4019 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004020 * 1. proceed with the record if this function returns 0
4021 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4022 * 3. return CLIENT_RECONNECT if this function return that value
4023 * 4. drop the whole datagram if this function returns anything else.
4024 * Point 2 is needed when the peer is resending, and we have already received
4025 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004028{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004029 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 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 +02004032
Paul Bakker5121ce52009-01-03 21:22:43 +00004033 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004034 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004035 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004039 ssl->in_msgtype,
4040 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004041
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004042 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4044 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4045 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4046 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004049
4050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004051 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4052 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004053 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4054#endif /* MBEDTLS_SSL_PROTO_DTLS */
4055 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4056 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004059 }
4060
4061 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004062 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4065 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066 }
4067
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004068 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4071 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004072 }
4073
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004074 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004075 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004076 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4079 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004080 }
4081
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004082 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004083 * DTLS-related tests.
4084 * Check epoch before checking length constraint because
4085 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4086 * message gets duplicated before the corresponding Finished message,
4087 * the second ChangeCipherSpec should be discarded because it belongs
4088 * to an old epoch, but not because its length is shorter than
4089 * the minimum record length for packets using the new record transform.
4090 * Note that these two kinds of failures are handled differently,
4091 * as an unexpected record is silently skipped but an invalid
4092 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004093 */
4094#if defined(MBEDTLS_SSL_PROTO_DTLS)
4095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4096 {
4097 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4098
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004099 /* Check epoch (and sequence number) with DTLS */
4100 if( rec_epoch != ssl->in_epoch )
4101 {
4102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4103 "expected %d, received %d",
4104 ssl->in_epoch, rec_epoch ) );
4105
4106#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4107 /*
4108 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4109 * access the first byte of record content (handshake type), as we
4110 * have an active transform (possibly iv_len != 0), so use the
4111 * fact that the record header len is 13 instead.
4112 */
4113 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4114 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4115 rec_epoch == 0 &&
4116 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4117 ssl->in_left > 13 &&
4118 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4119 {
4120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4121 "from the same port" ) );
4122 return( ssl_handle_possible_reconnect( ssl ) );
4123 }
4124 else
4125#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004126 {
4127 /* Consider buffering the record. */
4128 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4129 {
4130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4131 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4132 }
4133
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004134 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004135 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004136 }
4137
4138#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4139 /* Replay detection only works for the current epoch */
4140 if( rec_epoch == ssl->in_epoch &&
4141 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4142 {
4143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4144 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4145 }
4146#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004147
Hanno Becker52c6dc62017-05-26 16:07:36 +01004148 /* Drop unexpected ApplicationData records,
4149 * except at the beginning of renegotiations */
4150 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4151 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4152#if defined(MBEDTLS_SSL_RENEGOTIATION)
4153 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4154 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4155#endif
4156 )
4157 {
4158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4159 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4160 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004161 }
4162#endif /* MBEDTLS_SSL_PROTO_DTLS */
4163
Hanno Becker52c6dc62017-05-26 16:07:36 +01004164
4165 /* Check length against bounds of the current transform and version */
4166 if( ssl->transform_in == NULL )
4167 {
4168 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004169 ssl->in_msglen > 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 }
4175 else
4176 {
4177 if( ssl->in_msglen < ssl->transform_in->minlen )
4178 {
4179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4180 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4181 }
4182
4183#if defined(MBEDTLS_SSL_PROTO_SSL3)
4184 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004185 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004186 {
4187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4188 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4189 }
4190#endif
4191#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4192 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4193 /*
4194 * TLS encrypted messages can have up to 256 bytes of padding
4195 */
4196 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4197 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004198 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004199 {
4200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4201 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4202 }
4203#endif
4204 }
4205
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004206 return( 0 );
4207}
Paul Bakker5121ce52009-01-03 21:22:43 +00004208
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004209/*
4210 * If applicable, decrypt (and decompress) record content
4211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004213{
4214 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4217 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4220 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 ret = mbedtls_ssl_hw_record_read( ssl );
4225 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4228 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004229 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004230
4231 if( ret == 0 )
4232 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004235 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 {
4237 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004240 return( ret );
4241 }
4242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004244 ssl->in_msg, ssl->in_msglen );
4245
Angus Grattond8213d02016-05-25 20:56:48 +10004246 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4249 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 }
4251 }
4252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004254 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004256 {
4257 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004260 return( ret );
4261 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004266 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004269 }
4270#endif
4271
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004272 return( 0 );
4273}
4274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004276
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004277/*
4278 * Read a record.
4279 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004280 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4281 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4282 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004283 */
Hanno Becker1097b342018-08-15 14:09:41 +01004284
4285/* Helper functions for mbedtls_ssl_read_record(). */
4286static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004287static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4288static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004289
Hanno Becker40f50842018-08-15 14:48:01 +01004290#if defined(MBEDTLS_SSL_PROTO_DTLS)
4291static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004292static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004293static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker5f066e72018-08-16 14:56:31 +01004294static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01004295static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl );
4296#endif /* MBEDTLS_SSL_PROTO_DTLS */
4297
Hanno Becker327c93b2018-08-15 13:56:18 +01004298int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004299 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004300{
4301 int ret;
4302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004304
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004305 if( ssl->keep_current_message == 0 )
4306 {
4307 do {
Simon Butcher99000142016-10-13 17:21:01 +01004308
Hanno Becker26994592018-08-15 14:14:59 +01004309 ret = ssl_consume_current_message( ssl );
4310 if( ret != 0 )
4311 return( ret );
4312
Hanno Beckere74d5562018-08-15 14:26:08 +01004313 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004314 {
Hanno Becker40f50842018-08-15 14:48:01 +01004315#if defined(MBEDTLS_SSL_PROTO_DTLS)
4316 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004317
Hanno Becker40f50842018-08-15 14:48:01 +01004318 /* We only check for buffered messages if the
4319 * current datagram is fully consumed. */
4320 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4321 ssl_another_record_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004322 {
Hanno Becker40f50842018-08-15 14:48:01 +01004323 if( ssl_load_buffered_message( ssl ) == 0 )
4324 have_buffered = 1;
4325 }
4326
4327 if( have_buffered == 0 )
4328#endif /* MBEDTLS_SSL_PROTO_DTLS */
4329 {
4330 ret = ssl_get_next_record( ssl );
4331 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4332 continue;
4333
4334 if( ret != 0 )
4335 {
4336 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4337 return( ret );
4338 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004339 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004340 }
4341
4342 ret = mbedtls_ssl_handle_message_type( ssl );
4343
Hanno Becker40f50842018-08-15 14:48:01 +01004344#if defined(MBEDTLS_SSL_PROTO_DTLS)
4345 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4346 {
4347 /* Buffer future message */
4348 ret = ssl_buffer_message( ssl );
4349 if( ret != 0 )
4350 return( ret );
4351
4352 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4353 }
4354#endif /* MBEDTLS_SSL_PROTO_DTLS */
4355
Hanno Becker90333da2017-10-10 11:27:13 +01004356 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4357 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004358
4359 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004360 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004361 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004362 return( ret );
4363 }
4364
Hanno Becker327c93b2018-08-15 13:56:18 +01004365 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004366 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004367 {
4368 mbedtls_ssl_update_handshake_status( ssl );
4369 }
Simon Butcher99000142016-10-13 17:21:01 +01004370 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004371 else
Simon Butcher99000142016-10-13 17:21:01 +01004372 {
Hanno Becker02f59072018-08-15 14:00:24 +01004373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004374 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004375 }
4376
4377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4378
4379 return( 0 );
4380}
4381
Hanno Becker40f50842018-08-15 14:48:01 +01004382#if defined(MBEDTLS_SSL_PROTO_DTLS)
4383static int ssl_another_record_in_datagram( mbedtls_ssl_context *ssl )
4384{
4385 if( ssl->in_left > ssl->next_record_offset )
4386 return( 1 );
4387
4388 return( 0 );
4389}
4390
4391static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4392{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004393 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004394 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004395 int ret = 0;
4396
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004397 if( hs == NULL )
4398 return( -1 );
4399
Hanno Beckere00ae372018-08-20 09:39:42 +01004400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4401
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004402 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4403 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4404 {
4405 /* Check if we have seen a ChangeCipherSpec before.
4406 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004407 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004408 {
4409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4410 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004411 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004412 }
4413
4414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Inject buffered CCS message" ) );
4415 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4416 ssl->in_msglen = 1;
4417 ssl->in_msg[0] = 1;
4418
4419 /* As long as they are equal, the exact value doesn't matter. */
4420 ssl->in_left = 0;
4421 ssl->next_record_offset = 0;
4422
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004423 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004424 goto exit;
4425 }
Hanno Becker37f95322018-08-16 13:55:32 +01004426
Hanno Beckerb8f50142018-08-28 10:01:34 +01004427#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004428 /* Debug only */
4429 {
4430 unsigned offset;
4431 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4432 {
4433 hs_buf = &hs->buffering.hs[offset];
4434 if( hs_buf->is_valid == 1 )
4435 {
4436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4437 hs->in_msg_seq + offset,
4438 hs_buf->is_complete ? "fully" : "partitially" ) );
4439 }
4440 }
4441 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004442#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004443
4444 /* Check if we have buffered and/or fully reassembled the
4445 * next handshake message. */
4446 hs_buf = &hs->buffering.hs[0];
4447 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4448 {
4449 /* Synthesize a record containing the buffered HS message. */
4450 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4451 ( hs_buf->data[2] << 8 ) |
4452 hs_buf->data[3];
4453
4454 /* Double-check that we haven't accidentally buffered
4455 * a message that doesn't fit into the input buffer. */
4456 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4457 {
4458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4460 }
4461
4462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4463 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4464 hs_buf->data, msg_len + 12 );
4465
4466 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4467 ssl->in_hslen = msg_len + 12;
4468 ssl->in_msglen = msg_len + 12;
4469 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4470
4471 ret = 0;
4472 goto exit;
4473 }
4474 else
4475 {
4476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4477 hs->in_msg_seq ) );
4478 }
4479
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004480 ret = -1;
4481
4482exit:
4483
4484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4485 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004486}
4487
Hanno Becker01315ea2018-08-21 17:22:17 +01004488static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004489static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4490 size_t desired )
4491{
4492 int offset;
4493 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4495 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004496
Hanno Becker01315ea2018-08-21 17:22:17 +01004497 /* Get rid of future records epoch first, if such exist. */
4498 ssl_free_buffered_record( ssl );
4499
4500 /* Check if we have enough space available now. */
4501 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4502 hs->buffering.total_bytes_buffered ) )
4503 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004505 return( 0 );
4506 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004507
Hanno Becker4f432ad2018-08-28 10:02:32 +01004508 /* We don't have enough space to buffer the next expected handshake
4509 * message. Remove buffers used for future messages to gain space,
4510 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004511 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4512 offset >= 0; offset-- )
4513 {
4514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4515 offset ) );
4516
Hanno Beckerb309b922018-08-23 13:18:05 +01004517 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004518
4519 /* Check if we have enough space available now. */
4520 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4521 hs->buffering.total_bytes_buffered ) )
4522 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004524 return( 0 );
4525 }
4526 }
4527
4528 return( -1 );
4529}
4530
Hanno Becker40f50842018-08-15 14:48:01 +01004531static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4532{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004533 int ret = 0;
4534 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4535
4536 if( hs == NULL )
4537 return( 0 );
4538
4539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4540
4541 switch( ssl->in_msgtype )
4542 {
4543 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004545
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004546 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004547 break;
4548
4549 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004550 {
4551 unsigned recv_msg_seq_offset;
4552 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4553 mbedtls_ssl_hs_buffer *hs_buf;
4554 size_t msg_len = ssl->in_hslen - 12;
4555
4556 /* We should never receive an old handshake
4557 * message - double-check nonetheless. */
4558 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4559 {
4560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4561 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4562 }
4563
4564 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4565 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4566 {
4567 /* Silently ignore -- message too far in the future */
4568 MBEDTLS_SSL_DEBUG_MSG( 2,
4569 ( "Ignore future HS message with sequence number %u, "
4570 "buffering window %u - %u",
4571 recv_msg_seq, ssl->handshake->in_msg_seq,
4572 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4573
4574 goto exit;
4575 }
4576
4577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4578 recv_msg_seq, recv_msg_seq_offset ) );
4579
4580 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4581
4582 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004583 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004584 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004585 size_t reassembly_buf_sz;
4586
Hanno Becker37f95322018-08-16 13:55:32 +01004587 hs_buf->is_fragmented =
4588 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4589
4590 /* We copy the message back into the input buffer
4591 * after reassembly, so check that it's not too large.
4592 * This is an implementation-specific limitation
4593 * and not one from the standard, hence it is not
4594 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004595 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004596 {
4597 /* Ignore message */
4598 goto exit;
4599 }
4600
Hanno Beckere0b150f2018-08-21 15:51:03 +01004601 /* Check if we have enough space to buffer the message. */
4602 if( hs->buffering.total_bytes_buffered >
4603 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4604 {
4605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4607 }
4608
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004609 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4610 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004611
4612 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4613 hs->buffering.total_bytes_buffered ) )
4614 {
4615 if( recv_msg_seq_offset > 0 )
4616 {
4617 /* If we can't buffer a future message because
4618 * of space limitations -- ignore. */
4619 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",
4620 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4621 (unsigned) hs->buffering.total_bytes_buffered ) );
4622 goto exit;
4623 }
Hanno Beckere1801392018-08-21 16:51:05 +01004624 else
4625 {
4626 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",
4627 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4628 (unsigned) hs->buffering.total_bytes_buffered ) );
4629 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004630
Hanno Beckera02b0b42018-08-21 17:20:27 +01004631 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004632 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
4634 (unsigned) msg_len,
4635 (unsigned) reassembly_buf_sz,
4636 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004637 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004638 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4639 goto exit;
4640 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004641 }
4642
4643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4644 msg_len ) );
4645
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004646 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4647 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004648 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004649 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004650 goto exit;
4651 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004652 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004653
4654 /* Prepare final header: copy msg_type, length and message_seq,
4655 * then add standardised fragment_offset and fragment_length */
4656 memcpy( hs_buf->data, ssl->in_msg, 6 );
4657 memset( hs_buf->data + 6, 0, 3 );
4658 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4659
4660 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004661
4662 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004663 }
4664 else
4665 {
4666 /* Make sure msg_type and length are consistent */
4667 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4668 {
4669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4670 /* Ignore */
4671 goto exit;
4672 }
4673 }
4674
Hanno Becker4422bbb2018-08-20 09:40:19 +01004675 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004676 {
4677 size_t frag_len, frag_off;
4678 unsigned char * const msg = hs_buf->data + 12;
4679
4680 /*
4681 * Check and copy current fragment
4682 */
4683
4684 /* Validation of header fields already done in
4685 * mbedtls_ssl_prepare_handshake_record(). */
4686 frag_off = ssl_get_hs_frag_off( ssl );
4687 frag_len = ssl_get_hs_frag_len( ssl );
4688
4689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4690 frag_off, frag_len ) );
4691 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4692
4693 if( hs_buf->is_fragmented )
4694 {
4695 unsigned char * const bitmask = msg + msg_len;
4696 ssl_bitmask_set( bitmask, frag_off, frag_len );
4697 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4698 msg_len ) == 0 );
4699 }
4700 else
4701 {
4702 hs_buf->is_complete = 1;
4703 }
4704
4705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4706 hs_buf->is_complete ? "" : "not yet " ) );
4707 }
4708
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004709 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004710 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004711
4712 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004713 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004714 break;
4715 }
4716
4717exit:
4718
4719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4720 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004721}
4722#endif /* MBEDTLS_SSL_PROTO_DTLS */
4723
Hanno Becker1097b342018-08-15 14:09:41 +01004724static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004725{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004726 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004727 * Consume last content-layer message and potentially
4728 * update in_msglen which keeps track of the contents'
4729 * consumption state.
4730 *
4731 * (1) Handshake messages:
4732 * Remove last handshake message, move content
4733 * and adapt in_msglen.
4734 *
4735 * (2) Alert messages:
4736 * Consume whole record content, in_msglen = 0.
4737 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004738 * (3) Change cipher spec:
4739 * Consume whole record content, in_msglen = 0.
4740 *
4741 * (4) Application data:
4742 * Don't do anything - the record layer provides
4743 * the application data as a stream transport
4744 * and consumes through mbedtls_ssl_read only.
4745 *
4746 */
4747
4748 /* Case (1): Handshake messages */
4749 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004750 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004751 /* Hard assertion to be sure that no application data
4752 * is in flight, as corrupting ssl->in_msglen during
4753 * ssl->in_offt != NULL is fatal. */
4754 if( ssl->in_offt != NULL )
4755 {
4756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4757 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4758 }
4759
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004760 /*
4761 * Get next Handshake message in the current record
4762 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004763
Hanno Becker4a810fb2017-05-24 16:27:30 +01004764 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004765 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004766 * current handshake content: If DTLS handshake
4767 * fragmentation is used, that's the fragment
4768 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004769 * size here is faulty and should be changed at
4770 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004771 * (2) While it doesn't seem to cause problems, one
4772 * has to be very careful not to assume that in_hslen
4773 * is always <= in_msglen in a sensible communication.
4774 * Again, it's wrong for DTLS handshake fragmentation.
4775 * The following check is therefore mandatory, and
4776 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004777 * Additionally, ssl->in_hslen might be arbitrarily out of
4778 * bounds after handling a DTLS message with an unexpected
4779 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004780 */
4781 if( ssl->in_hslen < ssl->in_msglen )
4782 {
4783 ssl->in_msglen -= ssl->in_hslen;
4784 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4785 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004786
Hanno Becker4a810fb2017-05-24 16:27:30 +01004787 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4788 ssl->in_msg, ssl->in_msglen );
4789 }
4790 else
4791 {
4792 ssl->in_msglen = 0;
4793 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004794
Hanno Becker4a810fb2017-05-24 16:27:30 +01004795 ssl->in_hslen = 0;
4796 }
4797 /* Case (4): Application data */
4798 else if( ssl->in_offt != NULL )
4799 {
4800 return( 0 );
4801 }
4802 /* Everything else (CCS & Alerts) */
4803 else
4804 {
4805 ssl->in_msglen = 0;
4806 }
4807
Hanno Becker1097b342018-08-15 14:09:41 +01004808 return( 0 );
4809}
4810
Hanno Beckere74d5562018-08-15 14:26:08 +01004811static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4812{
4813 if( ssl->in_msglen > 0 )
4814 return( 1 );
4815
4816 return( 0 );
4817}
4818
Hanno Becker5f066e72018-08-16 14:56:31 +01004819#if defined(MBEDTLS_SSL_PROTO_DTLS)
4820
4821static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4822{
4823 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4824 if( hs == NULL )
4825 return;
4826
Hanno Becker01315ea2018-08-21 17:22:17 +01004827 if( hs->buffering.future_record.data != NULL )
4828 {
4829 hs->buffering.total_bytes_buffered -=
4830 hs->buffering.future_record.len;
4831
4832 mbedtls_free( hs->buffering.future_record.data );
4833 hs->buffering.future_record.data = NULL;
4834 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004835}
4836
4837static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4838{
4839 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4840 unsigned char * rec;
4841 size_t rec_len;
4842 unsigned rec_epoch;
4843
4844 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4845 return( 0 );
4846
4847 if( hs == NULL )
4848 return( 0 );
4849
Hanno Becker5f066e72018-08-16 14:56:31 +01004850 rec = hs->buffering.future_record.data;
4851 rec_len = hs->buffering.future_record.len;
4852 rec_epoch = hs->buffering.future_record.epoch;
4853
4854 if( rec == NULL )
4855 return( 0 );
4856
Hanno Becker4cb782d2018-08-20 11:19:05 +01004857 /* Only consider loading future records if the
4858 * input buffer is empty. */
4859 if( ssl_another_record_in_datagram( ssl ) == 1 )
4860 return( 0 );
4861
Hanno Becker5f066e72018-08-16 14:56:31 +01004862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4863
4864 if( rec_epoch != ssl->in_epoch )
4865 {
4866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4867 goto exit;
4868 }
4869
4870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4871
4872 /* Double-check that the record is not too large */
4873 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4874 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4875 {
4876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4878 }
4879
4880 memcpy( ssl->in_hdr, rec, rec_len );
4881 ssl->in_left = rec_len;
4882 ssl->next_record_offset = 0;
4883
4884 ssl_free_buffered_record( ssl );
4885
4886exit:
4887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4888 return( 0 );
4889}
4890
4891static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4892{
4893 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4894 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004895 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004896
4897 /* Don't buffer future records outside handshakes. */
4898 if( hs == NULL )
4899 return( 0 );
4900
4901 /* Only buffer handshake records (we are only interested
4902 * in Finished messages). */
4903 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4904 return( 0 );
4905
4906 /* Don't buffer more than one future epoch record. */
4907 if( hs->buffering.future_record.data != NULL )
4908 return( 0 );
4909
Hanno Becker01315ea2018-08-21 17:22:17 +01004910 /* Don't buffer record if there's not enough buffering space remaining. */
4911 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4912 hs->buffering.total_bytes_buffered ) )
4913 {
4914 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",
4915 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4916 (unsigned) hs->buffering.total_bytes_buffered ) );
4917 return( 0 );
4918 }
4919
Hanno Becker5f066e72018-08-16 14:56:31 +01004920 /* Buffer record */
4921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4922 ssl->in_epoch + 1 ) );
4923 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4924 rec_hdr_len + ssl->in_msglen );
4925
4926 /* ssl_parse_record_header() only considers records
4927 * of the next epoch as candidates for buffering. */
4928 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004929 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004930
4931 hs->buffering.future_record.data =
4932 mbedtls_calloc( 1, hs->buffering.future_record.len );
4933 if( hs->buffering.future_record.data == NULL )
4934 {
4935 /* If we run out of RAM trying to buffer a
4936 * record from the next epoch, just ignore. */
4937 return( 0 );
4938 }
4939
Hanno Becker01315ea2018-08-21 17:22:17 +01004940 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004941
Hanno Becker01315ea2018-08-21 17:22:17 +01004942 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004943 return( 0 );
4944}
4945
4946#endif /* MBEDTLS_SSL_PROTO_DTLS */
4947
Hanno Beckere74d5562018-08-15 14:26:08 +01004948static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004949{
4950 int ret;
4951
Hanno Becker5f066e72018-08-16 14:56:31 +01004952#if defined(MBEDTLS_SSL_PROTO_DTLS)
4953 /* We might have buffered a future record; if so,
4954 * and if the epoch matches now, load it.
4955 * On success, this call will set ssl->in_left to
4956 * the length of the buffered record, so that
4957 * the calls to ssl_fetch_input() below will
4958 * essentially be no-ops. */
4959 ret = ssl_load_buffered_record( ssl );
4960 if( ret != 0 )
4961 return( ret );
4962#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004967 return( ret );
4968 }
4969
4970 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004973 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4974 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004975 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004976 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4977 {
4978 ret = ssl_buffer_future_record( ssl );
4979 if( ret != 0 )
4980 return( ret );
4981
4982 /* Fall through to handling of unexpected records */
4983 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4984 }
4985
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004986 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4987 {
4988 /* Skip unexpected record (but not whole datagram) */
4989 ssl->next_record_offset = ssl->in_msglen
4990 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004991
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4993 "(header)" ) );
4994 }
4995 else
4996 {
4997 /* Skip invalid record and the rest of the datagram */
4998 ssl->next_record_offset = 0;
4999 ssl->in_left = 0;
5000
5001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5002 "(header)" ) );
5003 }
5004
5005 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005006 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005007 }
5008#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005009 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005010 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005011
5012 /*
5013 * Read and optionally decrypt the message contents
5014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5016 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005019 return( ret );
5020 }
5021
5022 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005027 if( ssl->next_record_offset < ssl->in_left )
5028 {
5029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5030 }
5031 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005032 else
5033#endif
5034 ssl->in_left = 0;
5035
5036 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005039 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005040 {
5041 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005042 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5043 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005044 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005045 /* Except when waiting for Finished as a bad mac here
5046 * probably means something went wrong in the handshake
5047 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5048 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5049 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5050 {
5051#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5052 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5053 {
5054 mbedtls_ssl_send_alert_message( ssl,
5055 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5056 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5057 }
5058#endif
5059 return( ret );
5060 }
5061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005063 if( ssl->conf->badmac_limit != 0 &&
5064 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5067 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005068 }
5069#endif
5070
Hanno Becker4a810fb2017-05-24 16:27:30 +01005071 /* As above, invalid records cause
5072 * dismissal of the whole datagram. */
5073
5074 ssl->next_record_offset = 0;
5075 ssl->in_left = 0;
5076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005078 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005079 }
5080
5081 return( ret );
5082 }
5083 else
5084#endif
5085 {
5086 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5088 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 mbedtls_ssl_send_alert_message( ssl,
5091 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5092 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005093 }
5094#endif
5095 return( ret );
5096 }
5097 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005098
Simon Butcher99000142016-10-13 17:21:01 +01005099 return( 0 );
5100}
5101
5102int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5103{
5104 int ret;
5105
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005106 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005107 * Handle particular types of records
5108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 {
Simon Butcher99000142016-10-13 17:21:01 +01005111 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5112 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005113 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005115 }
5116
Hanno Beckere678eaa2018-08-21 14:57:46 +01005117 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005118 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005119 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5122 ssl->in_msglen ) );
5123 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005124 }
5125
Hanno Beckere678eaa2018-08-21 14:57:46 +01005126 if( ssl->in_msg[0] != 1 )
5127 {
5128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5129 ssl->in_msg[0] ) );
5130 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5131 }
5132
5133#if defined(MBEDTLS_SSL_PROTO_DTLS)
5134 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5135 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5136 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5137 {
5138 if( ssl->handshake == NULL )
5139 {
5140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5141 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5142 }
5143
5144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5145 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5146 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005147#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005148 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005151 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005152 if( ssl->in_msglen != 2 )
5153 {
5154 /* Note: Standard allows for more than one 2 byte alert
5155 to be packed in a single message, but Mbed TLS doesn't
5156 currently support this. */
5157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5158 ssl->in_msglen ) );
5159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5160 }
5161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005163 ssl->in_msg[0], ssl->in_msg[1] ) );
5164
5165 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005166 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005171 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005173 }
5174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5176 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5179 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005180 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005181
5182#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5183 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5184 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5185 {
Hanno Becker90333da2017-10-10 11:27:13 +01005186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005187 /* Will be handled when trying to parse ServerHello */
5188 return( 0 );
5189 }
5190#endif
5191
5192#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5193 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5194 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5195 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5196 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5197 {
5198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5199 /* Will be handled in mbedtls_ssl_parse_certificate() */
5200 return( 0 );
5201 }
5202#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5203
5204 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005205 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005206 }
5207
Hanno Beckerc76c6192017-06-06 10:03:17 +01005208#if defined(MBEDTLS_SSL_PROTO_DTLS)
5209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5210 ssl->handshake != NULL &&
5211 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5212 {
5213 ssl_handshake_wrapup_free_hs_transform( ssl );
5214 }
5215#endif
5216
Paul Bakker5121ce52009-01-03 21:22:43 +00005217 return( 0 );
5218}
5219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005221{
5222 int ret;
5223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5225 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5226 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005227 {
5228 return( ret );
5229 }
5230
5231 return( 0 );
5232}
5233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005235 unsigned char level,
5236 unsigned char message )
5237{
5238 int ret;
5239
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005240 if( ssl == NULL || ssl->conf == NULL )
5241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005247 ssl->out_msglen = 2;
5248 ssl->out_msg[0] = level;
5249 ssl->out_msg[1] = message;
5250
Hanno Becker67bc7c32018-08-06 11:33:50 +01005251 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005254 return( ret );
5255 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005257
5258 return( 0 );
5259}
5260
Paul Bakker5121ce52009-01-03 21:22:43 +00005261/*
5262 * Handshake functions
5263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5265 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5266 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5267 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5268 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5269 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5270 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005271/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5279 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005280 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5281 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005284 ssl->state++;
5285 return( 0 );
5286 }
5287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005290}
5291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5299 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005300 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5301 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005304 ssl->state++;
5305 return( 0 );
5306 }
5307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005310}
Gilles Peskinef9828522017-05-03 12:28:43 +02005311
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005312#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005313/* Some certificate support -> implement write and parse */
5314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005318 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 const mbedtls_x509_crt *crt;
5320 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5325 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005326 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5327 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005330 ssl->state++;
5331 return( 0 );
5332 }
5333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005335 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 {
5337 if( ssl->client_auth == 0 )
5338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005340 ssl->state++;
5341 return( 0 );
5342 }
5343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005345 /*
5346 * If using SSLv3 and got no cert, send an Alert message
5347 * (otherwise an empty Certificate message will be sent).
5348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5350 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005351 {
5352 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5354 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5355 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005358 goto write_msg;
5359 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362#endif /* MBEDTLS_SSL_CLI_C */
5363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5369 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005370 }
5371 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005375
5376 /*
5377 * 0 . 0 handshake type
5378 * 1 . 3 handshake length
5379 * 4 . 6 length of all certs
5380 * 7 . 9 length of cert. 1
5381 * 10 . n-1 peer certificate
5382 * n . n+2 length of cert. 2
5383 * n+3 . ... upper level cert, etc.
5384 */
5385 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005387
Paul Bakker29087132010-03-21 21:03:34 +00005388 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005389 {
5390 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005391 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005394 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 }
5397
5398 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5399 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5400 ssl->out_msg[i + 2] = (unsigned char)( n );
5401
5402 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5403 i += n; crt = crt->next;
5404 }
5405
5406 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5407 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5408 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5409
5410 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5412 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005413
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005414#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005415write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005416#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005417
5418 ssl->state++;
5419
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005420 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 return( ret );
5424 }
5425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005427
Paul Bakkered27a042013-04-18 22:46:23 +02005428 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005429}
5430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005434 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005436 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005437 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5442 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005443 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5444 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005447 ssl->state++;
5448 return( 0 );
5449 }
5450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005452 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005453 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5454 {
5455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5456 ssl->state++;
5457 return( 0 );
5458 }
5459
5460#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5461 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5462 authmode = ssl->handshake->sni_authmode;
5463#endif
5464
5465 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5466 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005467 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005468 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 ssl->state++;
5471 return( 0 );
5472 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005473#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005474
Hanno Becker327c93b2018-08-15 13:56:18 +01005475 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005477 /* mbedtls_ssl_read_record may have sent an alert already. We
5478 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005480 return( ret );
5481 }
5482
5483 ssl->state++;
5484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#if defined(MBEDTLS_SSL_SRV_C)
5486#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 /*
5488 * Check if the client sent an empty certificate
5489 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005490 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005493 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5495 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5496 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005499
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005500 /* The client was asked for a certificate but didn't send
5501 one. The client should know what's going on, so we
5502 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005503 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005504 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 return( 0 );
5506 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005508 }
5509 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5513 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005514 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5518 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5519 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5520 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005523
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005524 /* The client was asked for a certificate but didn't send
5525 one. The client should know what's going on, so we
5526 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005527 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005528 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005529 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005530 else
5531 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005532 }
5533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5535 MBEDTLS_SSL_PROTO_TLS1_2 */
5536#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005544 }
5545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5547 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005550 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5551 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005553 }
5554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005556
Paul Bakker5121ce52009-01-03 21:22:43 +00005557 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005559 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005560 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005561
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005562 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005566 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5567 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005569 }
5570
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005571 /* In case we tried to reuse a session but it failed */
5572 if( ssl->session_negotiate->peer_cert != NULL )
5573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5575 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005576 }
5577
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005578 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005580 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005583 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5584 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005585 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005586 }
5587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005589
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005590 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005591
5592 while( i < ssl->in_hslen )
5593 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005594 if ( i + 3 > ssl->in_hslen ) {
5595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5596 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5597 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5598 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 if( ssl->in_msg[i] != 0 )
5601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005603 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5604 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 }
5607
5608 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5609 | (unsigned int) ssl->in_msg[i + 2];
5610 i += 3;
5611
5612 if( n < 128 || i + n > ssl->in_hslen )
5613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005615 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5616 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005618 }
5619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005621 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005622 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005624 case 0: /*ok*/
5625 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5626 /* Ignore certificate with an unknown algorithm: maybe a
5627 prior certificate was already trusted. */
5628 break;
5629
5630 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5631 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5632 goto crt_parse_der_failed;
5633
5634 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5635 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5636 goto crt_parse_der_failed;
5637
5638 default:
5639 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5640 crt_parse_der_failed:
5641 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005643 return( ret );
5644 }
5645
5646 i += n;
5647 }
5648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005650
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005651 /*
5652 * On client, make sure the server cert doesn't change during renego to
5653 * avoid "triple handshake" attack: https://secure-resumption.com/
5654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005656 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005658 {
5659 if( ssl->session->peer_cert == NULL )
5660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005662 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5663 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005665 }
5666
5667 if( ssl->session->peer_cert->raw.len !=
5668 ssl->session_negotiate->peer_cert->raw.len ||
5669 memcmp( ssl->session->peer_cert->raw.p,
5670 ssl->session_negotiate->peer_cert->raw.p,
5671 ssl->session->peer_cert->raw.len ) != 0 )
5672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005674 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5675 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005677 }
5678 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005680
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005681 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005682 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005683 mbedtls_x509_crt *ca_chain;
5684 mbedtls_x509_crl *ca_crl;
5685
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005686#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005687 if( ssl->handshake->sni_ca_chain != NULL )
5688 {
5689 ca_chain = ssl->handshake->sni_ca_chain;
5690 ca_crl = ssl->handshake->sni_ca_crl;
5691 }
5692 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005693#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005694 {
5695 ca_chain = ssl->conf->ca_chain;
5696 ca_crl = ssl->conf->ca_crl;
5697 }
5698
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005699 /*
5700 * Main check: verify certificate
5701 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005702 ret = mbedtls_x509_crt_verify_with_profile(
5703 ssl->session_negotiate->peer_cert,
5704 ca_chain, ca_crl,
5705 ssl->conf->cert_profile,
5706 ssl->hostname,
5707 &ssl->session_negotiate->verify_result,
5708 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005709
5710 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005713 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005714
5715 /*
5716 * Secondary checks: always done, but change 'ret' only if it was 0
5717 */
5718
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005719#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005722
5723 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005725 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005726 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005727 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005730 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005732 }
5733 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005734#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005737 ciphersuite_info,
5738 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005739 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005742 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005744 }
5745
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005746 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5747 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5748 * with details encoded in the verification flags. All other kinds
5749 * of error codes, including those from the user provided f_vrfy
5750 * functions, are treated as fatal and lead to a failure of
5751 * ssl_parse_certificate even if verification was optional. */
5752 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5753 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5754 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5755 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005757 }
5758
5759 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5760 {
5761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5762 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5763 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005764
5765 if( ret != 0 )
5766 {
5767 /* The certificate may have been rejected for several reasons.
5768 Pick one and send the corresponding alert. Which alert to send
5769 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005770 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5771 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5772 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5773 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5774 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5775 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5776 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5777 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5778 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5779 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5780 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5781 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5782 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5783 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5784 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5785 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5786 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5787 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5788 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5789 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005790 else
5791 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5793 alert );
5794 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005795
Hanno Beckere6706e62017-05-15 16:05:15 +01005796#if defined(MBEDTLS_DEBUG_C)
5797 if( ssl->session_negotiate->verify_result != 0 )
5798 {
5799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5800 ssl->session_negotiate->verify_result ) );
5801 }
5802 else
5803 {
5804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5805 }
5806#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005807 }
5808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005810
5811 return( ret );
5812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5814 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5815 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5816 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5817 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5818 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5819 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005822{
5823 int ret;
5824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 ssl->out_msglen = 1;
5829 ssl->out_msg[0] = 1;
5830
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 ssl->state++;
5832
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005833 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005836 return( ret );
5837 }
5838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005840
5841 return( 0 );
5842}
5843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005845{
5846 int ret;
5847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005849
Hanno Becker327c93b2018-08-15 13:56:18 +01005850 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005853 return( ret );
5854 }
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005859 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5860 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005862 }
5863
Hanno Beckere678eaa2018-08-21 14:57:46 +01005864 /* CCS records are only accepted if they have length 1 and content '1',
5865 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005866
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005867 /*
5868 * Switch to our negotiated transform and session parameters for inbound
5869 * data.
5870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005872 ssl->transform_in = ssl->transform_negotiate;
5873 ssl->session_in = ssl->session_negotiate;
5874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005879 ssl_dtls_replay_reset( ssl );
5880#endif
5881
5882 /* Increment epoch */
5883 if( ++ssl->in_epoch == 0 )
5884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005886 /* This is highly unlikely to happen for legitimate reasons, so
5887 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005889 }
5890 }
5891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005893 memset( ssl->in_ctr, 0, 8 );
5894
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005895 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5898 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005903 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5904 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005906 }
5907 }
5908#endif
5909
Paul Bakker5121ce52009-01-03 21:22:43 +00005910 ssl->state++;
5911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005913
5914 return( 0 );
5915}
5916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5918 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005919{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005920 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5923 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5924 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005925 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005926 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005927#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5929#if defined(MBEDTLS_SHA512_C)
5930 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005931 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5932 else
5933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#if defined(MBEDTLS_SHA256_C)
5935 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005936 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005937 else
5938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005942 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005943 }
Paul Bakker380da532012-04-18 16:10:25 +00005944}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005947{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5949 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005950 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5951 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5954#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005955 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005958 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005961}
5962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005964 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5967 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005968 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5969 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005970#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5972#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005973 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005976 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005979}
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5982 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5983static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005984 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005985{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005986 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5987 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005988}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005989#endif
Paul Bakker380da532012-04-18 16:10:25 +00005990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5992#if defined(MBEDTLS_SHA256_C)
5993static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005994 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005995{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005996 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005997}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005998#endif
Paul Bakker380da532012-04-18 16:10:25 +00005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_SHA512_C)
6001static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006002 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006003{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006004 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006005}
Paul Bakker769075d2012-11-24 11:26:46 +01006006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006010static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006012{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006013 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006014 mbedtls_md5_context md5;
6015 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006016
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 unsigned char padbuf[48];
6018 unsigned char md5sum[16];
6019 unsigned char sha1sum[20];
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006022 if( !session )
6023 session = ssl->session;
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006026
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006027 mbedtls_md5_init( &md5 );
6028 mbedtls_sha1_init( &sha1 );
6029
6030 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6031 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006032
6033 /*
6034 * SSLv3:
6035 * hash =
6036 * MD5( master + pad2 +
6037 * MD5( handshake + sender + master + pad1 ) )
6038 * + SHA1( master + pad2 +
6039 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006040 */
6041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006043 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6044 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006045#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006048 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6049 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006053 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006054
Paul Bakker1ef83d62012-04-11 12:09:53 +00006055 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006056
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006057 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6058 mbedtls_md5_update_ret( &md5, session->master, 48 );
6059 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6060 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006061
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006062 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6063 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6064 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6065 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Paul Bakker1ef83d62012-04-11 12:09:53 +00006067 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006069 mbedtls_md5_starts_ret( &md5 );
6070 mbedtls_md5_update_ret( &md5, session->master, 48 );
6071 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6072 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6073 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006075 mbedtls_sha1_starts_ret( &sha1 );
6076 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6077 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6078 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6079 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006082
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006083 mbedtls_md5_free( &md5 );
6084 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006086 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6087 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6088 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006095static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006097{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006098 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006099 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006100 mbedtls_md5_context md5;
6101 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006102 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006105 if( !session )
6106 session = ssl->session;
6107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006110 mbedtls_md5_init( &md5 );
6111 mbedtls_sha1_init( &sha1 );
6112
6113 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6114 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006115
Paul Bakker1ef83d62012-04-11 12:09:53 +00006116 /*
6117 * TLSv1:
6118 * hash = PRF( master, finished_label,
6119 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6120 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006123 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6124 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006125#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006128 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6129 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006130#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006133 ? "client finished"
6134 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006135
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006136 mbedtls_md5_finish_ret( &md5, padbuf );
6137 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006138
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006139 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006140 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006143
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006144 mbedtls_md5_free( &md5 );
6145 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006146
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006147 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6154#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006155static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006157{
6158 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006159 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006160 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006161 unsigned char padbuf[32];
6162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006164 if( !session )
6165 session = ssl->session;
6166
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006167 mbedtls_sha256_init( &sha256 );
6168
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006170
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006171 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006172
6173 /*
6174 * TLSv1.2:
6175 * hash = PRF( master, finished_label,
6176 * Hash( handshake ) )[0.11]
6177 */
6178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179#if !defined(MBEDTLS_SHA256_ALT)
6180 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006181 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006182#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006185 ? "client finished"
6186 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006187
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006188 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006189
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006190 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006191 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006194
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006195 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006196
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006197 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006204static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006206{
6207 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006208 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006209 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006210 unsigned char padbuf[48];
6211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006213 if( !session )
6214 session = ssl->session;
6215
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006216 mbedtls_sha512_init( &sha512 );
6217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006219
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006220 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006221
6222 /*
6223 * TLSv1.2:
6224 * hash = PRF( master, finished_label,
6225 * Hash( handshake ) )[0.11]
6226 */
6227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006229 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6230 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006231#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006234 ? "client finished"
6235 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006236
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006237 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006238
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006239 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006240 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006243
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006244 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006245
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006246 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250#endif /* MBEDTLS_SHA512_C */
6251#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006256
6257 /*
6258 * Free our handshake params
6259 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006260 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006262 ssl->handshake = NULL;
6263
6264 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006265 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006266 */
6267 if( ssl->transform )
6268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 mbedtls_ssl_transform_free( ssl->transform );
6270 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006271 }
6272 ssl->transform = ssl->transform_negotiate;
6273 ssl->transform_negotiate = NULL;
6274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006276}
6277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006279{
6280 int resume = ssl->handshake->resume;
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284#if defined(MBEDTLS_SSL_RENEGOTIATION)
6285 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006288 ssl->renego_records_seen = 0;
6289 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006290#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006291
6292 /*
6293 * Free the previous session and switch in the current one
6294 */
Paul Bakker0a597072012-09-25 21:55:46 +00006295 if( ssl->session )
6296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006298 /* RFC 7366 3.1: keep the EtM state */
6299 ssl->session_negotiate->encrypt_then_mac =
6300 ssl->session->encrypt_then_mac;
6301#endif
6302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 mbedtls_ssl_session_free( ssl->session );
6304 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006305 }
6306 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006307 ssl->session_negotiate = NULL;
6308
Paul Bakker0a597072012-09-25 21:55:46 +00006309 /*
6310 * Add cache entry
6311 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006312 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006313 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006314 resume == 0 )
6315 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006316 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006318 }
Paul Bakker0a597072012-09-25 21:55:46 +00006319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006321 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006322 ssl->handshake->flight != NULL )
6323 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006324 /* Cancel handshake timer */
6325 ssl_set_timer( ssl, 0 );
6326
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006327 /* Keep last flight around in case we need to resend it:
6328 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006330 }
6331 else
6332#endif
6333 ssl_handshake_wrapup_free_hs_transform( ssl );
6334
Paul Bakker48916f92012-09-16 19:57:18 +00006335 ssl->state++;
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006338}
6339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006341{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006342 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006345
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006346 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006347
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006348 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006349
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006350 /*
6351 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6352 * may define some other value. Currently (early 2016), no defined
6353 * ciphersuite does this (and this is unlikely to change as activity has
6354 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006359 ssl->verify_data_len = hash_len;
6360 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006361#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006362
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6365 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006366
6367 /*
6368 * In case of session resuming, invert the client and server
6369 * ChangeCipherSpec messages order.
6370 */
Paul Bakker0a597072012-09-25 21:55:46 +00006371 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006374 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006380#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006381 }
6382 else
6383 ssl->state++;
6384
Paul Bakker48916f92012-09-16 19:57:18 +00006385 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006386 * Switch to our negotiated transform and session parameters for outbound
6387 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006392 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006393 {
6394 unsigned char i;
6395
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006396 /* Remember current epoch settings for resending */
6397 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006398 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006399
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006400 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006401 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006402
6403 /* Increment epoch */
6404 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006405 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006406 break;
6407
6408 /* The loop goes to its end iff the counter is wrapping */
6409 if( i == 0 )
6410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6412 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006413 }
6414 }
6415 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006417 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006418
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006419 ssl->transform_out = ssl->transform_negotiate;
6420 ssl->session_out = ssl->session_negotiate;
6421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6423 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6428 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006429 }
6430 }
6431#endif
6432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006434 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006436#endif
6437
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006438 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006439 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006441 return( ret );
6442 }
6443
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006444#if defined(MBEDTLS_SSL_PROTO_DTLS)
6445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6446 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6447 {
6448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6449 return( ret );
6450 }
6451#endif
6452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006454
6455 return( 0 );
6456}
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006459#define SSL_MAX_HASH_LEN 36
6460#else
6461#define SSL_MAX_HASH_LEN 12
6462#endif
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006465{
Paul Bakker23986e52011-04-24 08:57:21 +00006466 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006467 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006468 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006471
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006472 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006473
Hanno Becker327c93b2018-08-15 13:56:18 +01006474 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006477 return( ret );
6478 }
6479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006483 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6484 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 }
6487
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006488 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489#if defined(MBEDTLS_SSL_PROTO_SSL3)
6490 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006491 hash_len = 36;
6492 else
6493#endif
6494 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6497 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006500 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6501 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006503 }
6504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006506 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 }
6513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006515 ssl->verify_data_len = hash_len;
6516 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006517#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006518
Paul Bakker0a597072012-09-25 21:55:46 +00006519 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006522 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006526 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006528#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 }
6530 else
6531 ssl->state++;
6532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006536#endif
6537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006539
6540 return( 0 );
6541}
6542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6548 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6549 mbedtls_md5_init( &handshake->fin_md5 );
6550 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006551 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6552 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6555#if defined(MBEDTLS_SHA256_C)
6556 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006557 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006558#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559#if defined(MBEDTLS_SHA512_C)
6560 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006561 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006564
6565 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006566
6567#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6568 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6569 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6570#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572#if defined(MBEDTLS_DHM_C)
6573 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575#if defined(MBEDTLS_ECDH_C)
6576 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006577#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006578#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006579 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006580#if defined(MBEDTLS_SSL_CLI_C)
6581 handshake->ecjpake_cache = NULL;
6582 handshake->ecjpake_cache_len = 0;
6583#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006584#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006585
6586#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6587 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6588#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006589}
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006592{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6596 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 mbedtls_md_init( &transform->md_ctx_enc );
6599 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006600}
6601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006605}
6606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006608{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006609 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006610 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006612 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006614 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006615 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006616
6617 /*
6618 * Either the pointers are now NULL or cleared properly and can be freed.
6619 * Now allocate missing structures.
6620 */
6621 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006622 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006623 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006624 }
Paul Bakker48916f92012-09-16 19:57:18 +00006625
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006626 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006627 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006628 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006629 }
Paul Bakker48916f92012-09-16 19:57:18 +00006630
Paul Bakker82788fb2014-10-20 13:59:19 +02006631 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006632 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006633 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006634 }
Paul Bakker48916f92012-09-16 19:57:18 +00006635
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006636 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006637 if( ssl->handshake == NULL ||
6638 ssl->transform_negotiate == NULL ||
6639 ssl->session_negotiate == NULL )
6640 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 mbedtls_free( ssl->handshake );
6644 mbedtls_free( ssl->transform_negotiate );
6645 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006646
6647 ssl->handshake = NULL;
6648 ssl->transform_negotiate = NULL;
6649 ssl->session_negotiate = NULL;
6650
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006651 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006652 }
6653
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006654 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006656 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006657 ssl_handshake_params_init( ssl->handshake );
6658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6661 {
6662 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006663
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006664 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6665 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6666 else
6667 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006668
6669 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006670 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006671#endif
6672
Paul Bakker48916f92012-09-16 19:57:18 +00006673 return( 0 );
6674}
6675
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006676#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006677/* Dummy cookie callbacks for defaults */
6678static int ssl_cookie_write_dummy( void *ctx,
6679 unsigned char **p, unsigned char *end,
6680 const unsigned char *cli_id, size_t cli_id_len )
6681{
6682 ((void) ctx);
6683 ((void) p);
6684 ((void) end);
6685 ((void) cli_id);
6686 ((void) cli_id_len);
6687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006689}
6690
6691static int ssl_cookie_check_dummy( void *ctx,
6692 const unsigned char *cookie, size_t cookie_len,
6693 const unsigned char *cli_id, size_t cli_id_len )
6694{
6695 ((void) ctx);
6696 ((void) cookie);
6697 ((void) cookie_len);
6698 ((void) cli_id);
6699 ((void) cli_id_len);
6700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006702}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006703#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006704
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006705/* Once ssl->out_hdr as the address of the beginning of the
6706 * next outgoing record is set, deduce the other pointers.
6707 *
6708 * Note: For TLS, we save the implicit record sequence number
6709 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6710 * and the caller has to make sure there's space for this.
6711 */
6712
6713static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6714 mbedtls_ssl_transform *transform )
6715{
6716#if defined(MBEDTLS_SSL_PROTO_DTLS)
6717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6718 {
6719 ssl->out_ctr = ssl->out_hdr + 3;
6720 ssl->out_len = ssl->out_hdr + 11;
6721 ssl->out_iv = ssl->out_hdr + 13;
6722 }
6723 else
6724#endif
6725 {
6726 ssl->out_ctr = ssl->out_hdr - 8;
6727 ssl->out_len = ssl->out_hdr + 3;
6728 ssl->out_iv = ssl->out_hdr + 5;
6729 }
6730
6731 /* Adjust out_msg to make space for explicit IV, if used. */
6732 if( transform != NULL &&
6733 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6734 {
6735 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6736 }
6737 else
6738 ssl->out_msg = ssl->out_iv;
6739}
6740
6741/* Once ssl->in_hdr as the address of the beginning of the
6742 * next incoming record is set, deduce the other pointers.
6743 *
6744 * Note: For TLS, we save the implicit record sequence number
6745 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6746 * and the caller has to make sure there's space for this.
6747 */
6748
6749static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6750 mbedtls_ssl_transform *transform )
6751{
6752#if defined(MBEDTLS_SSL_PROTO_DTLS)
6753 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6754 {
6755 ssl->in_ctr = ssl->in_hdr + 3;
6756 ssl->in_len = ssl->in_hdr + 11;
6757 ssl->in_iv = ssl->in_hdr + 13;
6758 }
6759 else
6760#endif
6761 {
6762 ssl->in_ctr = ssl->in_hdr - 8;
6763 ssl->in_len = ssl->in_hdr + 3;
6764 ssl->in_iv = ssl->in_hdr + 5;
6765 }
6766
6767 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6768 if( transform != NULL &&
6769 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6770 {
6771 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6772 }
6773 else
6774 ssl->in_msg = ssl->in_iv;
6775}
6776
Paul Bakker5121ce52009-01-03 21:22:43 +00006777/*
6778 * Initialize an SSL context
6779 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006780void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6781{
6782 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6783}
6784
6785/*
6786 * Setup an SSL context
6787 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006788
6789static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6790{
6791 /* Set the incoming and outgoing record pointers. */
6792#if defined(MBEDTLS_SSL_PROTO_DTLS)
6793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6794 {
6795 ssl->out_hdr = ssl->out_buf;
6796 ssl->in_hdr = ssl->in_buf;
6797 }
6798 else
6799#endif /* MBEDTLS_SSL_PROTO_DTLS */
6800 {
6801 ssl->out_hdr = ssl->out_buf + 8;
6802 ssl->in_hdr = ssl->in_buf + 8;
6803 }
6804
6805 /* Derive other internal pointers. */
6806 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6807 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6808}
6809
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006810int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006811 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006812{
Paul Bakker48916f92012-09-16 19:57:18 +00006813 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006815 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006816
6817 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006818 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006819 */
Angus Grattond8213d02016-05-25 20:56:48 +10006820 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6821 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 {
Angus Grattond8213d02016-05-25 20:56:48 +10006823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6824 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6825 }
6826
6827 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6828 if( ssl->out_buf == NULL )
6829 {
6830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006832 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006833 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 }
6835
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006836 ssl_reset_in_out_pointers( ssl );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006837
Paul Bakker48916f92012-09-16 19:57:18 +00006838 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6839 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006840
6841 return( 0 );
6842}
6843
6844/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006845 * Reset an initialized and used SSL context for re-use while retaining
6846 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006847 *
6848 * If partial is non-zero, keep data in the input buffer and client ID.
6849 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006850 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006851static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006852{
Paul Bakker48916f92012-09-16 19:57:18 +00006853 int ret;
6854
Hanno Becker7e772132018-08-10 12:38:21 +01006855#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6856 !defined(MBEDTLS_SSL_SRV_C)
6857 ((void) partial);
6858#endif
6859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006861
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006862 /* Cancel any possibly running timer */
6863 ssl_set_timer( ssl, 0 );
6864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if defined(MBEDTLS_SSL_RENEGOTIATION)
6866 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006867 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006868
6869 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6871 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006874
Paul Bakker7eb013f2011-10-06 12:37:39 +00006875 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006876 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006877
6878 ssl->in_msgtype = 0;
6879 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006881 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006882 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006885 ssl_dtls_replay_reset( ssl );
6886#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006887
6888 ssl->in_hslen = 0;
6889 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006890
6891 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006892
6893 ssl->out_msgtype = 0;
6894 ssl->out_msglen = 0;
6895 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6897 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006898 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006899#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006900
Hanno Becker19859472018-08-06 09:40:20 +01006901 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6902
Paul Bakker48916f92012-09-16 19:57:18 +00006903 ssl->transform_in = NULL;
6904 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006905
Hanno Becker78640902018-08-13 16:35:15 +01006906 ssl->session_in = NULL;
6907 ssl->session_out = NULL;
6908
Angus Grattond8213d02016-05-25 20:56:48 +10006909 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006910
6911#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006912 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006913#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6914 {
6915 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006916 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006917 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6920 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6923 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6926 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006927 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006928 }
6929#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006930
Paul Bakker48916f92012-09-16 19:57:18 +00006931 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 mbedtls_ssl_transform_free( ssl->transform );
6934 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006935 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006936 }
Paul Bakker48916f92012-09-16 19:57:18 +00006937
Paul Bakkerc0463502013-02-14 11:19:38 +01006938 if( ssl->session )
6939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 mbedtls_ssl_session_free( ssl->session );
6941 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006942 ssl->session = NULL;
6943 }
6944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006946 ssl->alpn_chosen = NULL;
6947#endif
6948
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006949#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006950#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006951 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006952#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006953 {
6954 mbedtls_free( ssl->cli_id );
6955 ssl->cli_id = NULL;
6956 ssl->cli_id_len = 0;
6957 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006958#endif
6959
Paul Bakker48916f92012-09-16 19:57:18 +00006960 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6961 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006962
6963 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006964}
6965
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006966/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006967 * Reset an initialized and used SSL context for re-use while retaining
6968 * all application-set variables, function pointers and data.
6969 */
6970int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6971{
6972 return( ssl_session_reset_int( ssl, 0 ) );
6973}
6974
6975/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 * SSL set accessors
6977 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006978void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006980 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006981}
6982
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006983void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006984{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006985 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006986}
6987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006989void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006990{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006991 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006992}
6993#endif
6994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006996void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006997{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006998 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006999}
7000#endif
7001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007003
Hanno Becker1841b0a2018-08-24 11:13:57 +01007004void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7005 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007006{
7007 ssl->disable_datagram_packing = !allow_packing;
7008}
7009
7010void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7011 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007012{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007013 conf->hs_timeout_min = min;
7014 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007015}
7016#endif
7017
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007018void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007019{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007020 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007021}
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007024void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007025 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007026 void *p_vrfy )
7027{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007028 conf->f_vrfy = f_vrfy;
7029 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007032
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007033void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007034 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007035 void *p_rng )
7036{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007037 conf->f_rng = f_rng;
7038 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007039}
7040
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007041void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007042 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007043 void *p_dbg )
7044{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007045 conf->f_dbg = f_dbg;
7046 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007047}
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007050 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007051 mbedtls_ssl_send_t *f_send,
7052 mbedtls_ssl_recv_t *f_recv,
7053 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007054{
7055 ssl->p_bio = p_bio;
7056 ssl->f_send = f_send;
7057 ssl->f_recv = f_recv;
7058 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007059}
7060
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007061#if defined(MBEDTLS_SSL_PROTO_DTLS)
7062void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7063{
7064 ssl->mtu = mtu;
7065}
7066#endif
7067
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007068void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007069{
7070 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007071}
7072
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007073void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7074 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007075 mbedtls_ssl_set_timer_t *f_set_timer,
7076 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007077{
7078 ssl->p_timer = p_timer;
7079 ssl->f_set_timer = f_set_timer;
7080 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007081
7082 /* Make sure we start with no timer running */
7083 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007084}
7085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007087void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007088 void *p_cache,
7089 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7090 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007091{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007092 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007093 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007094 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_SSL_CLI_C)
7099int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007100{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007101 int ret;
7102
7103 if( ssl == NULL ||
7104 session == NULL ||
7105 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007106 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007109 }
7110
7111 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7112 return( ret );
7113
Paul Bakker0a597072012-09-25 21:55:46 +00007114 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007115
7116 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007120void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007121 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007123 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7124 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7125 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7126 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007127}
7128
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007129void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007130 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007131 int major, int minor )
7132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007134 return;
7135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007137 return;
7138
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007139 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007140}
7141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007143void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007144 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007145{
7146 conf->cert_profile = profile;
7147}
7148
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007149/* Append a new keycert entry to a (possibly empty) list */
7150static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7151 mbedtls_x509_crt *cert,
7152 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007153{
niisato8ee24222018-06-25 19:05:48 +09007154 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007155
niisato8ee24222018-06-25 19:05:48 +09007156 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7157 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007158 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007159
niisato8ee24222018-06-25 19:05:48 +09007160 new_cert->cert = cert;
7161 new_cert->key = key;
7162 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007163
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007164 /* Update head is the list was null, else add to the end */
7165 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007166 {
niisato8ee24222018-06-25 19:05:48 +09007167 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007168 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007169 else
7170 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007171 mbedtls_ssl_key_cert *cur = *head;
7172 while( cur->next != NULL )
7173 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007174 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007175 }
7176
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007177 return( 0 );
7178}
7179
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007180int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007181 mbedtls_x509_crt *own_cert,
7182 mbedtls_pk_context *pk_key )
7183{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007184 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007185}
7186
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007187void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007188 mbedtls_x509_crt *ca_chain,
7189 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007190{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007191 conf->ca_chain = ca_chain;
7192 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007195
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007196#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7197int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7198 mbedtls_x509_crt *own_cert,
7199 mbedtls_pk_context *pk_key )
7200{
7201 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7202 own_cert, pk_key ) );
7203}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007204
7205void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7206 mbedtls_x509_crt *ca_chain,
7207 mbedtls_x509_crl *ca_crl )
7208{
7209 ssl->handshake->sni_ca_chain = ca_chain;
7210 ssl->handshake->sni_ca_crl = ca_crl;
7211}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007212
7213void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7214 int authmode )
7215{
7216 ssl->handshake->sni_authmode = authmode;
7217}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007218#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7219
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007220#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007221/*
7222 * Set EC J-PAKE password for current handshake
7223 */
7224int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7225 const unsigned char *pw,
7226 size_t pw_len )
7227{
7228 mbedtls_ecjpake_role role;
7229
Janos Follath8eb64132016-06-03 15:40:57 +01007230 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7232
7233 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7234 role = MBEDTLS_ECJPAKE_SERVER;
7235 else
7236 role = MBEDTLS_ECJPAKE_CLIENT;
7237
7238 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7239 role,
7240 MBEDTLS_MD_SHA256,
7241 MBEDTLS_ECP_DP_SECP256R1,
7242 pw, pw_len ) );
7243}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007244#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007247int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007248 const unsigned char *psk, size_t psk_len,
7249 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007250{
Paul Bakker6db455e2013-09-18 17:29:31 +02007251 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007256
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007257 /* Identity len will be encoded on two bytes */
7258 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007259 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007260 {
7261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7262 }
7263
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007264 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007265 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007266 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007267
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007268 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007269 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007270 conf->psk_len = 0;
7271 }
7272 if( conf->psk_identity != NULL )
7273 {
7274 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007275 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007276 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007277 }
7278
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007279 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7280 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007281 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007282 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007283 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007284 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007285 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007286 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007287 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007288
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007289 conf->psk_len = psk_len;
7290 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007291
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007292 memcpy( conf->psk, psk, conf->psk_len );
7293 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007294
7295 return( 0 );
7296}
7297
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007298int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7299 const unsigned char *psk, size_t psk_len )
7300{
7301 if( psk == NULL || ssl->handshake == NULL )
7302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7303
7304 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7306
7307 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007308 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007309 mbedtls_platform_zeroize( ssl->handshake->psk,
7310 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007311 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007312 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007313 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007314
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007315 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007316 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007317
7318 ssl->handshake->psk_len = psk_len;
7319 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7320
7321 return( 0 );
7322}
7323
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007324void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007326 size_t),
7327 void *p_psk )
7328{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007329 conf->f_psk = f_psk;
7330 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007331}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007333
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007334#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007335
7336#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007337int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007338{
7339 int ret;
7340
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007341 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7342 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7343 {
7344 mbedtls_mpi_free( &conf->dhm_P );
7345 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007346 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
7349 return( 0 );
7350}
Hanno Becker470a8c42017-10-04 15:28:46 +01007351#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Hanno Beckera90658f2017-10-04 15:29:08 +01007353int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7354 const unsigned char *dhm_P, size_t P_len,
7355 const unsigned char *dhm_G, size_t G_len )
7356{
7357 int ret;
7358
7359 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7360 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7361 {
7362 mbedtls_mpi_free( &conf->dhm_P );
7363 mbedtls_mpi_free( &conf->dhm_G );
7364 return( ret );
7365 }
7366
7367 return( 0 );
7368}
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007370int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007371{
7372 int ret;
7373
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007374 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7375 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7376 {
7377 mbedtls_mpi_free( &conf->dhm_P );
7378 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007379 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007380 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007381
7382 return( 0 );
7383}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007384#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007385
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007386#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7387/*
7388 * Set the minimum length for Diffie-Hellman parameters
7389 */
7390void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7391 unsigned int bitlen )
7392{
7393 conf->dhm_min_bitlen = bitlen;
7394}
7395#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7396
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007397#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007398/*
7399 * Set allowed/preferred hashes for handshake signatures
7400 */
7401void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7402 const int *hashes )
7403{
7404 conf->sig_hashes = hashes;
7405}
Hanno Becker947194e2017-04-07 13:25:49 +01007406#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007407
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007408#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007409/*
7410 * Set the allowed elliptic curves
7411 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007412void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007413 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007415 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007416}
Hanno Becker947194e2017-04-07 13:25:49 +01007417#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007418
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007419#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007421{
Hanno Becker947194e2017-04-07 13:25:49 +01007422 /* Initialize to suppress unnecessary compiler warning */
7423 size_t hostname_len = 0;
7424
7425 /* Check if new hostname is valid before
7426 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007427 if( hostname != NULL )
7428 {
7429 hostname_len = strlen( hostname );
7430
7431 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7433 }
7434
7435 /* Now it's clear that we will overwrite the old hostname,
7436 * so we can free it safely */
7437
7438 if( ssl->hostname != NULL )
7439 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007440 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007441 mbedtls_free( ssl->hostname );
7442 }
7443
7444 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007445
Paul Bakker5121ce52009-01-03 21:22:43 +00007446 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007447 {
7448 ssl->hostname = NULL;
7449 }
7450 else
7451 {
7452 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007453 if( ssl->hostname == NULL )
7454 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007455
Hanno Becker947194e2017-04-07 13:25:49 +01007456 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007457
Hanno Becker947194e2017-04-07 13:25:49 +01007458 ssl->hostname[hostname_len] = '\0';
7459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007460
7461 return( 0 );
7462}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007463#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007464
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007465#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007466void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007468 const unsigned char *, size_t),
7469 void *p_sni )
7470{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007471 conf->f_sni = f_sni;
7472 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007473}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007477int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007478{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007479 size_t cur_len, tot_len;
7480 const char **p;
7481
7482 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007483 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7484 * MUST NOT be truncated."
7485 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007486 */
7487 tot_len = 0;
7488 for( p = protos; *p != NULL; p++ )
7489 {
7490 cur_len = strlen( *p );
7491 tot_len += cur_len;
7492
7493 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007495 }
7496
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007497 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007498
7499 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007500}
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007503{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007504 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007507
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007508void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007510 conf->max_major_ver = major;
7511 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007512}
7513
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007514void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007515{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007516 conf->min_major_ver = major;
7517 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007518}
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007521void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007522{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007523 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007524}
7525#endif
7526
Janos Follath088ce432017-04-10 12:42:31 +01007527#if defined(MBEDTLS_SSL_SRV_C)
7528void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7529 char cert_req_ca_list )
7530{
7531 conf->cert_req_ca_list = cert_req_ca_list;
7532}
7533#endif
7534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007536void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007538 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007539}
7540#endif
7541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007543void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007545 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007546}
7547#endif
7548
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007549#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007550void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007551{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007552 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007553}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007554#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007557int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007560 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007563 }
7564
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007565 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007566
7567 return( 0 );
7568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007572void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007574 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007579void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007580{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007581 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007582}
7583#endif
7584
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007585void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007587 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007588}
7589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007591void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007593 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007594}
7595
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007596void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007598 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007599}
7600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007601void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007602 const unsigned char period[8] )
7603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007604 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007609#if defined(MBEDTLS_SSL_CLI_C)
7610void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007611{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007612 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007613}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007614#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007615
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007616#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007617void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7618 mbedtls_ssl_ticket_write_t *f_ticket_write,
7619 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7620 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007621{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007622 conf->f_ticket_write = f_ticket_write;
7623 conf->f_ticket_parse = f_ticket_parse;
7624 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007625}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007626#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007628
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007629#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7630void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7631 mbedtls_ssl_export_keys_t *f_export_keys,
7632 void *p_export_keys )
7633{
7634 conf->f_export_keys = f_export_keys;
7635 conf->p_export_keys = p_export_keys;
7636}
7637#endif
7638
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007639#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007640void mbedtls_ssl_conf_async_private_cb(
7641 mbedtls_ssl_config *conf,
7642 mbedtls_ssl_async_sign_t *f_async_sign,
7643 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7644 mbedtls_ssl_async_resume_t *f_async_resume,
7645 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007646 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007647{
7648 conf->f_async_sign_start = f_async_sign;
7649 conf->f_async_decrypt_start = f_async_decrypt;
7650 conf->f_async_resume = f_async_resume;
7651 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007652 conf->p_async_config_data = async_config_data;
7653}
7654
Gilles Peskine8f97af72018-04-26 11:46:10 +02007655void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7656{
7657 return( conf->p_async_config_data );
7658}
7659
Gilles Peskine1febfef2018-04-30 11:54:39 +02007660void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007661{
7662 if( ssl->handshake == NULL )
7663 return( NULL );
7664 else
7665 return( ssl->handshake->user_async_ctx );
7666}
7667
Gilles Peskine1febfef2018-04-30 11:54:39 +02007668void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007669 void *ctx )
7670{
7671 if( ssl->handshake != NULL )
7672 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007673}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007674#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007675
Paul Bakker5121ce52009-01-03 21:22:43 +00007676/*
7677 * SSL get accessors
7678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007680{
7681 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7682}
7683
Hanno Becker8b170a02017-10-10 11:51:19 +01007684int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7685{
7686 /*
7687 * Case A: We're currently holding back
7688 * a message for further processing.
7689 */
7690
7691 if( ssl->keep_current_message == 1 )
7692 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007694 return( 1 );
7695 }
7696
7697 /*
7698 * Case B: Further records are pending in the current datagram.
7699 */
7700
7701#if defined(MBEDTLS_SSL_PROTO_DTLS)
7702 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7703 ssl->in_left > ssl->next_record_offset )
7704 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007706 return( 1 );
7707 }
7708#endif /* MBEDTLS_SSL_PROTO_DTLS */
7709
7710 /*
7711 * Case C: A handshake message is being processed.
7712 */
7713
Hanno Becker8b170a02017-10-10 11:51:19 +01007714 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7715 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007717 return( 1 );
7718 }
7719
7720 /*
7721 * Case D: An application data message is being processed
7722 */
7723 if( ssl->in_offt != NULL )
7724 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007726 return( 1 );
7727 }
7728
7729 /*
7730 * In all other cases, the rest of the message can be dropped.
7731 * As in ssl_read_record_layer, this needs to be adapted if
7732 * we implement support for multiple alerts in single records.
7733 */
7734
7735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7736 return( 0 );
7737}
7738
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007739uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007740{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007741 if( ssl->session != NULL )
7742 return( ssl->session->verify_result );
7743
7744 if( ssl->session_negotiate != NULL )
7745 return( ssl->session_negotiate->verify_result );
7746
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007747 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007748}
7749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007751{
Paul Bakker926c8e42013-03-06 10:23:34 +01007752 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007753 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007756}
7757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007762 {
7763 switch( ssl->minor_ver )
7764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007766 return( "DTLSv1.0" );
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007769 return( "DTLSv1.2" );
7770
7771 default:
7772 return( "unknown (DTLS)" );
7773 }
7774 }
7775#endif
7776
Paul Bakker43ca69c2011-01-15 17:35:19 +00007777 switch( ssl->minor_ver )
7778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007780 return( "SSLv3.0" );
7781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007783 return( "TLSv1.0" );
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007786 return( "TLSv1.1" );
7787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007789 return( "TLSv1.2" );
7790
Paul Bakker43ca69c2011-01-15 17:35:19 +00007791 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007792 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007793 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007794}
7795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007797{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007798 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007800 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007801
Hanno Becker78640902018-08-13 16:35:15 +01007802 if( transform == NULL )
7803 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_ZLIB_SUPPORT)
7806 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7807 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007808#endif
7809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 case MBEDTLS_MODE_GCM:
7813 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007814 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007816 transform_expansion = transform->minlen;
7817 break;
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007820
7821 block_size = mbedtls_cipher_get_block_size(
7822 &transform->cipher_ctx_enc );
7823
7824#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7825 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7826 {
7827 /* Expansion due to addition of
7828 * - MAC
7829 * - CBC padding (theoretically up to 256 bytes, but
7830 * we never use more than block_size)
7831 * - explicit IV
7832 */
7833 transform_expansion = transform->maclen + 2 * block_size;
7834 }
7835 else
7836#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7837 {
7838 /* No explicit IV prior to TLS 1.1. */
7839 transform_expansion = transform->maclen + block_size;
7840 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007841 break;
7842
7843 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007846 }
7847
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007848 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007849}
7850
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007851#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7852size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7853{
7854 size_t max_len;
7855
7856 /*
7857 * Assume mfl_code is correct since it was checked when set
7858 */
Angus Grattond8213d02016-05-25 20:56:48 +10007859 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007860
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007861 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007862 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007863 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007864 {
Angus Grattond8213d02016-05-25 20:56:48 +10007865 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007866 }
7867
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007868 /* During a handshake, use the value being negotiated */
7869 if( ssl->session_negotiate != NULL &&
7870 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7871 {
7872 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7873 }
7874
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007875 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007876}
7877#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7878
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007879#if defined(MBEDTLS_SSL_PROTO_DTLS)
7880static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7881{
7882 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7883 return( ssl->mtu );
7884
7885 if( ssl->mtu == 0 )
7886 return( ssl->handshake->mtu );
7887
7888 return( ssl->mtu < ssl->handshake->mtu ?
7889 ssl->mtu : ssl->handshake->mtu );
7890}
7891#endif /* MBEDTLS_SSL_PROTO_DTLS */
7892
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007893int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7894{
7895 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7896
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007897#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7898 !defined(MBEDTLS_SSL_PROTO_DTLS)
7899 (void) ssl;
7900#endif
7901
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007902#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7903 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7904
7905 if( max_len > mfl )
7906 max_len = mfl;
7907#endif
7908
7909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007910 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007911 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007912 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007913 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7914 const size_t overhead = (size_t) ret;
7915
7916 if( ret < 0 )
7917 return( ret );
7918
7919 if( mtu <= overhead )
7920 {
7921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7922 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7923 }
7924
7925 if( max_len > mtu - overhead )
7926 max_len = mtu - overhead;
7927 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007928#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007929
Hanno Becker0defedb2018-08-10 12:35:02 +01007930#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7931 !defined(MBEDTLS_SSL_PROTO_DTLS)
7932 ((void) ssl);
7933#endif
7934
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007935 return( (int) max_len );
7936}
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_X509_CRT_PARSE_C)
7939const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007940{
7941 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007942 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007943
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007944 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948#if defined(MBEDTLS_SSL_CLI_C)
7949int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007950{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007951 if( ssl == NULL ||
7952 dst == NULL ||
7953 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007954 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007957 }
7958
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007959 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007962
Paul Bakker5121ce52009-01-03 21:22:43 +00007963/*
Paul Bakker1961b702013-01-25 14:49:24 +01007964 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007969
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007970 if( ssl == NULL || ssl->conf == NULL )
7971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007978 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007980#endif
7981
Paul Bakker1961b702013-01-25 14:49:24 +01007982 return( ret );
7983}
7984
7985/*
7986 * Perform the SSL handshake
7987 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007989{
7990 int ret = 0;
7991
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007992 if( ssl == NULL || ssl->conf == NULL )
7993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008000
8001 if( ret != 0 )
8002 break;
8003 }
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008006
8007 return( ret );
8008}
8009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#if defined(MBEDTLS_SSL_RENEGOTIATION)
8011#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008012/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008013 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008016{
8017 int ret;
8018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008020
8021 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8023 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008024
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008025 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008026 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008028 return( ret );
8029 }
8030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008032
8033 return( 0 );
8034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008036
8037/*
8038 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 * - any side: calling mbedtls_ssl_renegotiate(),
8040 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8041 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008042 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008043 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008047{
8048 int ret;
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008051
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008052 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8053 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008054
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008055 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8056 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008058 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008060 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008061 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008062 ssl->handshake->out_msg_seq = 1;
8063 else
8064 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008065 }
8066#endif
8067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8069 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008074 return( ret );
8075 }
8076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008078
8079 return( 0 );
8080}
8081
8082/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008083 * Renegotiate current connection on client,
8084 * or request renegotiation on server
8085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008086int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008089
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008090 if( ssl == NULL || ssl->conf == NULL )
8091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008094 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008095 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008101
8102 /* Did we already try/start sending HelloRequest? */
8103 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008105
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008106 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008107 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008111 /*
8112 * On client, either start the renegotiation process or,
8113 * if already in progress, continue the handshake
8114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008119
8120 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008123 return( ret );
8124 }
8125 }
8126 else
8127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008131 return( ret );
8132 }
8133 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008135
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008136 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008137}
8138
8139/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008140 * Check record counters and renegotiate if they're above the limit.
8141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008143{
Andres AG2196c7f2016-12-15 17:01:16 +00008144 size_t ep_len = ssl_ep_len( ssl );
8145 int in_ctr_cmp;
8146 int out_ctr_cmp;
8147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8149 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008150 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008151 {
8152 return( 0 );
8153 }
8154
Andres AG2196c7f2016-12-15 17:01:16 +00008155 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8156 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008157 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008158 ssl->conf->renego_period + ep_len, 8 - ep_len );
8159
8160 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008161 {
8162 return( 0 );
8163 }
8164
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008169
8170/*
8171 * Receive application data decrypted from the SSL layer
8172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008174{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008175 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008176 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008177
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008178 if( ssl == NULL || ssl->conf == NULL )
8179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008187 return( ret );
8188
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008189 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008191 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008192 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008193 return( ret );
8194 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008195 }
8196#endif
8197
Hanno Becker4a810fb2017-05-24 16:27:30 +01008198 /*
8199 * Check if renegotiation is necessary and/or handshake is
8200 * in process. If yes, perform/continue, and fall through
8201 * if an unexpected packet is received while the client
8202 * is waiting for the ServerHello.
8203 *
8204 * (There is no equivalent to the last condition on
8205 * the server-side as it is not treated as within
8206 * a handshake while waiting for the ClientHello
8207 * after a renegotiation request.)
8208 */
8209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008211 ret = ssl_check_ctr_renegotiate( ssl );
8212 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8213 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008216 return( ret );
8217 }
8218#endif
8219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008223 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8224 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008227 return( ret );
8228 }
8229 }
8230
Hanno Beckere41158b2017-10-23 13:30:32 +01008231 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008232 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008233 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008234 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008235 if( ssl->f_get_timer != NULL &&
8236 ssl->f_get_timer( ssl->p_timer ) == -1 )
8237 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008238 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008239 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008240
Hanno Becker327c93b2018-08-15 13:56:18 +01008241 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008242 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008243 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8244 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008245
Hanno Becker4a810fb2017-05-24 16:27:30 +01008246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8247 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008248 }
8249
8250 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008252 {
8253 /*
8254 * OpenSSL sends empty messages to randomize the IV
8255 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008256 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008259 return( 0 );
8260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008262 return( ret );
8263 }
8264 }
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008269
Hanno Becker4a810fb2017-05-24 16:27:30 +01008270 /*
8271 * - For client-side, expect SERVER_HELLO_REQUEST.
8272 * - For server-side, expect CLIENT_HELLO.
8273 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8274 */
8275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008277 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008279 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008282
8283 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008286 {
8287 continue;
8288 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008289#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008291 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008292#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008293
Hanno Becker4a810fb2017-05-24 16:27:30 +01008294#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008295 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008299
8300 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008303 {
8304 continue;
8305 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008308 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008309#endif /* MBEDTLS_SSL_SRV_C */
8310
Hanno Becker21df7f92017-10-17 11:03:26 +01008311#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008312 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008313 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8314 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8315 ssl->conf->allow_legacy_renegotiation ==
8316 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8317 {
8318 /*
8319 * Accept renegotiation request
8320 */
Paul Bakker48916f92012-09-16 19:57:18 +00008321
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008322 /* DTLS clients need to know renego is server-initiated */
8323#if defined(MBEDTLS_SSL_PROTO_DTLS)
8324 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8325 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8326 {
8327 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8328 }
8329#endif
8330 ret = ssl_start_renegotiation( ssl );
8331 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8332 ret != 0 )
8333 {
8334 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8335 return( ret );
8336 }
8337 }
8338 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008339#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008340 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008341 /*
8342 * Refuse renegotiation
8343 */
8344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_PROTO_SSL3)
8348 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008349 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008350 /* SSLv3 does not have a "no_renegotiation" warning, so
8351 we send a fatal alert and abort the connection. */
8352 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8353 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8354 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008355 }
8356 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8358#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8359 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8360 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8363 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8364 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008365 {
8366 return( ret );
8367 }
Paul Bakker48916f92012-09-16 19:57:18 +00008368 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008369 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8371 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8374 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008375 }
Paul Bakker48916f92012-09-16 19:57:18 +00008376 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008377
Hanno Becker90333da2017-10-10 11:27:13 +01008378 /* At this point, we don't know whether the renegotiation has been
8379 * completed or not. The cases to consider are the following:
8380 * 1) The renegotiation is complete. In this case, no new record
8381 * has been read yet.
8382 * 2) The renegotiation is incomplete because the client received
8383 * an application data record while awaiting the ServerHello.
8384 * 3) The renegotiation is incomplete because the client received
8385 * a non-handshake, non-application data message while awaiting
8386 * the ServerHello.
8387 * In each of these case, looping will be the proper action:
8388 * - For 1), the next iteration will read a new record and check
8389 * if it's application data.
8390 * - For 2), the loop condition isn't satisfied as application data
8391 * is present, hence continue is the same as break
8392 * - For 3), the loop condition is satisfied and read_record
8393 * will re-deliver the message that was held back by the client
8394 * when expecting the ServerHello.
8395 */
8396 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008397 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008398#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008400 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008401 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008402 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008403 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008406 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008408 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008409 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8414 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008417 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008418 }
8419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8423 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008424 }
8425
8426 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008427
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008428 /* We're going to return something now, cancel timer,
8429 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008431 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008432
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008434 /* If we requested renego but received AppData, resend HelloRequest.
8435 * Do it now, after setting in_offt, to avoid taking this branch
8436 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008438 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008440 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008441 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008444 return( ret );
8445 }
8446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008448#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008449 }
8450
8451 n = ( len < ssl->in_msglen )
8452 ? len : ssl->in_msglen;
8453
8454 memcpy( buf, ssl->in_offt, n );
8455 ssl->in_msglen -= n;
8456
8457 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008458 {
8459 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008460 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008461 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008462 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008463 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008464 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008465 /* more data available */
8466 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008470
Paul Bakker23986e52011-04-24 08:57:21 +00008471 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008472}
8473
8474/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008475 * Send application data to be encrypted by the SSL layer, taking care of max
8476 * fragment length and buffer size.
8477 *
8478 * According to RFC 5246 Section 6.2.1:
8479 *
8480 * Zero-length fragments of Application data MAY be sent as they are
8481 * potentially useful as a traffic analysis countermeasure.
8482 *
8483 * Therefore, it is possible that the input message length is 0 and the
8484 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008485 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008486static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008487 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008488{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008489 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8490 const size_t max_len = (size_t) ret;
8491
8492 if( ret < 0 )
8493 {
8494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8495 return( ret );
8496 }
8497
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008498 if( len > max_len )
8499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008504 "maximum fragment length: %d > %d",
8505 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008507 }
8508 else
8509#endif
8510 len = max_len;
8511 }
Paul Bakker887bd502011-06-08 13:10:54 +00008512
Paul Bakker5121ce52009-01-03 21:22:43 +00008513 if( ssl->out_left != 0 )
8514 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008515 /*
8516 * The user has previously tried to send the data and
8517 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8518 * written. In this case, we expect the high-level write function
8519 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008524 return( ret );
8525 }
8526 }
Paul Bakker887bd502011-06-08 13:10:54 +00008527 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008528 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008529 /*
8530 * The user is trying to send a message the first time, so we need to
8531 * copy the data into the internal buffers and setup the data structure
8532 * to keep track of partial writes
8533 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008534 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008536 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008537
Hanno Becker67bc7c32018-08-06 11:33:50 +01008538 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008541 return( ret );
8542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008543 }
8544
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008545 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008546}
8547
8548/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008549 * Write application data, doing 1/n-1 splitting if necessary.
8550 *
8551 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008552 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008553 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008556static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008557 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008558{
8559 int ret;
8560
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008561 if( ssl->conf->cbc_record_splitting ==
8562 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008563 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8565 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8566 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008567 {
8568 return( ssl_write_real( ssl, buf, len ) );
8569 }
8570
8571 if( ssl->split_done == 0 )
8572 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008573 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008574 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008575 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008576 }
8577
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008578 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8579 return( ret );
8580 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008581
8582 return( ret + 1 );
8583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008584#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008585
8586/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008587 * Write application data (public-facing wrapper)
8588 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008589int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008590{
8591 int ret;
8592
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008594
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008595 if( ssl == NULL || ssl->conf == NULL )
8596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8597
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008598#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008599 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8600 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008601 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008602 return( ret );
8603 }
8604#endif
8605
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008606 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008607 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008608 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008609 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008611 return( ret );
8612 }
8613 }
8614
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008615#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008616 ret = ssl_write_split( ssl, buf, len );
8617#else
8618 ret = ssl_write_real( ssl, buf, len );
8619#endif
8620
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008622
8623 return( ret );
8624}
8625
8626/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008627 * Notify the peer that the connection is being closed
8628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008630{
8631 int ret;
8632
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008633 if( ssl == NULL || ssl->conf == NULL )
8634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008637
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008638 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8644 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8645 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008648 return( ret );
8649 }
8650 }
8651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008653
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008654 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008655}
8656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008657void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008658{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008659 if( transform == NULL )
8660 return;
8661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008663 deflateEnd( &transform->ctx_deflate );
8664 inflateEnd( &transform->ctx_inflate );
8665#endif
8666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8668 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 mbedtls_md_free( &transform->md_ctx_enc );
8671 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008672
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008673 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008674}
8675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676#if defined(MBEDTLS_X509_CRT_PARSE_C)
8677static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008680
8681 while( cur != NULL )
8682 {
8683 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008685 cur = next;
8686 }
8687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008689
Hanno Becker0271f962018-08-16 13:23:47 +01008690#if defined(MBEDTLS_SSL_PROTO_DTLS)
8691
8692static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8693{
8694 unsigned offset;
8695 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8696
8697 if( hs == NULL )
8698 return;
8699
Hanno Becker283f5ef2018-08-24 09:34:47 +01008700 ssl_free_buffered_record( ssl );
8701
Hanno Becker0271f962018-08-16 13:23:47 +01008702 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008703 ssl_buffering_free_slot( ssl, offset );
8704}
8705
8706static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8707 uint8_t slot )
8708{
8709 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8710 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008711
8712 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8713 return;
8714
Hanno Beckere605b192018-08-21 15:59:07 +01008715 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008716 {
Hanno Beckere605b192018-08-21 15:59:07 +01008717 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8718 mbedtls_free( hs_buf->data );
8719 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008720 }
8721}
8722
8723#endif /* MBEDTLS_SSL_PROTO_DTLS */
8724
Gilles Peskine9b562d52018-04-25 20:32:43 +02008725void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008726{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008727 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8728
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008729 if( handshake == NULL )
8730 return;
8731
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008732#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8733 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8734 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008735 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008736 handshake->async_in_progress = 0;
8737 }
8738#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8739
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008740#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8741 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8742 mbedtls_md5_free( &handshake->fin_md5 );
8743 mbedtls_sha1_free( &handshake->fin_sha1 );
8744#endif
8745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8746#if defined(MBEDTLS_SHA256_C)
8747 mbedtls_sha256_free( &handshake->fin_sha256 );
8748#endif
8749#if defined(MBEDTLS_SHA512_C)
8750 mbedtls_sha512_free( &handshake->fin_sha512 );
8751#endif
8752#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#if defined(MBEDTLS_DHM_C)
8755 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757#if defined(MBEDTLS_ECDH_C)
8758 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008759#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008760#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008761 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008762#if defined(MBEDTLS_SSL_CLI_C)
8763 mbedtls_free( handshake->ecjpake_cache );
8764 handshake->ecjpake_cache = NULL;
8765 handshake->ecjpake_cache_len = 0;
8766#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008767#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008768
Janos Follath4ae5c292016-02-10 11:27:43 +00008769#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8770 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008771 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008773#endif
8774
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008775#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8776 if( handshake->psk != NULL )
8777 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008778 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008779 mbedtls_free( handshake->psk );
8780 }
8781#endif
8782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8784 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008785 /*
8786 * Free only the linked list wrapper, not the keys themselves
8787 * since the belong to the SNI callback
8788 */
8789 if( handshake->sni_key_cert != NULL )
8790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008792
8793 while( cur != NULL )
8794 {
8795 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008797 cur = next;
8798 }
8799 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_SSL_PROTO_DTLS)
8803 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008804 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008805 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008806#endif
8807
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008808 mbedtls_platform_zeroize( handshake,
8809 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008810}
8811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008813{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008814 if( session == NULL )
8815 return;
8816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008818 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820 mbedtls_x509_crt_free( session->peer_cert );
8821 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008822 }
Paul Bakkered27a042013-04-18 22:46:23 +02008823#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008824
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008825#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008826 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008827#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008828
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008829 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008830}
8831
Paul Bakker5121ce52009-01-03 21:22:43 +00008832/*
8833 * Free an SSL context
8834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008836{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008837 if( ssl == NULL )
8838 return;
8839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008841
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008842 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008843 {
Angus Grattond8213d02016-05-25 20:56:48 +10008844 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008846 }
8847
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008848 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008849 {
Angus Grattond8213d02016-05-25 20:56:48 +10008850 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 }
8853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008855 if( ssl->compress_buf != NULL )
8856 {
Angus Grattond8213d02016-05-25 20:56:48 +10008857 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008859 }
8860#endif
8861
Paul Bakker48916f92012-09-16 19:57:18 +00008862 if( ssl->transform )
8863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864 mbedtls_ssl_transform_free( ssl->transform );
8865 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008866 }
8867
8868 if( ssl->handshake )
8869 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008870 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8872 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 mbedtls_free( ssl->handshake );
8875 mbedtls_free( ssl->transform_negotiate );
8876 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008877 }
8878
Paul Bakkerc0463502013-02-14 11:19:38 +01008879 if( ssl->session )
8880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881 mbedtls_ssl_session_free( ssl->session );
8882 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008883 }
8884
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008885#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008886 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008887 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008888 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008890 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008891#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8894 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8897 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008898 }
8899#endif
8900
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008901#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008902 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008903#endif
8904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008906
Paul Bakker86f04f42013-02-14 11:20:09 +01008907 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008908 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008909}
8910
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008911/*
8912 * Initialze mbedtls_ssl_config
8913 */
8914void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8915{
8916 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8917}
8918
Simon Butcherc97b6972015-12-27 23:48:17 +00008919#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008920static int ssl_preset_default_hashes[] = {
8921#if defined(MBEDTLS_SHA512_C)
8922 MBEDTLS_MD_SHA512,
8923 MBEDTLS_MD_SHA384,
8924#endif
8925#if defined(MBEDTLS_SHA256_C)
8926 MBEDTLS_MD_SHA256,
8927 MBEDTLS_MD_SHA224,
8928#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008929#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008930 MBEDTLS_MD_SHA1,
8931#endif
8932 MBEDTLS_MD_NONE
8933};
Simon Butcherc97b6972015-12-27 23:48:17 +00008934#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008935
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008936static int ssl_preset_suiteb_ciphersuites[] = {
8937 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8938 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8939 0
8940};
8941
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008942#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008943static int ssl_preset_suiteb_hashes[] = {
8944 MBEDTLS_MD_SHA256,
8945 MBEDTLS_MD_SHA384,
8946 MBEDTLS_MD_NONE
8947};
8948#endif
8949
8950#if defined(MBEDTLS_ECP_C)
8951static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8952 MBEDTLS_ECP_DP_SECP256R1,
8953 MBEDTLS_ECP_DP_SECP384R1,
8954 MBEDTLS_ECP_DP_NONE
8955};
8956#endif
8957
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008958/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008959 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008960 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008961int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008962 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008963{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008964#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008965 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008966#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008967
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008968 /* Use the functions here so that they are covered in tests,
8969 * but otherwise access member directly for efficiency */
8970 mbedtls_ssl_conf_endpoint( conf, endpoint );
8971 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008972
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008973 /*
8974 * Things that are common to all presets
8975 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008976#if defined(MBEDTLS_SSL_CLI_C)
8977 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8978 {
8979 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8980#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8981 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8982#endif
8983 }
8984#endif
8985
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008986#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008987 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008988#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008989
8990#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8991 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8992#endif
8993
8994#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8995 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8996#endif
8997
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008998#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8999 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9000#endif
9001
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009002#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009003 conf->f_cookie_write = ssl_cookie_write_dummy;
9004 conf->f_cookie_check = ssl_cookie_check_dummy;
9005#endif
9006
9007#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9008 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9009#endif
9010
Janos Follath088ce432017-04-10 12:42:31 +01009011#if defined(MBEDTLS_SSL_SRV_C)
9012 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9013#endif
9014
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009015#if defined(MBEDTLS_SSL_PROTO_DTLS)
9016 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9017 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9018#endif
9019
9020#if defined(MBEDTLS_SSL_RENEGOTIATION)
9021 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009022 memset( conf->renego_period, 0x00, 2 );
9023 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009024#endif
9025
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009026#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9027 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9028 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009029 const unsigned char dhm_p[] =
9030 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9031 const unsigned char dhm_g[] =
9032 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9033
Hanno Beckera90658f2017-10-04 15:29:08 +01009034 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9035 dhm_p, sizeof( dhm_p ),
9036 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009037 {
9038 return( ret );
9039 }
9040 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009041#endif
9042
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009043 /*
9044 * Preset-specific defaults
9045 */
9046 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009047 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009048 /*
9049 * NSA Suite B
9050 */
9051 case MBEDTLS_SSL_PRESET_SUITEB:
9052 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9053 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9054 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9055 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9056
9057 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9058 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9059 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9060 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9061 ssl_preset_suiteb_ciphersuites;
9062
9063#if defined(MBEDTLS_X509_CRT_PARSE_C)
9064 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009065#endif
9066
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009067#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009068 conf->sig_hashes = ssl_preset_suiteb_hashes;
9069#endif
9070
9071#if defined(MBEDTLS_ECP_C)
9072 conf->curve_list = ssl_preset_suiteb_curves;
9073#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009074 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009075
9076 /*
9077 * Default
9078 */
9079 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009080 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9081 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9082 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9083 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9084 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9085 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9086 MBEDTLS_SSL_MIN_MINOR_VERSION :
9087 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009088 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9089 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9090
9091#if defined(MBEDTLS_SSL_PROTO_DTLS)
9092 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9093 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9094#endif
9095
9096 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9097 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9098 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9099 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9100 mbedtls_ssl_list_ciphersuites();
9101
9102#if defined(MBEDTLS_X509_CRT_PARSE_C)
9103 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9104#endif
9105
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009106#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009107 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009108#endif
9109
9110#if defined(MBEDTLS_ECP_C)
9111 conf->curve_list = mbedtls_ecp_grp_id_list();
9112#endif
9113
9114#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9115 conf->dhm_min_bitlen = 1024;
9116#endif
9117 }
9118
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009119 return( 0 );
9120}
9121
9122/*
9123 * Free mbedtls_ssl_config
9124 */
9125void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9126{
9127#if defined(MBEDTLS_DHM_C)
9128 mbedtls_mpi_free( &conf->dhm_P );
9129 mbedtls_mpi_free( &conf->dhm_G );
9130#endif
9131
9132#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9133 if( conf->psk != NULL )
9134 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009135 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009136 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009137 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009138 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009139 }
9140
9141 if( conf->psk_identity != NULL )
9142 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009143 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009144 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009145 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009146 conf->psk_identity_len = 0;
9147 }
9148#endif
9149
9150#if defined(MBEDTLS_X509_CRT_PARSE_C)
9151 ssl_key_cert_free( conf->key_cert );
9152#endif
9153
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009154 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009155}
9156
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009157#if defined(MBEDTLS_PK_C) && \
9158 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009159/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164#if defined(MBEDTLS_RSA_C)
9165 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9166 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009167#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168#if defined(MBEDTLS_ECDSA_C)
9169 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9170 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009173}
9174
Hanno Becker7e5437a2017-04-28 17:15:26 +01009175unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9176{
9177 switch( type ) {
9178 case MBEDTLS_PK_RSA:
9179 return( MBEDTLS_SSL_SIG_RSA );
9180 case MBEDTLS_PK_ECDSA:
9181 case MBEDTLS_PK_ECKEY:
9182 return( MBEDTLS_SSL_SIG_ECDSA );
9183 default:
9184 return( MBEDTLS_SSL_SIG_ANON );
9185 }
9186}
9187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009189{
9190 switch( sig )
9191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192#if defined(MBEDTLS_RSA_C)
9193 case MBEDTLS_SSL_SIG_RSA:
9194 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196#if defined(MBEDTLS_ECDSA_C)
9197 case MBEDTLS_SSL_SIG_ECDSA:
9198 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009199#endif
9200 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009202 }
9203}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009204#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009205
Hanno Becker7e5437a2017-04-28 17:15:26 +01009206#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9207 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9208
9209/* Find an entry in a signature-hash set matching a given hash algorithm. */
9210mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9211 mbedtls_pk_type_t sig_alg )
9212{
9213 switch( sig_alg )
9214 {
9215 case MBEDTLS_PK_RSA:
9216 return( set->rsa );
9217 case MBEDTLS_PK_ECDSA:
9218 return( set->ecdsa );
9219 default:
9220 return( MBEDTLS_MD_NONE );
9221 }
9222}
9223
9224/* Add a signature-hash-pair to a signature-hash set */
9225void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9226 mbedtls_pk_type_t sig_alg,
9227 mbedtls_md_type_t md_alg )
9228{
9229 switch( sig_alg )
9230 {
9231 case MBEDTLS_PK_RSA:
9232 if( set->rsa == MBEDTLS_MD_NONE )
9233 set->rsa = md_alg;
9234 break;
9235
9236 case MBEDTLS_PK_ECDSA:
9237 if( set->ecdsa == MBEDTLS_MD_NONE )
9238 set->ecdsa = md_alg;
9239 break;
9240
9241 default:
9242 break;
9243 }
9244}
9245
9246/* Allow exactly one hash algorithm for each signature. */
9247void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9248 mbedtls_md_type_t md_alg )
9249{
9250 set->rsa = md_alg;
9251 set->ecdsa = md_alg;
9252}
9253
9254#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9255 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9256
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009257/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009258 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009261{
9262 switch( hash )
9263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264#if defined(MBEDTLS_MD5_C)
9265 case MBEDTLS_SSL_HASH_MD5:
9266 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268#if defined(MBEDTLS_SHA1_C)
9269 case MBEDTLS_SSL_HASH_SHA1:
9270 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009271#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272#if defined(MBEDTLS_SHA256_C)
9273 case MBEDTLS_SSL_HASH_SHA224:
9274 return( MBEDTLS_MD_SHA224 );
9275 case MBEDTLS_SSL_HASH_SHA256:
9276 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009277#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278#if defined(MBEDTLS_SHA512_C)
9279 case MBEDTLS_SSL_HASH_SHA384:
9280 return( MBEDTLS_MD_SHA384 );
9281 case MBEDTLS_SSL_HASH_SHA512:
9282 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009283#endif
9284 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009286 }
9287}
9288
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009289/*
9290 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9291 */
9292unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9293{
9294 switch( md )
9295 {
9296#if defined(MBEDTLS_MD5_C)
9297 case MBEDTLS_MD_MD5:
9298 return( MBEDTLS_SSL_HASH_MD5 );
9299#endif
9300#if defined(MBEDTLS_SHA1_C)
9301 case MBEDTLS_MD_SHA1:
9302 return( MBEDTLS_SSL_HASH_SHA1 );
9303#endif
9304#if defined(MBEDTLS_SHA256_C)
9305 case MBEDTLS_MD_SHA224:
9306 return( MBEDTLS_SSL_HASH_SHA224 );
9307 case MBEDTLS_MD_SHA256:
9308 return( MBEDTLS_SSL_HASH_SHA256 );
9309#endif
9310#if defined(MBEDTLS_SHA512_C)
9311 case MBEDTLS_MD_SHA384:
9312 return( MBEDTLS_SSL_HASH_SHA384 );
9313 case MBEDTLS_MD_SHA512:
9314 return( MBEDTLS_SSL_HASH_SHA512 );
9315#endif
9316 default:
9317 return( MBEDTLS_SSL_HASH_NONE );
9318 }
9319}
9320
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009321#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009322/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009323 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009324 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009325 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009326int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009329
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009330 if( ssl->conf->curve_list == NULL )
9331 return( -1 );
9332
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009333 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009334 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009335 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009336
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009337 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009338}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009339#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009340
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009341#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009342/*
9343 * Check if a hash proposed by the peer is in our list.
9344 * Return 0 if we're willing to use it, -1 otherwise.
9345 */
9346int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9347 mbedtls_md_type_t md )
9348{
9349 const int *cur;
9350
9351 if( ssl->conf->sig_hashes == NULL )
9352 return( -1 );
9353
9354 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9355 if( *cur == (int) md )
9356 return( 0 );
9357
9358 return( -1 );
9359}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009360#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362#if defined(MBEDTLS_X509_CRT_PARSE_C)
9363int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9364 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009365 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009366 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009367{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009368 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009370 int usage = 0;
9371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009373 const char *ext_oid;
9374 size_t ext_len;
9375#endif
9376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9378 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009379 ((void) cert);
9380 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009381 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009382#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9385 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009386 {
9387 /* Server part of the key exchange */
9388 switch( ciphersuite->key_exchange )
9389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 case MBEDTLS_KEY_EXCHANGE_RSA:
9391 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009392 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009393 break;
9394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9396 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9397 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9398 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009399 break;
9400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9402 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009403 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009404 break;
9405
9406 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407 case MBEDTLS_KEY_EXCHANGE_NONE:
9408 case MBEDTLS_KEY_EXCHANGE_PSK:
9409 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9410 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009411 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009412 usage = 0;
9413 }
9414 }
9415 else
9416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9418 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009419 }
9420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009422 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009423 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009424 ret = -1;
9425 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009426#else
9427 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9431 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9434 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009435 }
9436 else
9437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9439 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009440 }
9441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009443 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009444 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009445 ret = -1;
9446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009448
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009449 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009452
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009453/*
9454 * Convert version numbers to/from wire format
9455 * and, for DTLS, to/from TLS equivalent.
9456 *
9457 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009458 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009459 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9460 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009463 unsigned char ver[2] )
9464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#if defined(MBEDTLS_SSL_PROTO_DTLS)
9466 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009469 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9470
9471 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9472 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9473 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009474 else
9475#else
9476 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009477#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009478 {
9479 ver[0] = (unsigned char) major;
9480 ver[1] = (unsigned char) minor;
9481 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009482}
9483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009485 const unsigned char ver[2] )
9486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487#if defined(MBEDTLS_SSL_PROTO_DTLS)
9488 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009489 {
9490 *major = 255 - ver[0] + 2;
9491 *minor = 255 - ver[1] + 1;
9492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009494 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9495 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009496 else
9497#else
9498 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009499#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009500 {
9501 *major = ver[0];
9502 *minor = ver[1];
9503 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009504}
9505
Simon Butcher99000142016-10-13 17:21:01 +01009506int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9507{
9508#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9509 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9510 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9511
9512 switch( md )
9513 {
9514#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9515#if defined(MBEDTLS_MD5_C)
9516 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009517 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009518#endif
9519#if defined(MBEDTLS_SHA1_C)
9520 case MBEDTLS_SSL_HASH_SHA1:
9521 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9522 break;
9523#endif
9524#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9525#if defined(MBEDTLS_SHA512_C)
9526 case MBEDTLS_SSL_HASH_SHA384:
9527 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9528 break;
9529#endif
9530#if defined(MBEDTLS_SHA256_C)
9531 case MBEDTLS_SSL_HASH_SHA256:
9532 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9533 break;
9534#endif
9535 default:
9536 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9537 }
9538
9539 return 0;
9540#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9541 (void) ssl;
9542 (void) md;
9543
9544 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9545#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9546}
9547
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009548#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9549 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9550int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9551 unsigned char *output,
9552 unsigned char *data, size_t data_len )
9553{
9554 int ret = 0;
9555 mbedtls_md5_context mbedtls_md5;
9556 mbedtls_sha1_context mbedtls_sha1;
9557
9558 mbedtls_md5_init( &mbedtls_md5 );
9559 mbedtls_sha1_init( &mbedtls_sha1 );
9560
9561 /*
9562 * digitally-signed struct {
9563 * opaque md5_hash[16];
9564 * opaque sha_hash[20];
9565 * };
9566 *
9567 * md5_hash
9568 * MD5(ClientHello.random + ServerHello.random
9569 * + ServerParams);
9570 * sha_hash
9571 * SHA(ClientHello.random + ServerHello.random
9572 * + ServerParams);
9573 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009574 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009575 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009577 goto exit;
9578 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009579 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009580 ssl->handshake->randbytes, 64 ) ) != 0 )
9581 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009583 goto exit;
9584 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009585 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009586 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009588 goto exit;
9589 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009590 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009591 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009593 goto exit;
9594 }
9595
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009596 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009597 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009599 goto exit;
9600 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009601 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009602 ssl->handshake->randbytes, 64 ) ) != 0 )
9603 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009605 goto exit;
9606 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009607 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009608 data_len ) ) != 0 )
9609 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009611 goto exit;
9612 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009613 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009614 output + 16 ) ) != 0 )
9615 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009617 goto exit;
9618 }
9619
9620exit:
9621 mbedtls_md5_free( &mbedtls_md5 );
9622 mbedtls_sha1_free( &mbedtls_sha1 );
9623
9624 if( ret != 0 )
9625 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9626 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9627
9628 return( ret );
9629
9630}
9631#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9632 MBEDTLS_SSL_PROTO_TLS1_1 */
9633
9634#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9635 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9636int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009637 unsigned char *hash, size_t *hashlen,
9638 unsigned char *data, size_t data_len,
9639 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009640{
9641 int ret = 0;
9642 mbedtls_md_context_t ctx;
9643 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009644 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009645
9646 mbedtls_md_init( &ctx );
9647
9648 /*
9649 * digitally-signed struct {
9650 * opaque client_random[32];
9651 * opaque server_random[32];
9652 * ServerDHParams params;
9653 * };
9654 */
9655 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9656 {
9657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9658 goto exit;
9659 }
9660 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9661 {
9662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9663 goto exit;
9664 }
9665 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9666 {
9667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9668 goto exit;
9669 }
9670 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9671 {
9672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9673 goto exit;
9674 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009675 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009676 {
9677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9678 goto exit;
9679 }
9680
9681exit:
9682 mbedtls_md_free( &ctx );
9683
9684 if( ret != 0 )
9685 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9686 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9687
9688 return( ret );
9689}
9690#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9691 MBEDTLS_SSL_PROTO_TLS1_2 */
9692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693#endif /* MBEDTLS_SSL_TLS_C */