blob: 38826f93f6d9c27e8d35ecaec2474d3e7a91fdb1 [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 Beckercd9dcda2018-08-28 17:18:56 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *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 Beckerd5847772018-08-28 10:09:23 +0100112/* Forward declarations for functions related to message buffering. */
113static void ssl_buffering_free( mbedtls_ssl_context *ssl );
114static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
115 uint8_t slot );
116static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
117static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
118static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
119static int ssl_buffer_message( mbedtls_ssl_context *ssl );
120static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100121static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100122
Hanno Beckera67dee22018-08-22 10:05:20 +0100123static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100124static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100125{
Hanno Becker11682cc2018-08-22 14:41:02 +0100126 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100127
128 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100129 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100130
131 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
132}
133
Hanno Becker67bc7c32018-08-06 11:33:50 +0100134static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
135{
Hanno Becker11682cc2018-08-22 14:41:02 +0100136 size_t const bytes_written = ssl->out_left;
137 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100138
139 /* Double-check that the write-index hasn't gone
140 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100141 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100142 {
143 /* Should never happen... */
144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
145 }
146
147 return( (int) ( mtu - bytes_written ) );
148}
149
150static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
151{
152 int ret;
153 size_t remaining, expansion;
154 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
155
156#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
157 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
158
159 if( max_len > mfl )
160 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100161
162 /* By the standard (RFC 6066 Sect. 4), the MFL extension
163 * only limits the maximum record payload size, so in theory
164 * we would be allowed to pack multiple records of payload size
165 * MFL into a single datagram. However, this would mean that there's
166 * no way to explicitly communicate MTU restrictions to the peer.
167 *
168 * The following reduction of max_len makes sure that we never
169 * write datagrams larger than MFL + Record Expansion Overhead.
170 */
171 if( max_len <= ssl->out_left )
172 return( 0 );
173
174 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100175#endif
176
177 ret = ssl_get_remaining_space_in_datagram( ssl );
178 if( ret < 0 )
179 return( ret );
180 remaining = (size_t) ret;
181
182 ret = mbedtls_ssl_get_record_expansion( ssl );
183 if( ret < 0 )
184 return( ret );
185 expansion = (size_t) ret;
186
187 if( remaining <= expansion )
188 return( 0 );
189
190 remaining -= expansion;
191 if( remaining >= max_len )
192 remaining = max_len;
193
194 return( (int) remaining );
195}
196
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200197/*
198 * Double the retransmit timeout value, within the allowed range,
199 * returning -1 if the maximum value has already been reached.
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202{
203 uint32_t new_timeout;
204
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200205 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206 return( -1 );
207
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200208 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
209 * in the following way: after the initial transmission and a first
210 * retransmission, back off to a temporary estimated MTU of 508 bytes.
211 * This value is guaranteed to be deliverable (if not guaranteed to be
212 * delivered) of any compliant IPv4 (and IPv6) network, and should work
213 * on most non-IP stacks too. */
214 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400215 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200216 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
218 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200219
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200220 new_timeout = 2 * ssl->handshake->retransmit_timeout;
221
222 /* Avoid arithmetic overflow and range overflow */
223 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200224 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200225 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200226 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200227 }
228
229 ssl->handshake->retransmit_timeout = new_timeout;
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
233 return( 0 );
234}
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200237{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200238 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240 ssl->handshake->retransmit_timeout ) );
241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200245/*
246 * Convert max_fragment_length codes to length.
247 * RFC 6066 says:
248 * enum{
249 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
250 * } MaxFragmentLength;
251 * and we add 0 -> extension unused
252 */
Angus Grattond8213d02016-05-25 20:56:48 +1000253static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200254{
Angus Grattond8213d02016-05-25 20:56:48 +1000255 switch( mfl )
256 {
257 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
258 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
259 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
260 return 512;
261 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
262 return 1024;
263 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
264 return 2048;
265 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
266 return 4096;
267 default:
268 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
269 }
270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200272
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200273#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ssl_session_free( dst );
277 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200280 if( src->peer_cert != NULL )
281 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200282 int ret;
283
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200284 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200285 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200286 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200291 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200294 dst->peer_cert = NULL;
295 return( ret );
296 }
297 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 if( src->ticket != NULL )
302 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200303 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200304 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306
307 memcpy( dst->ticket, src->ticket, src->ticket_len );
308 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200309#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310
311 return( 0 );
312}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200313#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
316int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200317 const unsigned char *key_enc, const unsigned char *key_dec,
318 size_t keylen,
319 const unsigned char *iv_enc, const unsigned char *iv_dec,
320 size_t ivlen,
321 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200322 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
324int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
325int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
326int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
327int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
328#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000329
Paul Bakker5121ce52009-01-03 21:22:43 +0000330/*
331 * Key material generation
332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200334static int ssl3_prf( const unsigned char *secret, size_t slen,
335 const char *label,
336 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000337 unsigned char *dstbuf, size_t dlen )
338{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100339 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000340 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200341 mbedtls_md5_context md5;
342 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000343 unsigned char padding[16];
344 unsigned char sha1sum[20];
345 ((void)label);
346
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200347 mbedtls_md5_init( &md5 );
348 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200349
Paul Bakker5f70b252012-09-13 14:23:06 +0000350 /*
351 * SSLv3:
352 * block =
353 * MD5( secret + SHA1( 'A' + secret + random ) ) +
354 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
355 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
356 * ...
357 */
358 for( i = 0; i < dlen / 16; i++ )
359 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200360 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000361
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100362 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100363 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100364 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100365 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100366 if( ( ret = mbedtls_sha1_update_ret( &sha1, 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_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100369 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100370 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100371 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000372
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100373 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100374 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100375 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100376 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100377 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100378 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100379 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100380 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000381 }
382
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100383exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200384 mbedtls_md5_free( &md5 );
385 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500387 mbedtls_platform_zeroize( padding, sizeof( padding ) );
388 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000389
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200395static int tls1_prf( const unsigned char *secret, size_t slen,
396 const char *label,
397 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000398 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000399{
Paul Bakker23986e52011-04-24 08:57:21 +0000400 size_t nb, hs;
401 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200402 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 unsigned char tmp[128];
404 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 const mbedtls_md_info_t *md_info;
406 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100407 int ret;
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414 hs = ( slen + 1 ) / 2;
415 S1 = secret;
416 S2 = secret + slen - hs;
417
418 nb = strlen( label );
419 memcpy( tmp + 20, label, nb );
420 memcpy( tmp + 20 + nb, random, rlen );
421 nb += rlen;
422
423 /*
424 * First compute P_md5(secret,label+random)[0..dlen]
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100430 return( ret );
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
433 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
434 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 for( i = 0; i < dlen; i += 16 )
437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_md_hmac_reset ( &md_ctx );
439 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
440 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_md_hmac_reset ( &md_ctx );
443 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
444 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
446 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
447
448 for( j = 0; j < k; j++ )
449 dstbuf[i + j] = h_i[j];
450 }
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100453
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 /*
455 * XOR out with P_sha1(secret,label+random)[0..dlen]
456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100461 return( ret );
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
464 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
465 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 for( i = 0; i < dlen; i += 20 )
468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_md_hmac_reset ( &md_ctx );
470 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
471 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_md_hmac_reset ( &md_ctx );
474 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
475 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
478
479 for( j = 0; j < k; j++ )
480 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
481 }
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500485 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
486 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 return( 0 );
489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
493static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100494 const unsigned char *secret, size_t slen,
495 const char *label,
496 const unsigned char *random, size_t rlen,
497 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000498{
499 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100500 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000501 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
503 const mbedtls_md_info_t *md_info;
504 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100505 int ret;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
510 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100513
514 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000516
517 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100518 memcpy( tmp + md_len, label, nb );
519 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000520 nb += rlen;
521
522 /*
523 * Compute P_<hash>(secret, label + random)[0..dlen]
524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100526 return( ret );
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
529 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
530 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100531
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100532 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_md_hmac_reset ( &md_ctx );
535 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
536 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_md_hmac_reset ( &md_ctx );
539 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
540 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000541
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100542 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000543
544 for( j = 0; j < k; j++ )
545 dstbuf[i + j] = h_i[j];
546 }
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100549
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500550 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
551 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000552
553 return( 0 );
554}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100557static int tls_prf_sha256( const unsigned char *secret, size_t slen,
558 const char *label,
559 const unsigned char *random, size_t rlen,
560 unsigned char *dstbuf, size_t dlen )
561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100563 label, random, rlen, dstbuf, dlen ) );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200568static int tls_prf_sha384( const unsigned char *secret, size_t slen,
569 const char *label,
570 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000571 unsigned char *dstbuf, size_t dlen )
572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100574 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_SHA512_C */
577#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
582 defined(MBEDTLS_SSL_PROTO_TLS1_1)
583static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200584#endif
Paul Bakker380da532012-04-18 16:10:25 +0000585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#if defined(MBEDTLS_SSL_PROTO_SSL3)
587static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
588static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200589#endif
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
592static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
593static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200594#endif
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
597#if defined(MBEDTLS_SHA256_C)
598static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
599static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
600static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200601#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#if defined(MBEDTLS_SHA512_C)
604static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
605static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
606static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000611{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200612 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 unsigned char keyblk[256];
615 unsigned char *key1;
616 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100617 unsigned char *mac_enc;
618 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000619 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200620 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 const mbedtls_cipher_info_t *cipher_info;
622 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_ssl_session *session = ssl->session_negotiate;
625 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
626 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100631 if( cipher_info == NULL )
632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100634 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100636 }
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100639 if( md_info == NULL )
640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100642 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100644 }
645
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000647 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SSL_PROTO_SSL3)
650 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000651 {
Paul Bakker48916f92012-09-16 19:57:18 +0000652 handshake->tls_prf = ssl3_prf;
653 handshake->calc_verify = ssl_calc_verify_ssl;
654 handshake->calc_finished = ssl_calc_finished_ssl;
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) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
659 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000660 {
Paul Bakker48916f92012-09-16 19:57:18 +0000661 handshake->tls_prf = tls1_prf;
662 handshake->calc_verify = ssl_calc_verify_tls;
663 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200665 else
666#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
668#if defined(MBEDTLS_SHA512_C)
669 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
670 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671 {
Paul Bakker48916f92012-09-16 19:57:18 +0000672 handshake->tls_prf = tls_prf_sha384;
673 handshake->calc_verify = ssl_calc_verify_tls_sha384;
674 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000675 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000676 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SHA256_C)
679 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000680 {
Paul Bakker48916f92012-09-16 19:57:18 +0000681 handshake->tls_prf = tls_prf_sha256;
682 handshake->calc_verify = ssl_calc_verify_tls_sha256;
683 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685 else
686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
690 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200691 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000692
693 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 * SSLv3:
695 * master =
696 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
697 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
698 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200699 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200700 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 * master = PRF( premaster, "master secret", randbytes )[0..47]
702 */
Paul Bakker0a597072012-09-25 21:55:46 +0000703 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000706 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
709 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200710 {
711 unsigned char session_hash[48];
712 size_t hash_len;
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200715
716 ssl->handshake->calc_verify( ssl, session_hash );
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
719 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200722 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200724 {
725 hash_len = 48;
726 }
727 else
728#endif
729 hash_len = 32;
730 }
731 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200733 hash_len = 36;
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200736
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100737 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
738 "extended master secret",
739 session_hash, hash_len,
740 session->master, 48 );
741 if( ret != 0 )
742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100744 return( ret );
745 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200746
747 }
748 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200749#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100750 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
751 "master secret",
752 handshake->randbytes, 64,
753 session->master, 48 );
754 if( ret != 0 )
755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100757 return( ret );
758 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200759
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500760 mbedtls_platform_zeroize( handshake->premaster,
761 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 }
763 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
766 /*
767 * Swap the client and server random values.
768 */
Paul Bakker48916f92012-09-16 19:57:18 +0000769 memcpy( tmp, handshake->randbytes, 64 );
770 memcpy( handshake->randbytes, tmp + 32, 32 );
771 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500772 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000773
774 /*
775 * SSLv3:
776 * key block =
777 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
778 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
779 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
780 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
781 * ...
782 *
783 * TLSv1:
784 * key block = PRF( master, "key expansion", randbytes )
785 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100786 ret = handshake->tls_prf( session->master, 48, "key expansion",
787 handshake->randbytes, 64, keyblk, 256 );
788 if( ret != 0 )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100791 return( ret );
792 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
795 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
796 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
797 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
798 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500800 mbedtls_platform_zeroize( handshake->randbytes,
801 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
803 /*
804 * Determine the appropriate key, IV and MAC length.
805 */
Paul Bakker68884e32013-01-07 18:20:04 +0100806
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200807 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200810 cipher_info->mode == MBEDTLS_MODE_CCM ||
811 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200813 size_t taglen, explicit_ivlen;
814
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200815 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000816 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200817
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200818 /* All modes haves 96-bit IVs;
819 * GCM and CCM has 4 implicit and 8 explicit bytes
820 * ChachaPoly has all 12 bytes implicit
821 */
Paul Bakker68884e32013-01-07 18:20:04 +0100822 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200823 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
824 transform->fixed_ivlen = 12;
825 else
826 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200827
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200828 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
829 taglen = transform->ciphersuite_info->flags &
830 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
831
832
833 /* Minimum length of encrypted record */
834 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
835 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100836 }
837 else
838 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200839 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
841 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200844 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100845 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200847 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000848 mac_key_len = mbedtls_md_get_size( md_info );
849 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200852 /*
853 * If HMAC is to be truncated, we shall keep the leftmost bytes,
854 * (rfc 6066 page 13 or rfc 2104 section 4),
855 * so we only need to adjust the length here.
856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000860
861#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
862 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000863 * HMAC implementation which also truncates the key
864 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000865 mac_key_len = transform->maclen;
866#endif
867 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200869
870 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100871 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000872
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200873 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200875 transform->minlen = transform->maclen;
876 else
Paul Bakker68884e32013-01-07 18:20:04 +0100877 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200878 /*
879 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100880 * 1. if EtM is in use: one block plus MAC
881 * otherwise: * first multiple of blocklen greater than maclen
882 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
885 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100886 {
887 transform->minlen = transform->maclen
888 + cipher_info->block_size;
889 }
890 else
891#endif
892 {
893 transform->minlen = transform->maclen
894 + cipher_info->block_size
895 - transform->maclen % cipher_info->block_size;
896 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
899 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
900 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200901 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100902 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
905 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
906 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200907 {
908 transform->minlen += transform->ivlen;
909 }
910 else
911#endif
912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200915 }
Paul Bakker68884e32013-01-07 18:20:04 +0100916 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 }
918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000920 transform->keylen, transform->minlen, transform->ivlen,
921 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
923 /*
924 * Finally setup the cipher contexts, IVs and MAC secrets.
925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200927 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000929 key1 = keyblk + mac_key_len * 2;
930 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Paul Bakker68884e32013-01-07 18:20:04 +0100932 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000933 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000935 /*
936 * This is not used in TLS v1.1.
937 */
Paul Bakker48916f92012-09-16 19:57:18 +0000938 iv_copy_len = ( transform->fixed_ivlen ) ?
939 transform->fixed_ivlen : transform->ivlen;
940 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
941 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000942 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 }
944 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_SSL_CLI_C */
946#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000948 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000949 key1 = keyblk + mac_key_len * 2 + transform->keylen;
950 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Hanno Becker81c7b182017-11-09 18:39:33 +0000952 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100953 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000955 /*
956 * This is not used in TLS v1.1.
957 */
Paul Bakker48916f92012-09-16 19:57:18 +0000958 iv_copy_len = ( transform->fixed_ivlen ) ?
959 transform->fixed_ivlen : transform->ivlen;
960 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
961 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000962 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100964 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +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é-Gonnardd16d1cb2014-11-20 18:15:05 +0100969 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_SSL_PROTO_SSL3)
972 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100973 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000974 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100978 }
979
Hanno Becker81c7b182017-11-09 18:39:33 +0000980 memcpy( transform->mac_enc, mac_enc, mac_key_len );
981 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100982 }
983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#endif /* MBEDTLS_SSL_PROTO_SSL3 */
985#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
986 defined(MBEDTLS_SSL_PROTO_TLS1_2)
987 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100988 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100989 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
990 For AEAD-based ciphersuites, there is nothing to do here. */
991 if( mac_key_len != 0 )
992 {
993 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
994 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
995 }
Paul Bakker68884e32013-01-07 18:20:04 +0100996 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200997 else
998#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001002 }
Paul Bakker68884e32013-01-07 18:20:04 +01001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1005 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001006 {
1007 int ret = 0;
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001012 transform->iv_enc, transform->iv_dec,
1013 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001014 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001015 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1018 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001019 }
1020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001022
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001023#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1024 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001025 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001026 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1027 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001028 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001029 iv_copy_len );
1030 }
1031#endif
1032
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001033 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001034 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001037 return( ret );
1038 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001039
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001040 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001041 cipher_info ) ) != 0 )
1042 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001044 return( ret );
1045 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001048 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001052 return( ret );
1053 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001056 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001060 return( ret );
1061 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_CIPHER_MODE_CBC)
1064 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1067 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001070 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001071 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1074 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001077 return( ret );
1078 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001082 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001085 // Initialize compression
1086 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001088 {
Paul Bakker16770332013-10-11 09:59:44 +02001089 if( ssl->compress_buf == NULL )
1090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001092 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001093 if( ssl->compress_buf == NULL )
1094 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001096 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001097 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001098 }
1099 }
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001102
Paul Bakker48916f92012-09-16 19:57:18 +00001103 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1104 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001105
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001106 if( deflateInit( &transform->ctx_deflate,
1107 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001108 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1111 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001112 }
1113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
1118 return( 0 );
1119}
1120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#if defined(MBEDTLS_SSL_PROTO_SSL3)
1122void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001123{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001124 mbedtls_md5_context md5;
1125 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 unsigned char pad_1[48];
1127 unsigned char pad_2[48];
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001131 mbedtls_md5_init( &md5 );
1132 mbedtls_sha1_init( &sha1 );
1133
1134 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1135 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Paul Bakker380da532012-04-18 16:10:25 +00001137 memset( pad_1, 0x36, 48 );
1138 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001140 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1141 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1142 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001144 mbedtls_md5_starts_ret( &md5 );
1145 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1146 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1147 mbedtls_md5_update_ret( &md5, hash, 16 );
1148 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001150 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1151 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1152 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001153
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001154 mbedtls_sha1_starts_ret( &sha1 );
1155 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1156 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1157 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1158 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001162
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001163 mbedtls_md5_free( &md5 );
1164 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001165
Paul Bakker380da532012-04-18 16:10:25 +00001166 return;
1167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1171void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001172{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001173 mbedtls_md5_context md5;
1174 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001177
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001178 mbedtls_md5_init( &md5 );
1179 mbedtls_sha1_init( &sha1 );
1180
1181 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1182 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001183
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001184 mbedtls_md5_finish_ret( &md5, hash );
1185 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001189
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001190 mbedtls_md5_free( &md5 );
1191 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001192
Paul Bakker380da532012-04-18 16:10:25 +00001193 return;
1194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1198#if defined(MBEDTLS_SHA256_C)
1199void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001200{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001201 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001202
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001203 mbedtls_sha256_init( &sha256 );
1204
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001206
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001207 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001208 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001212
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001213 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001214
Paul Bakker380da532012-04-18 16:10:25 +00001215 return;
1216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#if defined(MBEDTLS_SHA512_C)
1220void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001221{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001222 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001223
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001224 mbedtls_sha512_init( &sha512 );
1225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001227
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001228 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001229 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001234 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001235
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 return;
1237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#endif /* MBEDTLS_SHA512_C */
1239#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1242int 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 +02001243{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001244 unsigned char *p = ssl->handshake->premaster;
1245 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001246 const unsigned char *psk = ssl->conf->psk;
1247 size_t psk_len = ssl->conf->psk_len;
1248
1249 /* If the psk callback was called, use its result */
1250 if( ssl->handshake->psk != NULL )
1251 {
1252 psk = ssl->handshake->psk;
1253 psk_len = ssl->handshake->psk_len;
1254 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001255
1256 /*
1257 * PMS = struct {
1258 * opaque other_secret<0..2^16-1>;
1259 * opaque psk<0..2^16-1>;
1260 * };
1261 * with "other_secret" depending on the particular key exchange
1262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1264 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001265 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001266 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001268
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001269 *(p++) = (unsigned char)( psk_len >> 8 );
1270 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001271
1272 if( end < p || (size_t)( end - p ) < psk_len )
1273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1274
1275 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001276 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001277 }
1278 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1280#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1281 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001282 {
1283 /*
1284 * other_secret already set by the ClientKeyExchange message,
1285 * and is 48 bytes long
1286 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001287 if( end - p < 2 )
1288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1289
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001290 *p++ = 0;
1291 *p++ = 48;
1292 p += 48;
1293 }
1294 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1296#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1297 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001298 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001299 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001300 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001301
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001302 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001304 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001305 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001308 return( ret );
1309 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001310 *(p++) = (unsigned char)( len >> 8 );
1311 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001312 p += len;
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001315 }
1316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1318#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1319 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001320 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001321 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001322 size_t zlen;
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001325 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001326 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001329 return( ret );
1330 }
1331
1332 *(p++) = (unsigned char)( zlen >> 8 );
1333 *(p++) = (unsigned char)( zlen );
1334 p += zlen;
1335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001337 }
1338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001343 }
1344
1345 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001346 if( end - p < 2 )
1347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001348
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001349 *(p++) = (unsigned char)( psk_len >> 8 );
1350 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001351
1352 if( end < p || (size_t)( end - p ) < psk_len )
1353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1354
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001355 memcpy( p, psk, psk_len );
1356 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001357
1358 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1359
1360 return( 0 );
1361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001365/*
1366 * SSLv3.0 MAC functions
1367 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001368#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001369static void ssl_mac( mbedtls_md_context_t *md_ctx,
1370 const unsigned char *secret,
1371 const unsigned char *buf, size_t len,
1372 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001373 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001374{
1375 unsigned char header[11];
1376 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001377 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1379 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001380
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001381 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001383 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001384 else
Paul Bakker68884e32013-01-07 18:20:04 +01001385 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
1387 memcpy( header, ctr, 8 );
1388 header[ 8] = (unsigned char) type;
1389 header[ 9] = (unsigned char)( len >> 8 );
1390 header[10] = (unsigned char)( len );
1391
Paul Bakker68884e32013-01-07 18:20:04 +01001392 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 mbedtls_md_starts( md_ctx );
1394 mbedtls_md_update( md_ctx, secret, md_size );
1395 mbedtls_md_update( md_ctx, padding, padlen );
1396 mbedtls_md_update( md_ctx, header, 11 );
1397 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001398 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Paul Bakker68884e32013-01-07 18:20:04 +01001400 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 mbedtls_md_starts( md_ctx );
1402 mbedtls_md_update( md_ctx, secret, md_size );
1403 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001404 mbedtls_md_update( md_ctx, out, md_size );
1405 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1410 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001411 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001412#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001413#endif
1414
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001415/* The function below is only used in the Lucky 13 counter-measure in
1416 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1417#if defined(SSL_SOME_MODES_USE_MAC) && \
1418 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1419 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1420 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1421/* This function makes sure every byte in the memory region is accessed
1422 * (in ascending addresses order) */
1423static void ssl_read_memory( unsigned char *p, size_t len )
1424{
1425 unsigned char acc = 0;
1426 volatile unsigned char force;
1427
1428 for( ; len != 0; p++, len-- )
1429 acc ^= *p;
1430
1431 force = acc;
1432 (void) force;
1433}
1434#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1435
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001436/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001442 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001446 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001450 }
1451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001455 ssl->out_msg, ssl->out_msglen );
1456
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001458 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001460#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 if( mode == MBEDTLS_MODE_STREAM ||
1462 ( mode == MBEDTLS_MODE_CBC
1463#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1464 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001465#endif
1466 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#if defined(MBEDTLS_SSL_PROTO_SSL3)
1469 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001470 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001471 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001472
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001473 ssl_mac( &ssl->transform_out->md_ctx_enc,
1474 ssl->transform_out->mac_enc,
1475 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001476 ssl->out_ctr, ssl->out_msgtype,
1477 mac );
1478
1479 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001480 }
1481 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1484 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1485 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001486 {
Hanno Becker992b6872017-11-09 18:57:39 +00001487 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1490 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1491 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1492 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001493 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001494 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001496
1497 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001498 }
1499 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001500#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001504 }
1505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001507 ssl->out_msg + ssl->out_msglen,
1508 ssl->transform_out->maclen );
1509
1510 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001511 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001512 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001513#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001515 /*
1516 * Encrypt
1517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1519 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001521 int ret;
1522 size_t olen = 0;
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 "including %d bytes of padding",
1526 ssl->out_msglen, 0 ) );
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001529 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001530 ssl->transform_out->ivlen,
1531 ssl->out_msg, ssl->out_msglen,
1532 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001535 return( ret );
1536 }
1537
1538 if( ssl->out_msglen != olen )
1539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 }
Paul Bakker68884e32013-01-07 18:20:04 +01001544 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001546#if defined(MBEDTLS_GCM_C) || \
1547 defined(MBEDTLS_CCM_C) || \
1548 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001550 mode == MBEDTLS_MODE_CCM ||
1551 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001552 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001553 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001554 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001555 unsigned char *enc_msg;
1556 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001557 unsigned char iv[12];
1558 mbedtls_ssl_transform *transform = ssl->transform_out;
1559 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001561 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001562
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001563 /*
1564 * Prepare additional authenticated data
1565 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001566 memcpy( add_data, ssl->out_ctr, 8 );
1567 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001569 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001570 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1571 add_data[12] = ssl->out_msglen & 0xFF;
1572
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001573 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001574
Paul Bakker68884e32013-01-07 18:20:04 +01001575 /*
1576 * Generate IV
1577 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001578 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1579 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001580 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001581 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1582 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1583 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1584
1585 }
1586 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1587 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001588 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001589 unsigned char i;
1590
1591 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1592
1593 for( i = 0; i < 8; i++ )
1594 iv[i+4] ^= ssl->out_ctr[i];
1595 }
1596 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001597 {
1598 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001601 }
1602
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001603 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1604 iv, transform->ivlen );
1605 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1606 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001607
Paul Bakker68884e32013-01-07 18:20:04 +01001608 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001609 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001610 */
1611 enc_msg = ssl->out_msg;
1612 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001613 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001616 "including 0 bytes of padding",
1617 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001618
Paul Bakker68884e32013-01-07 18:20:04 +01001619 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001620 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001621 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001622 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1623 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001624 add_data, 13,
1625 enc_msg, enc_msglen,
1626 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001627 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001630 return( ret );
1631 }
1632
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001633 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1636 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001637 }
1638
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001639 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001640 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1646#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001647 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001650 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001652 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653
Paul Bakker48916f92012-09-16 19:57:18 +00001654 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1655 ssl->transform_out->ivlen;
1656 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001657 padlen = 0;
1658
1659 for( i = 0; i <= padlen; i++ )
1660 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1661
1662 ssl->out_msglen += padlen + 1;
1663
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001664 enc_msglen = ssl->out_msglen;
1665 enc_msg = ssl->out_msg;
1666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001668 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001669 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1670 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001673 {
1674 /*
1675 * Generate IV
1676 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001677 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001678 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001679 if( ret != 0 )
1680 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001681
Paul Bakker92be97b2013-01-02 17:30:03 +01001682 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001683 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001684
1685 /*
1686 * Fix pointer positions and message length with added IV
1687 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001688 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001689 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001690 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001695 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001696 ssl->out_msglen, ssl->transform_out->ivlen,
1697 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001700 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001701 ssl->transform_out->ivlen,
1702 enc_msg, enc_msglen,
1703 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001706 return( ret );
1707 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001708
Paul Bakkercca5b812013-08-31 17:40:26 +02001709 if( enc_msglen != olen )
1710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001713 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1716 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001717 {
1718 /*
1719 * Save IV in SSL3 and TLS1
1720 */
1721 memcpy( ssl->transform_out->iv_enc,
1722 ssl->transform_out->cipher_ctx_enc.iv,
1723 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001725#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001728 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001729 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001730 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1731
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001732 /*
1733 * MAC(MAC_write_key, seq_num +
1734 * TLSCipherText.type +
1735 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001736 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001737 * IV + // except for TLS 1.0
1738 * ENC(content + padding + padding_length));
1739 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001740 unsigned char pseudo_hdr[13];
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001743
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001744 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1745 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001746 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1747 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1752 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001753 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001754 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001756
Hanno Becker3d8c9072018-01-05 16:24:22 +00001757 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1758 ssl->transform_out->maclen );
1759
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001760 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001761 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001765 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001767 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001771 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001773 /* Make extra sure authentication was performed, exactly once */
1774 if( auth_done != 1 )
1775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001778 }
1779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
1782 return( 0 );
1783}
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001786{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001788 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001789#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001790 size_t padlen = 0, correct = 1;
1791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001795 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001799 }
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001802
Paul Bakker48916f92012-09-16 19:57:18 +00001803 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001806 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 }
1809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1811 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001812 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001813 int ret;
1814 size_t olen = 0;
1815
Paul Bakker68884e32013-01-07 18:20:04 +01001816 padlen = 0;
1817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001819 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001820 ssl->transform_in->ivlen,
1821 ssl->in_msg, ssl->in_msglen,
1822 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001825 return( ret );
1826 }
1827
1828 if( ssl->in_msglen != olen )
1829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 }
Paul Bakker68884e32013-01-07 18:20:04 +01001834 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001836#if defined(MBEDTLS_GCM_C) || \
1837 defined(MBEDTLS_CCM_C) || \
1838 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001840 mode == MBEDTLS_MODE_CCM ||
1841 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001842 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001843 int ret;
1844 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001845 unsigned char *dec_msg;
1846 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001847 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001848 unsigned char iv[12];
1849 mbedtls_ssl_transform *transform = ssl->transform_in;
1850 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001852 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001853
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001854 /*
1855 * Compute and update sizes
1856 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001857 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001860 "+ taglen (%d)", ssl->in_msglen,
1861 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001863 }
1864 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1865
Paul Bakker68884e32013-01-07 18:20:04 +01001866 dec_msg = ssl->in_msg;
1867 dec_msg_result = ssl->in_msg;
1868 ssl->in_msglen = dec_msglen;
1869
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001870 /*
1871 * Prepare additional authenticated data
1872 */
Paul Bakker68884e32013-01-07 18:20:04 +01001873 memcpy( add_data, ssl->in_ctr, 8 );
1874 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001876 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001877 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1878 add_data[12] = ssl->in_msglen & 0xFF;
1879
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001880 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001881
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001882 /*
1883 * Prepare IV
1884 */
1885 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1886 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001887 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001888 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1889 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001891 }
1892 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1893 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001894 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001895 unsigned char i;
1896
1897 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1898
1899 for( i = 0; i < 8; i++ )
1900 iv[i+4] ^= ssl->in_ctr[i];
1901 }
1902 else
1903 {
1904 /* Reminder if we ever add an AEAD mode with a different size */
1905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1907 }
1908
1909 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001911
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001912 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001913 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001916 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001917 add_data, 13,
1918 dec_msg, dec_msglen,
1919 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001920 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1925 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001926
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001927 return( ret );
1928 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001929 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001930
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001931 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001935 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1939#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001940 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 {
Paul Bakker45829992013-01-03 14:52:21 +01001943 /*
1944 * Decrypt and check the padding
1945 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001946 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001947 unsigned char *dec_msg;
1948 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001949 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001950 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001951 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001952
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 /*
Paul Bakker45829992013-01-03 14:52:21 +01001954 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1957 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001958 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001959#endif
Paul Bakker45829992013-01-03 14:52:21 +01001960
1961 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1962 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001965 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1966 ssl->transform_in->ivlen,
1967 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001969 }
1970
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001971 dec_msglen = ssl->in_msglen;
1972 dec_msg = ssl->in_msg;
1973 dec_msg_result = ssl->in_msg;
1974
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001975 /*
1976 * Authenticate before decrypt if enabled
1977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1979 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001980 {
Hanno Becker992b6872017-11-09 18:57:39 +00001981 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001982 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001985
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001986 dec_msglen -= ssl->transform_in->maclen;
1987 ssl->in_msglen -= ssl->transform_in->maclen;
1988
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001989 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1990 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1991 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1992 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1997 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001999 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002003 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002004 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002005 ssl->transform_in->maclen );
2006
Hanno Becker992b6872017-11-09 18:57:39 +00002007 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2008 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002011
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 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002014 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002015 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002017
2018 /*
2019 * Check length sanity
2020 */
2021 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002024 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002026 }
2027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002029 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002030 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002033 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002034 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002035 dec_msglen -= ssl->transform_in->ivlen;
2036 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002037
Paul Bakker48916f92012-09-16 19:57:18 +00002038 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002039 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002044 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002045 ssl->transform_in->ivlen,
2046 dec_msg, dec_msglen,
2047 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002050 return( ret );
2051 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002052
Paul Bakkercca5b812013-08-31 17:40:26 +02002053 if( dec_msglen != olen )
2054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002057 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2060 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002061 {
2062 /*
2063 * Save IV in SSL3 and TLS1
2064 */
2065 memcpy( ssl->transform_in->iv_dec,
2066 ssl->transform_in->cipher_ctx_dec.iv,
2067 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002069#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
2071 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002072
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002073 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002074 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#if defined(MBEDTLS_SSL_DEBUG_ALL)
2077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002078 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002079#endif
Paul Bakker45829992013-01-03 14:52:21 +01002080 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002081 correct = 0;
2082 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#if defined(MBEDTLS_SSL_PROTO_SSL3)
2085 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 {
Paul Bakker48916f92012-09-16 19:57:18 +00002087 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_SSL_DEBUG_ALL)
2090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002092 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002093#endif
Paul Bakker45829992013-01-03 14:52:21 +01002094 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096 }
2097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2099#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2100 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2101 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
2103 /*
Paul Bakker45829992013-01-03 14:52:21 +01002104 * TLSv1+: always check the padding up to the first failure
2105 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002107 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002108 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002109 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002110
Paul Bakker956c9e02013-12-19 14:42:28 +01002111 /*
2112 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002113 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002114 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002115 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002116 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002117 *
2118 * In both cases we reset padding_idx to a safe value (0) to
2119 * prevent out-of-buffer reads.
2120 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002121 correct &= ( padlen <= ssl->in_msglen );
2122 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002123 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002124
2125 padding_idx *= correct;
2126
Angus Grattonb512bc12018-06-19 15:57:50 +10002127 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002128 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002129 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002130 pad_count += real_count *
2131 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2132 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002133
2134 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002137 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002139#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002140 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002142 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2144 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002148 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002149
2150 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002154 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002158 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002160#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002164
2165 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002166 * Authenticate if not done yet.
2167 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002169#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002170 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002171 {
Hanno Becker992b6872017-11-09 18:57:39 +00002172 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002173
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002174 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002175
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002176 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2177 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#if defined(MBEDTLS_SSL_PROTO_SSL3)
2180 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002181 {
2182 ssl_mac( &ssl->transform_in->md_ctx_dec,
2183 ssl->transform_in->mac_dec,
2184 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002185 ssl->in_ctr, ssl->in_msgtype,
2186 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002187 }
2188 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2190#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2191 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2192 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002193 {
2194 /*
2195 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002196 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002197 *
2198 * Known timing attacks:
2199 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2200 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002201 * To compensate for different timings for the MAC calculation
2202 * depending on how much padding was removed (which is determined
2203 * by padlen), process extra_run more blocks through the hash
2204 * function.
2205 *
2206 * The formula in the paper is
2207 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2208 * where L1 is the size of the header plus the decrypted message
2209 * plus CBC padding and L2 is the size of the header plus the
2210 * decrypted message. This is for an underlying hash function
2211 * with 64-byte blocks.
2212 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2213 * correctly. We round down instead of up, so -56 is the correct
2214 * value for our calculations instead of -55.
2215 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002216 * Repeat the formula rather than defining a block_size variable.
2217 * This avoids requiring division by a variable at runtime
2218 * (which would be marginally less efficient and would require
2219 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002220 */
2221 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002222
2223 /*
2224 * The next two sizes are the minimum and maximum values of
2225 * in_msglen over all padlen values.
2226 *
2227 * They're independent of padlen, since we previously did
2228 * in_msglen -= padlen.
2229 *
2230 * Note that max_len + maclen is never more than the buffer
2231 * length, as we previously did in_msglen -= maclen too.
2232 */
2233 const size_t max_len = ssl->in_msglen + padlen;
2234 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2235
Gilles Peskine20b44082018-05-29 14:06:49 +02002236 switch( ssl->transform_in->ciphersuite_info->mac )
2237 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002238#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2239 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002240 case MBEDTLS_MD_MD5:
2241 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002242 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002243 /* 8 bytes of message size, 64-byte compression blocks */
2244 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2245 ( 13 + ssl->in_msglen + 8 ) / 64;
2246 break;
2247#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002248#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002249 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002250 /* 16 bytes of message size, 128-byte compression blocks */
2251 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2252 ( 13 + ssl->in_msglen + 16 ) / 128;
2253 break;
2254#endif
2255 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2258 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002259
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002260 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2263 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2264 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2265 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002267 /* Make sure we access everything even when padlen > 0. This
2268 * makes the synchronisation requirements for just-in-time
2269 * Prime+Probe attacks much tighter and hopefully impractical. */
2270 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002271 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002272
2273 /* Call mbedtls_md_process at least once due to cache attacks
2274 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002275 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002279
2280 /* Make sure we access all the memory that could contain the MAC,
2281 * before we check it in the next code block. This makes the
2282 * synchronisation requirements for just-in-time Prime+Probe
2283 * attacks much tighter and hopefully impractical. */
2284 ssl_read_memory( ssl->in_msg + min_len,
2285 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002286 }
2287 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2289 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002295#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002296 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2297 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2298 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002299#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
Hanno Becker992b6872017-11-09 18:57:39 +00002301 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2302 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_SSL_DEBUG_ALL)
2305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002306#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002307 correct = 0;
2308 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002309 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002311 /*
2312 * Finally check the correct flag
2313 */
2314 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002316 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002317#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002318
2319 /* Make extra sure authentication was performed, exactly once */
2320 if( auth_done != 1 )
2321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
2326 if( ssl->in_msglen == 0 )
2327 {
Angus Gratton34817922018-06-19 15:58:22 +10002328#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2329 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2330 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2331 {
2332 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2334 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2335 }
2336#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2337
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 ssl->nb_zero++;
2339
2340 /*
2341 * Three or more empty messages may be a DoS attack
2342 * (excessive CPU consumption).
2343 */
2344 if( ssl->nb_zero > 3 )
2345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 }
2350 }
2351 else
2352 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002356 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002357 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002358 }
2359 else
2360#endif
2361 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002362 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002363 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2364 if( ++ssl->in_ctr[i - 1] != 0 )
2365 break;
2366
2367 /* The loop goes to its end iff the counter is wrapping */
2368 if( i == ssl_ep_len( ssl ) )
2369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2371 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002372 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002373 }
2374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
2377 return( 0 );
2378}
2379
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002380#undef MAC_NONE
2381#undef MAC_PLAINTEXT
2382#undef MAC_CIPHERTEXT
2383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002385/*
2386 * Compression/decompression functions
2387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002389{
2390 int ret;
2391 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002392 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002393 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002394 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002397
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002398 if( len_pre == 0 )
2399 return( 0 );
2400
Paul Bakker2770fbd2012-07-03 13:30:23 +00002401 memcpy( msg_pre, ssl->out_msg, len_pre );
2402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404 ssl->out_msglen ) );
2405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002407 ssl->out_msg, ssl->out_msglen );
2408
Paul Bakker48916f92012-09-16 19:57:18 +00002409 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2410 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2411 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002412 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002413
Paul Bakker48916f92012-09-16 19:57:18 +00002414 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415 if( ret != Z_OK )
2416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2418 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002419 }
2420
Angus Grattond8213d02016-05-25 20:56:48 +10002421 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002422 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002425 ssl->out_msglen ) );
2426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002428 ssl->out_msg, ssl->out_msglen );
2429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002431
2432 return( 0 );
2433}
2434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002436{
2437 int ret;
2438 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002439 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002441 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002444
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002445 if( len_pre == 0 )
2446 return( 0 );
2447
Paul Bakker2770fbd2012-07-03 13:30:23 +00002448 memcpy( msg_pre, ssl->in_msg, len_pre );
2449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002451 ssl->in_msglen ) );
2452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002454 ssl->in_msg, ssl->in_msglen );
2455
Paul Bakker48916f92012-09-16 19:57:18 +00002456 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2457 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2458 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002459 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002460 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002461
Paul Bakker48916f92012-09-16 19:57:18 +00002462 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002463 if( ret != Z_OK )
2464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2466 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002467 }
2468
Angus Grattond8213d02016-05-25 20:56:48 +10002469 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002470 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002473 ssl->in_msglen ) );
2474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002476 ssl->in_msg, ssl->in_msglen );
2477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002479
2480 return( 0 );
2481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2485static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487#if defined(MBEDTLS_SSL_PROTO_DTLS)
2488static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002489{
2490 /* If renegotiation is not enforced, retransmit until we would reach max
2491 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002492 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002493 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002494 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002495 unsigned char doublings = 1;
2496
2497 while( ratio != 0 )
2498 {
2499 ++doublings;
2500 ratio >>= 1;
2501 }
2502
2503 if( ++ssl->renego_records_seen > doublings )
2504 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002506 return( 0 );
2507 }
2508 }
2509
2510 return( ssl_write_hello_request( ssl ) );
2511}
2512#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002514
Paul Bakker5121ce52009-01-03 21:22:43 +00002515/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002516 * Fill the input message buffer by appending data to it.
2517 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002518 *
2519 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2520 * available (from this read and/or a previous one). Otherwise, an error code
2521 * is returned (possibly EOF or WANT_READ).
2522 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002523 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2524 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2525 * since we always read a whole datagram at once.
2526 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002527 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002528 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002531{
Paul Bakker23986e52011-04-24 08:57:21 +00002532 int ret;
2533 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002537 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002540 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002542 }
2543
Angus Grattond8213d02016-05-25 20:56:48 +10002544 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002548 }
2549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002551 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002553 uint32_t timeout;
2554
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002555 /* Just to be sure */
2556 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2557 {
2558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2559 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2561 }
2562
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002563 /*
2564 * The point is, we need to always read a full datagram at once, so we
2565 * sometimes read more then requested, and handle the additional data.
2566 * It could be the rest of the current record (while fetching the
2567 * header) and/or some other records in the same datagram.
2568 */
2569
2570 /*
2571 * Move to the next record in the already read datagram if applicable
2572 */
2573 if( ssl->next_record_offset != 0 )
2574 {
2575 if( ssl->in_left < ssl->next_record_offset )
2576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002579 }
2580
2581 ssl->in_left -= ssl->next_record_offset;
2582
2583 if( ssl->in_left != 0 )
2584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002586 ssl->next_record_offset ) );
2587 memmove( ssl->in_hdr,
2588 ssl->in_hdr + ssl->next_record_offset,
2589 ssl->in_left );
2590 }
2591
2592 ssl->next_record_offset = 0;
2593 }
2594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002597
2598 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002599 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002600 */
2601 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002604 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002605 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002606
2607 /*
2608 * A record can't be split accross datagrams. If we need to read but
2609 * are not at the beginning of a new record, the caller did something
2610 * wrong.
2611 */
2612 if( ssl->in_left != 0 )
2613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002616 }
2617
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002618 /*
2619 * Don't even try to read if time's out already.
2620 * This avoids by-passing the timer when repeatedly receiving messages
2621 * that will end up being dropped.
2622 */
2623 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002624 {
2625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002626 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002627 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002628 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002629 {
Angus Grattond8213d02016-05-25 20:56:48 +10002630 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002633 timeout = ssl->handshake->retransmit_timeout;
2634 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002635 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002638
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002639 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002640 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2641 timeout );
2642 else
2643 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002646
2647 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002649 }
2650
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002651 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002654 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002657 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002658 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002661 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002662 }
2663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002667 return( ret );
2668 }
2669
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002670 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002673 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002675 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002676 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002679 return( ret );
2680 }
2681
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002682 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002685 }
2686
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 if( ret < 0 )
2688 return( ret );
2689
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002690 ssl->in_left = ret;
2691 }
2692 else
2693#endif
2694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002696 ssl->in_left, nb_want ) );
2697
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002698 while( ssl->in_left < nb_want )
2699 {
2700 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002701
2702 if( ssl_check_timer( ssl ) != 0 )
2703 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2704 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002705 {
2706 if( ssl->f_recv_timeout != NULL )
2707 {
2708 ret = ssl->f_recv_timeout( ssl->p_bio,
2709 ssl->in_hdr + ssl->in_left, len,
2710 ssl->conf->read_timeout );
2711 }
2712 else
2713 {
2714 ret = ssl->f_recv( ssl->p_bio,
2715 ssl->in_hdr + ssl->in_left, len );
2716 }
2717 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002720 ssl->in_left, nb_want ) );
2721 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002722
2723 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002725
2726 if( ret < 0 )
2727 return( ret );
2728
mohammad160352aecb92018-03-28 23:41:40 -07002729 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002730 {
Darryl Green11999bb2018-03-13 15:22:58 +00002731 MBEDTLS_SSL_DEBUG_MSG( 1,
2732 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002733 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2735 }
2736
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002737 ssl->in_left += ret;
2738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 }
2740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
2743 return( 0 );
2744}
2745
2746/*
2747 * Flush any data not yet written
2748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002750{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002751 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002752 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002756 if( ssl->f_send == NULL )
2757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002759 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002761 }
2762
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002763 /* Avoid incrementing counter if data is flushed */
2764 if( ssl->out_left == 0 )
2765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002767 return( 0 );
2768 }
2769
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 while( ssl->out_left > 0 )
2771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2773 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Hanno Becker2b1e3542018-08-06 11:19:13 +01002775 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002776 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
2780 if( ret <= 0 )
2781 return( ret );
2782
mohammad160352aecb92018-03-28 23:41:40 -07002783 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002784 {
Darryl Green11999bb2018-03-13 15:22:58 +00002785 MBEDTLS_SSL_DEBUG_MSG( 1,
2786 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002787 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2789 }
2790
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 ssl->out_left -= ret;
2792 }
2793
Hanno Becker2b1e3542018-08-06 11:19:13 +01002794#if defined(MBEDTLS_SSL_PROTO_DTLS)
2795 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002796 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002797 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002798 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002799 else
2800#endif
2801 {
2802 ssl->out_hdr = ssl->out_buf + 8;
2803 }
2804 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 return( 0 );
2809}
2810
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002811/*
2812 * Functions to handle the DTLS retransmission state machine
2813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002815/*
2816 * Append current handshake message to current outgoing flight
2817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002819{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2822 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2823 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002824
2825 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002826 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002827 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002830 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002831 }
2832
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002833 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002834 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002837 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002838 }
2839
2840 /* Copy current handshake message with headers */
2841 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2842 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002843 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002844 msg->next = NULL;
2845
2846 /* Append to the current flight */
2847 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002848 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002849 else
2850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002852 while( cur->next != NULL )
2853 cur = cur->next;
2854 cur->next = msg;
2855 }
2856
Hanno Becker3b235902018-08-06 09:54:53 +01002857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002858 return( 0 );
2859}
2860
2861/*
2862 * Free the current flight of handshake messages
2863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 mbedtls_ssl_flight_item *cur = flight;
2867 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002868
2869 while( cur != NULL )
2870 {
2871 next = cur->next;
2872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 mbedtls_free( cur->p );
2874 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002875
2876 cur = next;
2877 }
2878}
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2881static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002882#endif
2883
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002884/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002885 * Swap transform_out and out_ctr with the alternative ones
2886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002890 unsigned char tmp_out_ctr[8];
2891
2892 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002895 return;
2896 }
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002899
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002900 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002901 tmp_transform = ssl->transform_out;
2902 ssl->transform_out = ssl->handshake->alt_transform_out;
2903 ssl->handshake->alt_transform_out = tmp_transform;
2904
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002905 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002906 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2907 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002908 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002909
2910 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002911 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2914 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2919 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002920 }
2921 }
2922#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002923}
2924
2925/*
2926 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002927 */
2928int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2929{
2930 int ret = 0;
2931
2932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2933
2934 ret = mbedtls_ssl_flight_transmit( ssl );
2935
2936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2937
2938 return( ret );
2939}
2940
2941/*
2942 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002943 *
2944 * Need to remember the current message in case flush_output returns
2945 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002946 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002947 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002948int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002949{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002950 int ret;
Andrzej Kurek6290dae2018-10-05 08:06:01 -04002951 uint16_t mtu_temp = 0;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002955 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002957
2958 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002959 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002960 ssl_swap_epochs( ssl );
2961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002963 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002964
2965 while( ssl->handshake->cur_msg != NULL )
2966 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002967 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002968 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002969
Hanno Beckere1dcb032018-08-17 16:47:58 +01002970 int const is_finished =
2971 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2972 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2973
Hanno Becker04da1892018-08-14 13:22:10 +01002974 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2975 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2976
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002977 /* Swap epochs before sending Finished: we can't do it after
2978 * sending ChangeCipherSpec, in case write returns WANT_READ.
2979 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002980 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002981 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002983 ssl_swap_epochs( ssl );
2984 }
2985
Andrzej Kurek6290dae2018-10-05 08:06:01 -04002986 /* Disable handshake mtu for client hello message to avoid fragmentation.
2987 * Setting it back after calling mbedtls_ssl_write_record */
2988 if( ssl->out_msg[0] == MBEDTLS_SSL_HS_CLIENT_HELLO )
2989 {
2990 mtu_temp = ssl->handshake->mtu;
2991 ssl->handshake->mtu = 0;
2992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "disabling fragmentation of ClientHello message" ) );
2993 }
2994
Hanno Becker67bc7c32018-08-06 11:33:50 +01002995 ret = ssl_get_remaining_payload_in_datagram( ssl );
2996 if( ret < 0 )
2997 return( ret );
2998 max_frag_len = (size_t) ret;
2999
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003000 /* CCS is copied as is, while HS messages may need fragmentation */
3001 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3002 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003003 if( max_frag_len == 0 )
3004 {
3005 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3006 return( ret );
3007
3008 continue;
3009 }
3010
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003011 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003012 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003013 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003014
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003015 /* Update position inside current message */
3016 ssl->handshake->cur_msg_p += cur->len;
3017 }
3018 else
3019 {
3020 const unsigned char * const p = ssl->handshake->cur_msg_p;
3021 const size_t hs_len = cur->len - 12;
3022 const size_t frag_off = p - ( cur->p + 12 );
3023 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003024 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003025
Hanno Beckere1dcb032018-08-17 16:47:58 +01003026 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003027 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003028 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003029 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003030
Hanno Becker67bc7c32018-08-06 11:33:50 +01003031 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3032 return( ret );
3033
3034 continue;
3035 }
3036 max_hs_frag_len = max_frag_len - 12;
3037
3038 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3039 max_hs_frag_len : rem_len;
3040
3041 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003042 {
3043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003044 (unsigned) cur_hs_frag_len,
3045 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003046 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003047
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003048 /* Messages are stored with handshake headers as if not fragmented,
3049 * copy beginning of headers then fill fragmentation fields.
3050 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3051 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003053 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3054 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3055 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3056
Hanno Becker67bc7c32018-08-06 11:33:50 +01003057 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3058 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3059 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003060
3061 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3062
Hanno Becker3f7b9732018-08-28 09:53:25 +01003063 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003064 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3065 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003066 ssl->out_msgtype = cur->type;
3067
3068 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003069 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003070 }
3071
3072 /* If done with the current message move to the next one if any */
3073 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3074 {
3075 if( cur->next != NULL )
3076 {
3077 ssl->handshake->cur_msg = cur->next;
3078 ssl->handshake->cur_msg_p = cur->next->p + 12;
3079 }
3080 else
3081 {
3082 ssl->handshake->cur_msg = NULL;
3083 ssl->handshake->cur_msg_p = NULL;
3084 }
3085 }
3086
3087 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003088 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003091 return( ret );
3092 }
Andrzej Kurek6290dae2018-10-05 08:06:01 -04003093
3094 if( mtu_temp != 0 )
3095 {
3096 ssl->handshake->mtu = mtu_temp;
3097 mtu_temp = 0;
3098 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003099 }
3100
Hanno Becker67bc7c32018-08-06 11:33:50 +01003101 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3102 return( ret );
3103
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003104 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3106 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003107 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003110 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3111 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003112
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003114
3115 return( 0 );
3116}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003117
3118/*
3119 * To be called when the last message of an incoming flight is received.
3120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003122{
3123 /* We won't need to resend that one any more */
3124 ssl_flight_free( ssl->handshake->flight );
3125 ssl->handshake->flight = NULL;
3126 ssl->handshake->cur_msg = NULL;
3127
3128 /* The next incoming flight will start with this msg_seq */
3129 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3130
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003131 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003132 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003133
Hanno Becker0271f962018-08-16 13:23:47 +01003134 /* Clear future message buffering structure. */
3135 ssl_buffering_free( ssl );
3136
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003137 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003138 ssl_set_timer( ssl, 0 );
3139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3141 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003144 }
3145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003147}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003148
3149/*
3150 * To be called when the last message of an outgoing flight is send.
3151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003153{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003154 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003155 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3158 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003161 }
3162 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003168 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003169 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170
3171/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003172 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003173 *
3174 * - fill in handshake headers
3175 * - update handshake checksum
3176 * - DTLS: save message for resending
3177 * - then pass to the record layer
3178 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003179 * DTLS: except for HelloRequest, messages are only queued, and will only be
3180 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003181 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003182 * Inputs:
3183 * - ssl->out_msglen: 4 + actual handshake message len
3184 * (4 is the size of handshake headers for TLS)
3185 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3186 * - ssl->out_msg + 4: the handshake message body
3187 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003188 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003189 * - ssl->out_msglen: the length of the record contents
3190 * (including handshake headers but excluding record headers)
3191 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003192 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003193int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003194{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003195 int ret;
3196 const size_t hs_len = ssl->out_msglen - 4;
3197 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3200
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003201 /*
3202 * Sanity checks
3203 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003204 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003205 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3206 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003207 /* In SSLv3, the client might send a NoCertificate alert. */
3208#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3209 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3210 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3211 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3212#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3213 {
3214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3216 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003217 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003219 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3220 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3221 ssl->handshake == NULL )
3222 {
3223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3225 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003228 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003229 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003231 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003234 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003235#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003236
Hanno Beckerb50a2532018-08-06 11:52:54 +01003237 /* Double-check that we did not exceed the bounds
3238 * of the outgoing record buffer.
3239 * This should never fail as the various message
3240 * writing functions must obey the bounds of the
3241 * outgoing record buffer, but better be safe.
3242 *
3243 * Note: We deliberately do not check for the MTU or MFL here.
3244 */
3245 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3246 {
3247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3248 "size %u, maximum %u",
3249 (unsigned) ssl->out_msglen,
3250 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3252 }
3253
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003254 /*
3255 * Fill handshake headers
3256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003259 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3260 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3261 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003263 /*
3264 * DTLS has additional fields in the Handshake layer,
3265 * between the length field and the actual payload:
3266 * uint16 message_seq;
3267 * uint24 fragment_offset;
3268 * uint24 fragment_length;
3269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003271 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003272 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003273 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003274 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003275 {
3276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3277 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003278 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003279 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3281 }
3282
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003283 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003284 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003285
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003286 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003287 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003288 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003289 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3290 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3291 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003292 }
3293 else
3294 {
3295 ssl->out_msg[4] = 0;
3296 ssl->out_msg[5] = 0;
3297 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003298
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003299 /* Handshake hashes are computed without fragmentation,
3300 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003301 memset( ssl->out_msg + 6, 0x00, 3 );
3302 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003305
Hanno Becker0207e532018-08-28 10:28:28 +01003306 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003307 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3308 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 }
3310
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003311 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003313 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003314 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3315 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003316 {
3317 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320 return( ret );
3321 }
3322 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003323 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003325 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003327 {
3328 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3329 return( ret );
3330 }
3331 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003332
3333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3334
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003335 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003336}
3337
3338/*
3339 * Record layer functions
3340 */
3341
3342/*
3343 * Write current record.
3344 *
3345 * Uses:
3346 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3347 * - ssl->out_msglen: length of the record content (excl headers)
3348 * - ssl->out_msg: record content
3349 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003350int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003351{
3352 int ret, done = 0;
3353 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003354 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003355
3356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003359 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003361 {
3362 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003365 return( ret );
3366 }
3367
3368 len = ssl->out_msglen;
3369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3373 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 ret = mbedtls_ssl_hw_record_write( ssl );
3378 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3381 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003382 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003383
3384 if( ret == 0 )
3385 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003386 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003388 if( !done )
3389 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003390 unsigned i;
3391 size_t protected_record_size;
3392
Paul Bakker05ef8352012-05-08 09:17:57 +00003393 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003395 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003396
Hanno Becker19859472018-08-06 09:40:20 +01003397 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003398 ssl->out_len[0] = (unsigned char)( len >> 8 );
3399 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003400
Paul Bakker48916f92012-09-16 19:57:18 +00003401 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003402 {
3403 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003406 return( ret );
3407 }
3408
3409 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003410 ssl->out_len[0] = (unsigned char)( len >> 8 );
3411 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003412 }
3413
Hanno Becker2b1e3542018-08-06 11:19:13 +01003414 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3415
3416#if defined(MBEDTLS_SSL_PROTO_DTLS)
3417 /* In case of DTLS, double-check that we don't exceed
3418 * the remaining space in the datagram. */
3419 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3420 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003421 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003422 if( ret < 0 )
3423 return( ret );
3424
3425 if( protected_record_size > (size_t) ret )
3426 {
3427 /* Should never happen */
3428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3429 }
3430 }
3431#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003434 "version = [%d:%d], msglen = %d",
3435 ssl->out_hdr[0], ssl->out_hdr[1],
3436 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003439 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003440
3441 ssl->out_left += protected_record_size;
3442 ssl->out_hdr += protected_record_size;
3443 ssl_update_out_pointers( ssl, ssl->transform_out );
3444
Hanno Becker04484622018-08-06 09:49:38 +01003445 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3446 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3447 break;
3448
3449 /* The loop goes to its end iff the counter is wrapping */
3450 if( i == ssl_ep_len( ssl ) )
3451 {
3452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3453 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 }
3456
Hanno Becker67bc7c32018-08-06 11:33:50 +01003457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003458 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3459 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003460 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003461 size_t remaining;
3462 ret = ssl_get_remaining_payload_in_datagram( ssl );
3463 if( ret < 0 )
3464 {
3465 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3466 ret );
3467 return( ret );
3468 }
3469
3470 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003471 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003472 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003473 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003474 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003475 else
3476 {
Hanno Becker513815a2018-08-20 11:56:09 +01003477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003478 }
3479 }
3480#endif /* MBEDTLS_SSL_PROTO_DTLS */
3481
3482 if( ( flush == SSL_FORCE_FLUSH ) &&
3483 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 return( ret );
3487 }
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490
3491 return( 0 );
3492}
3493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003495
3496static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3497{
3498 if( ssl->in_msglen < ssl->in_hslen ||
3499 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3500 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3501 {
3502 return( 1 );
3503 }
3504 return( 0 );
3505}
Hanno Becker44650b72018-08-16 12:51:11 +01003506
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003507static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003508{
3509 return( ( ssl->in_msg[9] << 16 ) |
3510 ( ssl->in_msg[10] << 8 ) |
3511 ssl->in_msg[11] );
3512}
3513
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003514static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003515{
3516 return( ( ssl->in_msg[6] << 16 ) |
3517 ( ssl->in_msg[7] << 8 ) |
3518 ssl->in_msg[8] );
3519}
3520
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003521static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003522{
3523 uint32_t msg_len, frag_off, frag_len;
3524
3525 msg_len = ssl_get_hs_total_len( ssl );
3526 frag_off = ssl_get_hs_frag_off( ssl );
3527 frag_len = ssl_get_hs_frag_len( ssl );
3528
3529 if( frag_off > msg_len )
3530 return( -1 );
3531
3532 if( frag_len > msg_len - frag_off )
3533 return( -1 );
3534
3535 if( frag_len + 12 > ssl->in_msglen )
3536 return( -1 );
3537
3538 return( 0 );
3539}
3540
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003541/*
3542 * Mark bits in bitmask (used for DTLS HS reassembly)
3543 */
3544static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3545{
3546 unsigned int start_bits, end_bits;
3547
3548 start_bits = 8 - ( offset % 8 );
3549 if( start_bits != 8 )
3550 {
3551 size_t first_byte_idx = offset / 8;
3552
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003553 /* Special case */
3554 if( len <= start_bits )
3555 {
3556 for( ; len != 0; len-- )
3557 mask[first_byte_idx] |= 1 << ( start_bits - len );
3558
3559 /* Avoid potential issues with offset or len becoming invalid */
3560 return;
3561 }
3562
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003563 offset += start_bits; /* Now offset % 8 == 0 */
3564 len -= start_bits;
3565
3566 for( ; start_bits != 0; start_bits-- )
3567 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3568 }
3569
3570 end_bits = len % 8;
3571 if( end_bits != 0 )
3572 {
3573 size_t last_byte_idx = ( offset + len ) / 8;
3574
3575 len -= end_bits; /* Now len % 8 == 0 */
3576
3577 for( ; end_bits != 0; end_bits-- )
3578 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3579 }
3580
3581 memset( mask + offset / 8, 0xFF, len / 8 );
3582}
3583
3584/*
3585 * Check that bitmask is full
3586 */
3587static int ssl_bitmask_check( unsigned char *mask, size_t len )
3588{
3589 size_t i;
3590
3591 for( i = 0; i < len / 8; i++ )
3592 if( mask[i] != 0xFF )
3593 return( -1 );
3594
3595 for( i = 0; i < len % 8; i++ )
3596 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3597 return( -1 );
3598
3599 return( 0 );
3600}
3601
Hanno Becker56e205e2018-08-16 09:06:12 +01003602/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003603static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003604 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003605{
Hanno Becker56e205e2018-08-16 09:06:12 +01003606 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003607
Hanno Becker56e205e2018-08-16 09:06:12 +01003608 alloc_len = 12; /* Handshake header */
3609 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003610
Hanno Beckerd07df862018-08-16 09:14:58 +01003611 if( add_bitmap )
3612 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003613
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003614 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003615}
Hanno Becker56e205e2018-08-16 09:06:12 +01003616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003618
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003619static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003620{
3621 return( ( ssl->in_msg[1] << 16 ) |
3622 ( ssl->in_msg[2] << 8 ) |
3623 ssl->in_msg[3] );
3624}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003625
Simon Butcher99000142016-10-13 17:21:01 +01003626int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003631 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003633 }
3634
Hanno Becker12555c62018-08-16 12:47:53 +01003635 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003638 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003639 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003642 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003643 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003644 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003645 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003646
Hanno Becker44650b72018-08-16 12:51:11 +01003647 if( ssl_check_hs_header( ssl ) != 0 )
3648 {
3649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3651 }
3652
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003653 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003654 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3655 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3656 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3657 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003658 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003659 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3660 {
3661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3662 recv_msg_seq,
3663 ssl->handshake->in_msg_seq ) );
3664 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3665 }
3666
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003667 /* Retransmit only on last message from previous flight, to avoid
3668 * too many retransmissions.
3669 * Besides, No sane server ever retransmits HelloVerifyRequest */
3670 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003674 "message_seq = %d, start_of_flight = %d",
3675 recv_msg_seq,
3676 ssl->handshake->in_flight_start_seq ) );
3677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003681 return( ret );
3682 }
3683 }
3684 else
3685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003687 "message_seq = %d, expected = %d",
3688 recv_msg_seq,
3689 ssl->handshake->in_msg_seq ) );
3690 }
3691
Hanno Becker90333da2017-10-10 11:27:13 +01003692 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003693 }
3694 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003695
Hanno Becker6d97ef52018-08-16 13:09:04 +01003696 /* Message reassembly is handled alongside buffering of future
3697 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003698 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003699 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003700 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003703 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003704 }
3705 }
3706 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003708 /* With TLS we don't handle fragmentation (for now) */
3709 if( ssl->in_msglen < ssl->in_hslen )
3710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3712 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003713 }
3714
Simon Butcher99000142016-10-13 17:21:01 +01003715 return( 0 );
3716}
3717
3718void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3719{
Hanno Becker0271f962018-08-16 13:23:47 +01003720 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003721
Hanno Becker0271f962018-08-16 13:23:47 +01003722 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003723 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003724 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003725 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003726
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003727 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003729 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003730 ssl->handshake != NULL )
3731 {
Hanno Becker0271f962018-08-16 13:23:47 +01003732 unsigned offset;
3733 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003734
Hanno Becker0271f962018-08-16 13:23:47 +01003735 /* Increment handshake sequence number */
3736 hs->in_msg_seq++;
3737
3738 /*
3739 * Clear up handshake buffering and reassembly structure.
3740 */
3741
3742 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003743 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003744
3745 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003746 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3747 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003748 offset++, hs_buf++ )
3749 {
3750 *hs_buf = *(hs_buf + 1);
3751 }
3752
3753 /* Create a fresh last entry */
3754 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003755 }
3756#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003757}
3758
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003759/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003760 * DTLS anti-replay: RFC 6347 4.1.2.6
3761 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003762 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3763 * Bit n is set iff record number in_window_top - n has been seen.
3764 *
3765 * Usually, in_window_top is the last record number seen and the lsb of
3766 * in_window is set. The only exception is the initial state (record number 0
3767 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3770static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003771{
3772 ssl->in_window_top = 0;
3773 ssl->in_window = 0;
3774}
3775
3776static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3777{
3778 return( ( (uint64_t) buf[0] << 40 ) |
3779 ( (uint64_t) buf[1] << 32 ) |
3780 ( (uint64_t) buf[2] << 24 ) |
3781 ( (uint64_t) buf[3] << 16 ) |
3782 ( (uint64_t) buf[4] << 8 ) |
3783 ( (uint64_t) buf[5] ) );
3784}
3785
3786/*
3787 * Return 0 if sequence number is acceptable, -1 otherwise
3788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003790{
3791 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3792 uint64_t bit;
3793
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003794 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003795 return( 0 );
3796
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003797 if( rec_seqnum > ssl->in_window_top )
3798 return( 0 );
3799
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003800 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003801
3802 if( bit >= 64 )
3803 return( -1 );
3804
3805 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3806 return( -1 );
3807
3808 return( 0 );
3809}
3810
3811/*
3812 * Update replay window on new validated record
3813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003815{
3816 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3817
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003818 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003819 return;
3820
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003821 if( rec_seqnum > ssl->in_window_top )
3822 {
3823 /* Update window_top and the contents of the window */
3824 uint64_t shift = rec_seqnum - ssl->in_window_top;
3825
3826 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003827 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003828 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003829 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003830 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003831 ssl->in_window |= 1;
3832 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003833
3834 ssl->in_window_top = rec_seqnum;
3835 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003836 else
3837 {
3838 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003839 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003840
3841 if( bit < 64 ) /* Always true, but be extra sure */
3842 ssl->in_window |= (uint64_t) 1 << bit;
3843 }
3844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003846
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003847#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003848/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003849static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3850
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003851/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003852 * Without any SSL context, check if a datagram looks like a ClientHello with
3853 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003854 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003855 *
3856 * - if cookie is valid, return 0
3857 * - if ClientHello looks superficially valid but cookie is not,
3858 * fill obuf and set olen, then
3859 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3860 * - otherwise return a specific error code
3861 */
3862static int ssl_check_dtls_clihlo_cookie(
3863 mbedtls_ssl_cookie_write_t *f_cookie_write,
3864 mbedtls_ssl_cookie_check_t *f_cookie_check,
3865 void *p_cookie,
3866 const unsigned char *cli_id, size_t cli_id_len,
3867 const unsigned char *in, size_t in_len,
3868 unsigned char *obuf, size_t buf_len, size_t *olen )
3869{
3870 size_t sid_len, cookie_len;
3871 unsigned char *p;
3872
3873 if( f_cookie_write == NULL || f_cookie_check == NULL )
3874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3875
3876 /*
3877 * Structure of ClientHello with record and handshake headers,
3878 * and expected values. We don't need to check a lot, more checks will be
3879 * done when actually parsing the ClientHello - skipping those checks
3880 * avoids code duplication and does not make cookie forging any easier.
3881 *
3882 * 0-0 ContentType type; copied, must be handshake
3883 * 1-2 ProtocolVersion version; copied
3884 * 3-4 uint16 epoch; copied, must be 0
3885 * 5-10 uint48 sequence_number; copied
3886 * 11-12 uint16 length; (ignored)
3887 *
3888 * 13-13 HandshakeType msg_type; (ignored)
3889 * 14-16 uint24 length; (ignored)
3890 * 17-18 uint16 message_seq; copied
3891 * 19-21 uint24 fragment_offset; copied, must be 0
3892 * 22-24 uint24 fragment_length; (ignored)
3893 *
3894 * 25-26 ProtocolVersion client_version; (ignored)
3895 * 27-58 Random random; (ignored)
3896 * 59-xx SessionID session_id; 1 byte len + sid_len content
3897 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3898 * ...
3899 *
3900 * Minimum length is 61 bytes.
3901 */
3902 if( in_len < 61 ||
3903 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3904 in[3] != 0 || in[4] != 0 ||
3905 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3906 {
3907 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3908 }
3909
3910 sid_len = in[59];
3911 if( sid_len > in_len - 61 )
3912 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3913
3914 cookie_len = in[60 + sid_len];
3915 if( cookie_len > in_len - 60 )
3916 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3917
3918 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3919 cli_id, cli_id_len ) == 0 )
3920 {
3921 /* Valid cookie */
3922 return( 0 );
3923 }
3924
3925 /*
3926 * If we get here, we've got an invalid cookie, let's prepare HVR.
3927 *
3928 * 0-0 ContentType type; copied
3929 * 1-2 ProtocolVersion version; copied
3930 * 3-4 uint16 epoch; copied
3931 * 5-10 uint48 sequence_number; copied
3932 * 11-12 uint16 length; olen - 13
3933 *
3934 * 13-13 HandshakeType msg_type; hello_verify_request
3935 * 14-16 uint24 length; olen - 25
3936 * 17-18 uint16 message_seq; copied
3937 * 19-21 uint24 fragment_offset; copied
3938 * 22-24 uint24 fragment_length; olen - 25
3939 *
3940 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3941 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3942 *
3943 * Minimum length is 28.
3944 */
3945 if( buf_len < 28 )
3946 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3947
3948 /* Copy most fields and adapt others */
3949 memcpy( obuf, in, 25 );
3950 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3951 obuf[25] = 0xfe;
3952 obuf[26] = 0xff;
3953
3954 /* Generate and write actual cookie */
3955 p = obuf + 28;
3956 if( f_cookie_write( p_cookie,
3957 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3958 {
3959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3960 }
3961
3962 *olen = p - obuf;
3963
3964 /* Go back and fill length fields */
3965 obuf[27] = (unsigned char)( *olen - 28 );
3966
3967 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3968 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3969 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3970
3971 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3972 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3973
3974 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3975}
3976
3977/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003978 * Handle possible client reconnect with the same UDP quadruplet
3979 * (RFC 6347 Section 4.2.8).
3980 *
3981 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3982 * that looks like a ClientHello.
3983 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003984 * - if the input looks like a ClientHello without cookies,
3985 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003986 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003987 * - if the input looks like a ClientHello with a valid cookie,
3988 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003989 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003990 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003991 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003992 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003993 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3994 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003995 */
3996static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3997{
3998 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003999 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004000
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004001 ret = ssl_check_dtls_clihlo_cookie(
4002 ssl->conf->f_cookie_write,
4003 ssl->conf->f_cookie_check,
4004 ssl->conf->p_cookie,
4005 ssl->cli_id, ssl->cli_id_len,
4006 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004007 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004008
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004009 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4010
4011 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004012 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004013 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004014 * If the error is permanent we'll catch it later,
4015 * if it's not, then hopefully it'll work next time. */
4016 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4017
4018 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004019 }
4020
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004021 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004022 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004023 /* Got a valid cookie, partially reset context */
4024 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4025 {
4026 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4027 return( ret );
4028 }
4029
4030 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004031 }
4032
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004033 return( ret );
4034}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004035#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004036
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004037/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004038 * ContentType type;
4039 * ProtocolVersion version;
4040 * uint16 epoch; // DTLS only
4041 * uint48 sequence_number; // DTLS only
4042 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004043 *
4044 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004045 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004046 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4047 *
4048 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004049 * 1. proceed with the record if this function returns 0
4050 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4051 * 3. return CLIENT_RECONNECT if this function return that value
4052 * 4. drop the whole datagram if this function returns anything else.
4053 * Point 2 is needed when the peer is resending, and we have already received
4054 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004057{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004058 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 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 +02004061
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004063 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004064 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004067 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004068 ssl->in_msgtype,
4069 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004070
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004071 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4073 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4074 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4075 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004078
4079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004080 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4081 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004082 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4083#endif /* MBEDTLS_SSL_PROTO_DTLS */
4084 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4085 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004088 }
4089
4090 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004091 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4094 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 }
4096
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004097 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4100 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004101 }
4102
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004103 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004104 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004105 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4108 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004109 }
4110
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004111 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004112 * DTLS-related tests.
4113 * Check epoch before checking length constraint because
4114 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4115 * message gets duplicated before the corresponding Finished message,
4116 * the second ChangeCipherSpec should be discarded because it belongs
4117 * to an old epoch, but not because its length is shorter than
4118 * the minimum record length for packets using the new record transform.
4119 * Note that these two kinds of failures are handled differently,
4120 * as an unexpected record is silently skipped but an invalid
4121 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004122 */
4123#if defined(MBEDTLS_SSL_PROTO_DTLS)
4124 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4125 {
4126 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4127
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004128 /* Check epoch (and sequence number) with DTLS */
4129 if( rec_epoch != ssl->in_epoch )
4130 {
4131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4132 "expected %d, received %d",
4133 ssl->in_epoch, rec_epoch ) );
4134
4135#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4136 /*
4137 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4138 * access the first byte of record content (handshake type), as we
4139 * have an active transform (possibly iv_len != 0), so use the
4140 * fact that the record header len is 13 instead.
4141 */
4142 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4143 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4144 rec_epoch == 0 &&
4145 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4146 ssl->in_left > 13 &&
4147 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4148 {
4149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4150 "from the same port" ) );
4151 return( ssl_handle_possible_reconnect( ssl ) );
4152 }
4153 else
4154#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004155 {
4156 /* Consider buffering the record. */
4157 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4158 {
4159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4160 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4161 }
4162
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004163 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004164 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004165 }
4166
4167#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4168 /* Replay detection only works for the current epoch */
4169 if( rec_epoch == ssl->in_epoch &&
4170 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4171 {
4172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4173 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4174 }
4175#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004176
Hanno Becker52c6dc62017-05-26 16:07:36 +01004177 /* Drop unexpected ApplicationData records,
4178 * except at the beginning of renegotiations */
4179 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4180 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4181#if defined(MBEDTLS_SSL_RENEGOTIATION)
4182 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4183 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4184#endif
4185 )
4186 {
4187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4188 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4189 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004190 }
4191#endif /* MBEDTLS_SSL_PROTO_DTLS */
4192
Hanno Becker52c6dc62017-05-26 16:07:36 +01004193
4194 /* Check length against bounds of the current transform and version */
4195 if( ssl->transform_in == NULL )
4196 {
4197 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004198 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
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 }
4204 else
4205 {
4206 if( ssl->in_msglen < ssl->transform_in->minlen )
4207 {
4208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4209 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4210 }
4211
4212#if defined(MBEDTLS_SSL_PROTO_SSL3)
4213 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004214 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004215 {
4216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4217 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4218 }
4219#endif
4220#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4221 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4222 /*
4223 * TLS encrypted messages can have up to 256 bytes of padding
4224 */
4225 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4226 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004227 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004228 {
4229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4230 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4231 }
4232#endif
4233 }
4234
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004235 return( 0 );
4236}
Paul Bakker5121ce52009-01-03 21:22:43 +00004237
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004238/*
4239 * If applicable, decrypt (and decompress) record content
4240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004242{
4243 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4246 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4249 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 ret = mbedtls_ssl_hw_record_read( ssl );
4254 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4257 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004258 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004259
4260 if( ret == 0 )
4261 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004264 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004265 {
4266 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004269 return( ret );
4270 }
4271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004273 ssl->in_msg, ssl->in_msglen );
4274
Angus Grattond8213d02016-05-25 20:56:48 +10004275 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4278 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 }
4280 }
4281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004283 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004285 {
4286 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004289 return( ret );
4290 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004291 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004295 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004298 }
4299#endif
4300
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004301 return( 0 );
4302}
4303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004305
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004306/*
4307 * Read a record.
4308 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004309 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4310 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4311 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004312 */
Hanno Becker1097b342018-08-15 14:09:41 +01004313
4314/* Helper functions for mbedtls_ssl_read_record(). */
4315static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004316static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4317static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004318
Hanno Becker327c93b2018-08-15 13:56:18 +01004319int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004320 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004321{
4322 int ret;
4323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004325
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004326 if( ssl->keep_current_message == 0 )
4327 {
4328 do {
Simon Butcher99000142016-10-13 17:21:01 +01004329
Hanno Becker26994592018-08-15 14:14:59 +01004330 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004331 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004332 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004333
Hanno Beckere74d5562018-08-15 14:26:08 +01004334 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004335 {
Hanno Becker40f50842018-08-15 14:48:01 +01004336#if defined(MBEDTLS_SSL_PROTO_DTLS)
4337 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004338
Hanno Becker40f50842018-08-15 14:48:01 +01004339 /* We only check for buffered messages if the
4340 * current datagram is fully consumed. */
4341 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004342 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004343 {
Hanno Becker40f50842018-08-15 14:48:01 +01004344 if( ssl_load_buffered_message( ssl ) == 0 )
4345 have_buffered = 1;
4346 }
4347
4348 if( have_buffered == 0 )
4349#endif /* MBEDTLS_SSL_PROTO_DTLS */
4350 {
4351 ret = ssl_get_next_record( ssl );
4352 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4353 continue;
4354
4355 if( ret != 0 )
4356 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004357 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004358 return( ret );
4359 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004360 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004361 }
4362
4363 ret = mbedtls_ssl_handle_message_type( ssl );
4364
Hanno Becker40f50842018-08-15 14:48:01 +01004365#if defined(MBEDTLS_SSL_PROTO_DTLS)
4366 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4367 {
4368 /* Buffer future message */
4369 ret = ssl_buffer_message( ssl );
4370 if( ret != 0 )
4371 return( ret );
4372
4373 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4374 }
4375#endif /* MBEDTLS_SSL_PROTO_DTLS */
4376
Hanno Becker90333da2017-10-10 11:27:13 +01004377 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4378 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004379
4380 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004381 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004382 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004383 return( ret );
4384 }
4385
Hanno Becker327c93b2018-08-15 13:56:18 +01004386 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004387 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004388 {
4389 mbedtls_ssl_update_handshake_status( ssl );
4390 }
Simon Butcher99000142016-10-13 17:21:01 +01004391 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004392 else
Simon Butcher99000142016-10-13 17:21:01 +01004393 {
Hanno Becker02f59072018-08-15 14:00:24 +01004394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004395 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004396 }
4397
4398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4399
4400 return( 0 );
4401}
4402
Hanno Becker40f50842018-08-15 14:48:01 +01004403#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004404static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004405{
Hanno Becker40f50842018-08-15 14:48:01 +01004406 if( ssl->in_left > ssl->next_record_offset )
4407 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004408
Hanno Becker40f50842018-08-15 14:48:01 +01004409 return( 0 );
4410}
4411
4412static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4413{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004414 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004415 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004416 int ret = 0;
4417
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004418 if( hs == NULL )
4419 return( -1 );
4420
Hanno Beckere00ae372018-08-20 09:39:42 +01004421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4422
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004423 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4424 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4425 {
4426 /* Check if we have seen a ChangeCipherSpec before.
4427 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004428 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004429 {
4430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4431 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004432 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004433 }
4434
Hanno Becker39b8bc92018-08-28 17:17:13 +01004435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004436 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4437 ssl->in_msglen = 1;
4438 ssl->in_msg[0] = 1;
4439
4440 /* As long as they are equal, the exact value doesn't matter. */
4441 ssl->in_left = 0;
4442 ssl->next_record_offset = 0;
4443
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004444 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004445 goto exit;
4446 }
Hanno Becker37f95322018-08-16 13:55:32 +01004447
Hanno Beckerb8f50142018-08-28 10:01:34 +01004448#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004449 /* Debug only */
4450 {
4451 unsigned offset;
4452 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4453 {
4454 hs_buf = &hs->buffering.hs[offset];
4455 if( hs_buf->is_valid == 1 )
4456 {
4457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4458 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004459 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004460 }
4461 }
4462 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004463#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004464
4465 /* Check if we have buffered and/or fully reassembled the
4466 * next handshake message. */
4467 hs_buf = &hs->buffering.hs[0];
4468 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4469 {
4470 /* Synthesize a record containing the buffered HS message. */
4471 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4472 ( hs_buf->data[2] << 8 ) |
4473 hs_buf->data[3];
4474
4475 /* Double-check that we haven't accidentally buffered
4476 * a message that doesn't fit into the input buffer. */
4477 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4478 {
4479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4481 }
4482
4483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4484 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4485 hs_buf->data, msg_len + 12 );
4486
4487 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4488 ssl->in_hslen = msg_len + 12;
4489 ssl->in_msglen = msg_len + 12;
4490 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4491
4492 ret = 0;
4493 goto exit;
4494 }
4495 else
4496 {
4497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4498 hs->in_msg_seq ) );
4499 }
4500
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004501 ret = -1;
4502
4503exit:
4504
4505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4506 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004507}
4508
Hanno Beckera02b0b42018-08-21 17:20:27 +01004509static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4510 size_t desired )
4511{
4512 int offset;
4513 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4515 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004516
Hanno Becker01315ea2018-08-21 17:22:17 +01004517 /* Get rid of future records epoch first, if such exist. */
4518 ssl_free_buffered_record( ssl );
4519
4520 /* Check if we have enough space available now. */
4521 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4522 hs->buffering.total_bytes_buffered ) )
4523 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004525 return( 0 );
4526 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004527
Hanno Becker4f432ad2018-08-28 10:02:32 +01004528 /* We don't have enough space to buffer the next expected handshake
4529 * message. Remove buffers used for future messages to gain space,
4530 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004531 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4532 offset >= 0; offset-- )
4533 {
4534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4535 offset ) );
4536
Hanno Beckerb309b922018-08-23 13:18:05 +01004537 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004538
4539 /* Check if we have enough space available now. */
4540 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4541 hs->buffering.total_bytes_buffered ) )
4542 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004544 return( 0 );
4545 }
4546 }
4547
4548 return( -1 );
4549}
4550
Hanno Becker40f50842018-08-15 14:48:01 +01004551static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4552{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004553 int ret = 0;
4554 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4555
4556 if( hs == NULL )
4557 return( 0 );
4558
4559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4560
4561 switch( ssl->in_msgtype )
4562 {
4563 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004565
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004566 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004567 break;
4568
4569 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004570 {
4571 unsigned recv_msg_seq_offset;
4572 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4573 mbedtls_ssl_hs_buffer *hs_buf;
4574 size_t msg_len = ssl->in_hslen - 12;
4575
4576 /* We should never receive an old handshake
4577 * message - double-check nonetheless. */
4578 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4579 {
4580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4582 }
4583
4584 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4585 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4586 {
4587 /* Silently ignore -- message too far in the future */
4588 MBEDTLS_SSL_DEBUG_MSG( 2,
4589 ( "Ignore future HS message with sequence number %u, "
4590 "buffering window %u - %u",
4591 recv_msg_seq, ssl->handshake->in_msg_seq,
4592 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4593
4594 goto exit;
4595 }
4596
4597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4598 recv_msg_seq, recv_msg_seq_offset ) );
4599
4600 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4601
4602 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004603 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004604 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004605 size_t reassembly_buf_sz;
4606
Hanno Becker37f95322018-08-16 13:55:32 +01004607 hs_buf->is_fragmented =
4608 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4609
4610 /* We copy the message back into the input buffer
4611 * after reassembly, so check that it's not too large.
4612 * This is an implementation-specific limitation
4613 * and not one from the standard, hence it is not
4614 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004615 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004616 {
4617 /* Ignore message */
4618 goto exit;
4619 }
4620
Hanno Beckere0b150f2018-08-21 15:51:03 +01004621 /* Check if we have enough space to buffer the message. */
4622 if( hs->buffering.total_bytes_buffered >
4623 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4624 {
4625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4627 }
4628
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004629 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4630 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004631
4632 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4633 hs->buffering.total_bytes_buffered ) )
4634 {
4635 if( recv_msg_seq_offset > 0 )
4636 {
4637 /* If we can't buffer a future message because
4638 * of space limitations -- ignore. */
4639 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",
4640 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4641 (unsigned) hs->buffering.total_bytes_buffered ) );
4642 goto exit;
4643 }
Hanno Beckere1801392018-08-21 16:51:05 +01004644 else
4645 {
4646 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",
4647 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4648 (unsigned) hs->buffering.total_bytes_buffered ) );
4649 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004650
Hanno Beckera02b0b42018-08-21 17:20:27 +01004651 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004652 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004653 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",
4654 (unsigned) msg_len,
4655 (unsigned) reassembly_buf_sz,
4656 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004657 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004658 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4659 goto exit;
4660 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004661 }
4662
4663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4664 msg_len ) );
4665
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004666 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4667 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004668 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004669 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004670 goto exit;
4671 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004672 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004673
4674 /* Prepare final header: copy msg_type, length and message_seq,
4675 * then add standardised fragment_offset and fragment_length */
4676 memcpy( hs_buf->data, ssl->in_msg, 6 );
4677 memset( hs_buf->data + 6, 0, 3 );
4678 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4679
4680 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004681
4682 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004683 }
4684 else
4685 {
4686 /* Make sure msg_type and length are consistent */
4687 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4688 {
4689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4690 /* Ignore */
4691 goto exit;
4692 }
4693 }
4694
Hanno Becker4422bbb2018-08-20 09:40:19 +01004695 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004696 {
4697 size_t frag_len, frag_off;
4698 unsigned char * const msg = hs_buf->data + 12;
4699
4700 /*
4701 * Check and copy current fragment
4702 */
4703
4704 /* Validation of header fields already done in
4705 * mbedtls_ssl_prepare_handshake_record(). */
4706 frag_off = ssl_get_hs_frag_off( ssl );
4707 frag_len = ssl_get_hs_frag_len( ssl );
4708
4709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4710 frag_off, frag_len ) );
4711 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4712
4713 if( hs_buf->is_fragmented )
4714 {
4715 unsigned char * const bitmask = msg + msg_len;
4716 ssl_bitmask_set( bitmask, frag_off, frag_len );
4717 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4718 msg_len ) == 0 );
4719 }
4720 else
4721 {
4722 hs_buf->is_complete = 1;
4723 }
4724
4725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4726 hs_buf->is_complete ? "" : "not yet " ) );
4727 }
4728
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004729 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004730 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004731
4732 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004733 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004734 break;
4735 }
4736
4737exit:
4738
4739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4740 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004741}
4742#endif /* MBEDTLS_SSL_PROTO_DTLS */
4743
Hanno Becker1097b342018-08-15 14:09:41 +01004744static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004745{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004746 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004747 * Consume last content-layer message and potentially
4748 * update in_msglen which keeps track of the contents'
4749 * consumption state.
4750 *
4751 * (1) Handshake messages:
4752 * Remove last handshake message, move content
4753 * and adapt in_msglen.
4754 *
4755 * (2) Alert messages:
4756 * Consume whole record content, in_msglen = 0.
4757 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004758 * (3) Change cipher spec:
4759 * Consume whole record content, in_msglen = 0.
4760 *
4761 * (4) Application data:
4762 * Don't do anything - the record layer provides
4763 * the application data as a stream transport
4764 * and consumes through mbedtls_ssl_read only.
4765 *
4766 */
4767
4768 /* Case (1): Handshake messages */
4769 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004770 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004771 /* Hard assertion to be sure that no application data
4772 * is in flight, as corrupting ssl->in_msglen during
4773 * ssl->in_offt != NULL is fatal. */
4774 if( ssl->in_offt != NULL )
4775 {
4776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4778 }
4779
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004780 /*
4781 * Get next Handshake message in the current record
4782 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004783
Hanno Becker4a810fb2017-05-24 16:27:30 +01004784 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004785 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004786 * current handshake content: If DTLS handshake
4787 * fragmentation is used, that's the fragment
4788 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004789 * size here is faulty and should be changed at
4790 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004791 * (2) While it doesn't seem to cause problems, one
4792 * has to be very careful not to assume that in_hslen
4793 * is always <= in_msglen in a sensible communication.
4794 * Again, it's wrong for DTLS handshake fragmentation.
4795 * The following check is therefore mandatory, and
4796 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004797 * Additionally, ssl->in_hslen might be arbitrarily out of
4798 * bounds after handling a DTLS message with an unexpected
4799 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004800 */
4801 if( ssl->in_hslen < ssl->in_msglen )
4802 {
4803 ssl->in_msglen -= ssl->in_hslen;
4804 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4805 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806
Hanno Becker4a810fb2017-05-24 16:27:30 +01004807 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4808 ssl->in_msg, ssl->in_msglen );
4809 }
4810 else
4811 {
4812 ssl->in_msglen = 0;
4813 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004814
Hanno Becker4a810fb2017-05-24 16:27:30 +01004815 ssl->in_hslen = 0;
4816 }
4817 /* Case (4): Application data */
4818 else if( ssl->in_offt != NULL )
4819 {
4820 return( 0 );
4821 }
4822 /* Everything else (CCS & Alerts) */
4823 else
4824 {
4825 ssl->in_msglen = 0;
4826 }
4827
Hanno Becker1097b342018-08-15 14:09:41 +01004828 return( 0 );
4829}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004830
Hanno Beckere74d5562018-08-15 14:26:08 +01004831static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4832{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004833 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004834 return( 1 );
4835
4836 return( 0 );
4837}
4838
Hanno Becker5f066e72018-08-16 14:56:31 +01004839#if defined(MBEDTLS_SSL_PROTO_DTLS)
4840
4841static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4842{
4843 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4844 if( hs == NULL )
4845 return;
4846
Hanno Becker01315ea2018-08-21 17:22:17 +01004847 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004848 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004849 hs->buffering.total_bytes_buffered -=
4850 hs->buffering.future_record.len;
4851
4852 mbedtls_free( hs->buffering.future_record.data );
4853 hs->buffering.future_record.data = NULL;
4854 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004855}
4856
4857static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4858{
4859 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4860 unsigned char * rec;
4861 size_t rec_len;
4862 unsigned rec_epoch;
4863
4864 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4865 return( 0 );
4866
4867 if( hs == NULL )
4868 return( 0 );
4869
Hanno Becker5f066e72018-08-16 14:56:31 +01004870 rec = hs->buffering.future_record.data;
4871 rec_len = hs->buffering.future_record.len;
4872 rec_epoch = hs->buffering.future_record.epoch;
4873
4874 if( rec == NULL )
4875 return( 0 );
4876
Hanno Becker4cb782d2018-08-20 11:19:05 +01004877 /* Only consider loading future records if the
4878 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004879 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004880 return( 0 );
4881
Hanno Becker5f066e72018-08-16 14:56:31 +01004882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4883
4884 if( rec_epoch != ssl->in_epoch )
4885 {
4886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4887 goto exit;
4888 }
4889
4890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4891
4892 /* Double-check that the record is not too large */
4893 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4894 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4898 }
4899
4900 memcpy( ssl->in_hdr, rec, rec_len );
4901 ssl->in_left = rec_len;
4902 ssl->next_record_offset = 0;
4903
4904 ssl_free_buffered_record( ssl );
4905
4906exit:
4907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4908 return( 0 );
4909}
4910
4911static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4912{
4913 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4914 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004915 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004916
4917 /* Don't buffer future records outside handshakes. */
4918 if( hs == NULL )
4919 return( 0 );
4920
4921 /* Only buffer handshake records (we are only interested
4922 * in Finished messages). */
4923 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4924 return( 0 );
4925
4926 /* Don't buffer more than one future epoch record. */
4927 if( hs->buffering.future_record.data != NULL )
4928 return( 0 );
4929
Hanno Becker01315ea2018-08-21 17:22:17 +01004930 /* Don't buffer record if there's not enough buffering space remaining. */
4931 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4932 hs->buffering.total_bytes_buffered ) )
4933 {
4934 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",
4935 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4936 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004937 return( 0 );
4938 }
4939
Hanno Becker5f066e72018-08-16 14:56:31 +01004940 /* Buffer record */
4941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4942 ssl->in_epoch + 1 ) );
4943 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4944 rec_hdr_len + ssl->in_msglen );
4945
4946 /* ssl_parse_record_header() only considers records
4947 * of the next epoch as candidates for buffering. */
4948 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004949 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004950
4951 hs->buffering.future_record.data =
4952 mbedtls_calloc( 1, hs->buffering.future_record.len );
4953 if( hs->buffering.future_record.data == NULL )
4954 {
4955 /* If we run out of RAM trying to buffer a
4956 * record from the next epoch, just ignore. */
4957 return( 0 );
4958 }
4959
Hanno Becker01315ea2018-08-21 17:22:17 +01004960 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004961
Hanno Becker01315ea2018-08-21 17:22:17 +01004962 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004963 return( 0 );
4964}
4965
4966#endif /* MBEDTLS_SSL_PROTO_DTLS */
4967
Hanno Beckere74d5562018-08-15 14:26:08 +01004968static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004969{
4970 int ret;
4971
Hanno Becker5f066e72018-08-16 14:56:31 +01004972#if defined(MBEDTLS_SSL_PROTO_DTLS)
4973 /* We might have buffered a future record; if so,
4974 * and if the epoch matches now, load it.
4975 * On success, this call will set ssl->in_left to
4976 * the length of the buffered record, so that
4977 * the calls to ssl_fetch_input() below will
4978 * essentially be no-ops. */
4979 ret = ssl_load_buffered_record( ssl );
4980 if( ret != 0 )
4981 return( ret );
4982#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004987 return( ret );
4988 }
4989
4990 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004993 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4994 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004995 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004996 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4997 {
4998 ret = ssl_buffer_future_record( ssl );
4999 if( ret != 0 )
5000 return( ret );
5001
5002 /* Fall through to handling of unexpected records */
5003 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5004 }
5005
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005006 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5007 {
5008 /* Skip unexpected record (but not whole datagram) */
5009 ssl->next_record_offset = ssl->in_msglen
5010 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005011
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5013 "(header)" ) );
5014 }
5015 else
5016 {
5017 /* Skip invalid record and the rest of the datagram */
5018 ssl->next_record_offset = 0;
5019 ssl->in_left = 0;
5020
5021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5022 "(header)" ) );
5023 }
5024
5025 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005026 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005027 }
5028#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005029 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005030 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005031
5032 /*
5033 * Read and optionally decrypt the message contents
5034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5036 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005039 return( ret );
5040 }
5041
5042 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005044 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005047 if( ssl->next_record_offset < ssl->in_left )
5048 {
5049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5050 }
5051 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005052 else
5053#endif
5054 ssl->in_left = 0;
5055
5056 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005060 {
5061 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5063 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005064 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005065 /* Except when waiting for Finished as a bad mac here
5066 * probably means something went wrong in the handshake
5067 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5068 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5069 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5070 {
5071#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5072 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5073 {
5074 mbedtls_ssl_send_alert_message( ssl,
5075 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5076 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5077 }
5078#endif
5079 return( ret );
5080 }
5081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005083 if( ssl->conf->badmac_limit != 0 &&
5084 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5087 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005088 }
5089#endif
5090
Hanno Becker4a810fb2017-05-24 16:27:30 +01005091 /* As above, invalid records cause
5092 * dismissal of the whole datagram. */
5093
5094 ssl->next_record_offset = 0;
5095 ssl->in_left = 0;
5096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005098 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005099 }
5100
5101 return( ret );
5102 }
5103 else
5104#endif
5105 {
5106 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5108 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005110 mbedtls_ssl_send_alert_message( ssl,
5111 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5112 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005113 }
5114#endif
5115 return( ret );
5116 }
5117 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005118
Simon Butcher99000142016-10-13 17:21:01 +01005119 return( 0 );
5120}
5121
5122int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5123{
5124 int ret;
5125
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005126 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005127 * Handle particular types of records
5128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005129 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005130 {
Simon Butcher99000142016-10-13 17:21:01 +01005131 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5132 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005133 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005135 }
5136
Hanno Beckere678eaa2018-08-21 14:57:46 +01005137 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005138 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005139 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005140 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5142 ssl->in_msglen ) );
5143 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005144 }
5145
Hanno Beckere678eaa2018-08-21 14:57:46 +01005146 if( ssl->in_msg[0] != 1 )
5147 {
5148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5149 ssl->in_msg[0] ) );
5150 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5151 }
5152
5153#if defined(MBEDTLS_SSL_PROTO_DTLS)
5154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5155 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5156 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5157 {
5158 if( ssl->handshake == NULL )
5159 {
5160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5161 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5162 }
5163
5164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5165 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5166 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005167#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005168 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005171 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005172 if( ssl->in_msglen != 2 )
5173 {
5174 /* Note: Standard allows for more than one 2 byte alert
5175 to be packed in a single message, but Mbed TLS doesn't
5176 currently support this. */
5177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5178 ssl->in_msglen ) );
5179 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5180 }
5181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005183 ssl->in_msg[0], ssl->in_msg[1] ) );
5184
5185 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005186 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005191 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005193 }
5194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5196 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5199 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005200 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005201
5202#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5203 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5204 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5205 {
Hanno Becker90333da2017-10-10 11:27:13 +01005206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005207 /* Will be handled when trying to parse ServerHello */
5208 return( 0 );
5209 }
5210#endif
5211
5212#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5213 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5214 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5215 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5216 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5217 {
5218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5219 /* Will be handled in mbedtls_ssl_parse_certificate() */
5220 return( 0 );
5221 }
5222#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5223
5224 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005225 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005226 }
5227
Hanno Beckerc76c6192017-06-06 10:03:17 +01005228#if defined(MBEDTLS_SSL_PROTO_DTLS)
5229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5230 ssl->handshake != NULL &&
5231 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5232 {
5233 ssl_handshake_wrapup_free_hs_transform( ssl );
5234 }
5235#endif
5236
Paul Bakker5121ce52009-01-03 21:22:43 +00005237 return( 0 );
5238}
5239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005241{
5242 int ret;
5243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5245 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5246 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005247 {
5248 return( ret );
5249 }
5250
5251 return( 0 );
5252}
5253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005255 unsigned char level,
5256 unsigned char message )
5257{
5258 int ret;
5259
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005260 if( ssl == NULL || ssl->conf == NULL )
5261 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005267 ssl->out_msglen = 2;
5268 ssl->out_msg[0] = level;
5269 ssl->out_msg[1] = message;
5270
Hanno Becker67bc7c32018-08-06 11:33:50 +01005271 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005274 return( ret );
5275 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005277
5278 return( 0 );
5279}
5280
Paul Bakker5121ce52009-01-03 21:22:43 +00005281/*
5282 * Handshake functions
5283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5285 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5286 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5287 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5288 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5289 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5290 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005291/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
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 Bakkerd4a56ec2013-04-16 18:05:29 +02005302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +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}
5311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5319 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005320 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5321 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005324 ssl->state++;
5325 return( 0 );
5326 }
5327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5329 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005330}
Gilles Peskinef9828522017-05-03 12:28:43 +02005331
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005332#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005333/* Some certificate support -> implement write and parse */
5334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005336{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005338 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 const mbedtls_x509_crt *crt;
5340 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5345 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005346 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5347 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005350 ssl->state++;
5351 return( 0 );
5352 }
5353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005355 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005356 {
5357 if( ssl->client_auth == 0 )
5358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005360 ssl->state++;
5361 return( 0 );
5362 }
5363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005365 /*
5366 * If using SSLv3 and got no cert, send an Alert message
5367 * (otherwise an empty Certificate message will be sent).
5368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5370 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005371 {
5372 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5374 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5375 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005378 goto write_msg;
5379 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005381 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382#endif /* MBEDTLS_SSL_CLI_C */
5383#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005384 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5389 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005390 }
5391 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005392#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005395
5396 /*
5397 * 0 . 0 handshake type
5398 * 1 . 3 handshake length
5399 * 4 . 6 length of all certs
5400 * 7 . 9 length of cert. 1
5401 * 10 . n-1 peer certificate
5402 * n . n+2 length of cert. 2
5403 * n+3 . ... upper level cert, etc.
5404 */
5405 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005407
Paul Bakker29087132010-03-21 21:03:34 +00005408 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005409 {
5410 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005411 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005414 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005416 }
5417
5418 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5419 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5420 ssl->out_msg[i + 2] = (unsigned char)( n );
5421
5422 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5423 i += n; crt = crt->next;
5424 }
5425
5426 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5427 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5428 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5429
5430 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5432 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005433
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005434#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005435write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005436#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005437
5438 ssl->state++;
5439
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005440 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005441 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005443 return( ret );
5444 }
5445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005447
Paul Bakkered27a042013-04-18 22:46:23 +02005448 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005449}
5450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005452{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00005454 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005456 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005457 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5462 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005463 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5464 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005467 ssl->state++;
5468 return( 0 );
5469 }
5470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005472 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005473 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5474 {
5475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5476 ssl->state++;
5477 return( 0 );
5478 }
5479
5480#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5481 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
5482 authmode = ssl->handshake->sni_authmode;
5483#endif
5484
5485 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5486 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005488 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005490 ssl->state++;
5491 return( 0 );
5492 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005493#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005494
Hanno Becker327c93b2018-08-15 13:56:18 +01005495 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005497 /* mbedtls_ssl_read_record may have sent an alert already. We
5498 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005500 return( ret );
5501 }
5502
5503 ssl->state++;
5504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505#if defined(MBEDTLS_SSL_SRV_C)
5506#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 /*
5508 * Check if the client sent an empty certificate
5509 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005510 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005513 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5515 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5516 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005519
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005520 /* The client was asked for a certificate but didn't send
5521 one. The client should know what's going on, so we
5522 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005523 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005524 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 return( 0 );
5526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 }
5529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5533 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005534 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5538 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5539 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5540 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005543
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005544 /* The client was asked for a certificate but didn't send
5545 one. The client should know what's going on, so we
5546 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005547 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005548 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005549 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005550 else
5551 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005552 }
5553 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5555 MBEDTLS_SSL_PROTO_TLS1_2 */
5556#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5562 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 }
5565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5567 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005570 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5571 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005573 }
5574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005576
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005579 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005580 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005581
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005582 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005586 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5587 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 }
5590
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005591 /* In case we tried to reuse a session but it failed */
5592 if( ssl->session_negotiate->peer_cert != NULL )
5593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5595 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005596 }
5597
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005598 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005603 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5604 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005605 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 }
5607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005609
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005610 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005611
5612 while( i < ssl->in_hslen )
5613 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005614 if ( i + 3 > ssl->in_hslen ) {
5615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5617 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5618 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5619 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005620 if( ssl->in_msg[i] != 0 )
5621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005623 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5624 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 }
5627
5628 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5629 | (unsigned int) ssl->in_msg[i + 2];
5630 i += 3;
5631
5632 if( n < 128 || i + n > ssl->in_hslen )
5633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005635 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5636 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 }
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005641 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005642 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005643 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005644 case 0: /*ok*/
5645 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5646 /* Ignore certificate with an unknown algorithm: maybe a
5647 prior certificate was already trusted. */
5648 break;
5649
5650 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5651 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5652 goto crt_parse_der_failed;
5653
5654 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5655 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5656 goto crt_parse_der_failed;
5657
5658 default:
5659 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5660 crt_parse_der_failed:
5661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 return( ret );
5664 }
5665
5666 i += n;
5667 }
5668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005670
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005671 /*
5672 * On client, make sure the server cert doesn't change during renego to
5673 * avoid "triple handshake" attack: https://secure-resumption.com/
5674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005676 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005678 {
5679 if( ssl->session->peer_cert == NULL )
5680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005682 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5683 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005685 }
5686
5687 if( ssl->session->peer_cert->raw.len !=
5688 ssl->session_negotiate->peer_cert->raw.len ||
5689 memcmp( ssl->session->peer_cert->raw.p,
5690 ssl->session_negotiate->peer_cert->raw.p,
5691 ssl->session->peer_cert->raw.len ) != 0 )
5692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005694 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5695 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005697 }
5698 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005700
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005701 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005703 mbedtls_x509_crt *ca_chain;
5704 mbedtls_x509_crl *ca_crl;
5705
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005706#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005707 if( ssl->handshake->sni_ca_chain != NULL )
5708 {
5709 ca_chain = ssl->handshake->sni_ca_chain;
5710 ca_crl = ssl->handshake->sni_ca_crl;
5711 }
5712 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005713#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005714 {
5715 ca_chain = ssl->conf->ca_chain;
5716 ca_crl = ssl->conf->ca_crl;
5717 }
5718
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005719 /*
5720 * Main check: verify certificate
5721 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005722 ret = mbedtls_x509_crt_verify_with_profile(
5723 ssl->session_negotiate->peer_cert,
5724 ca_chain, ca_crl,
5725 ssl->conf->cert_profile,
5726 ssl->hostname,
5727 &ssl->session_negotiate->verify_result,
5728 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005729
5730 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005733 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005734
5735 /*
5736 * Secondary checks: always done, but change 'ret' only if it was 0
5737 */
5738
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005739#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005742
5743 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005745 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005746 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005747 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005750 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005752 }
5753 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005754#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005757 ciphersuite_info,
5758 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005759 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005762 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005764 }
5765
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005766 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5767 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5768 * with details encoded in the verification flags. All other kinds
5769 * of error codes, including those from the user provided f_vrfy
5770 * functions, are treated as fatal and lead to a failure of
5771 * ssl_parse_certificate even if verification was optional. */
5772 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5773 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5774 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5775 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005776 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005777 }
5778
5779 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5780 {
5781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5782 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5783 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005784
5785 if( ret != 0 )
5786 {
5787 /* The certificate may have been rejected for several reasons.
5788 Pick one and send the corresponding alert. Which alert to send
5789 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005790 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5791 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5792 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5793 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5794 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5795 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5796 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5797 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5798 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5799 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5800 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5801 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5802 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5803 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5804 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5805 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5806 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5807 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5808 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5809 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005810 else
5811 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005812 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5813 alert );
5814 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005815
Hanno Beckere6706e62017-05-15 16:05:15 +01005816#if defined(MBEDTLS_DEBUG_C)
5817 if( ssl->session_negotiate->verify_result != 0 )
5818 {
5819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5820 ssl->session_negotiate->verify_result ) );
5821 }
5822 else
5823 {
5824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5825 }
5826#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005827 }
5828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005830
5831 return( ret );
5832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5834 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5835 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5836 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5837 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5838 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5839 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005842{
5843 int ret;
5844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 ssl->out_msglen = 1;
5849 ssl->out_msg[0] = 1;
5850
Paul Bakker5121ce52009-01-03 21:22:43 +00005851 ssl->state++;
5852
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005853 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005856 return( ret );
5857 }
5858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005860
5861 return( 0 );
5862}
5863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005865{
5866 int ret;
5867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005869
Hanno Becker327c93b2018-08-15 13:56:18 +01005870 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005873 return( ret );
5874 }
5875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005879 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5880 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 }
5883
Hanno Beckere678eaa2018-08-21 14:57:46 +01005884 /* CCS records are only accepted if they have length 1 and content '1',
5885 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005886
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005887 /*
5888 * Switch to our negotiated transform and session parameters for inbound
5889 * data.
5890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005892 ssl->transform_in = ssl->transform_negotiate;
5893 ssl->session_in = ssl->session_negotiate;
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005896 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005898#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005899 ssl_dtls_replay_reset( ssl );
5900#endif
5901
5902 /* Increment epoch */
5903 if( ++ssl->in_epoch == 0 )
5904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005906 /* This is highly unlikely to happen for legitimate reasons, so
5907 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005909 }
5910 }
5911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005913 memset( ssl->in_ctr, 0, 8 );
5914
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005915 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5918 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005923 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5924 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005926 }
5927 }
5928#endif
5929
Paul Bakker5121ce52009-01-03 21:22:43 +00005930 ssl->state++;
5931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933
5934 return( 0 );
5935}
5936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5938 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005939{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005940 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5943 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5944 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005945 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005946 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5949#if defined(MBEDTLS_SHA512_C)
5950 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005951 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5952 else
5953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954#if defined(MBEDTLS_SHA256_C)
5955 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005956 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005957 else
5958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005962 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005963 }
Paul Bakker380da532012-04-18 16:10:25 +00005964}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5969 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005970 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5971 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5974#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005975 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005978 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005981}
5982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983static void ssl_update_checksum_start( 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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5987 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005988 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5989 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005990#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5992#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005993 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005996 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005997#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005999}
6000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6002 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6003static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006004 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006005{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006006 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6007 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006008}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006009#endif
Paul Bakker380da532012-04-18 16:10:25 +00006010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6012#if defined(MBEDTLS_SHA256_C)
6013static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006014 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006015{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006016 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006017}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006018#endif
Paul Bakker380da532012-04-18 16:10:25 +00006019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020#if defined(MBEDTLS_SHA512_C)
6021static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006022 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006023{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006024 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006025}
Paul Bakker769075d2012-11-24 11:26:46 +01006026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006030static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006032{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006033 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006034 mbedtls_md5_context md5;
6035 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006036
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 unsigned char padbuf[48];
6038 unsigned char md5sum[16];
6039 unsigned char sha1sum[20];
6040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006042 if( !session )
6043 session = ssl->session;
6044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006046
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006047 mbedtls_md5_init( &md5 );
6048 mbedtls_sha1_init( &sha1 );
6049
6050 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6051 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006052
6053 /*
6054 * SSLv3:
6055 * hash =
6056 * MD5( master + pad2 +
6057 * MD5( handshake + sender + master + pad1 ) )
6058 * + SHA1( master + pad2 +
6059 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 */
6061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006063 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6064 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006068 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6069 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006073 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006074
Paul Bakker1ef83d62012-04-11 12:09:53 +00006075 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006076
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006077 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6078 mbedtls_md5_update_ret( &md5, session->master, 48 );
6079 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6080 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006082 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6083 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6084 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6085 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006086
Paul Bakker1ef83d62012-04-11 12:09:53 +00006087 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006088
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006089 mbedtls_md5_starts_ret( &md5 );
6090 mbedtls_md5_update_ret( &md5, session->master, 48 );
6091 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6092 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6093 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006094
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006095 mbedtls_sha1_starts_ret( &sha1 );
6096 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6097 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6098 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6099 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006102
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006103 mbedtls_md5_free( &md5 );
6104 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006105
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006106 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6107 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6108 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006115static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006117{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006118 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006119 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006120 mbedtls_md5_context md5;
6121 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006122 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006125 if( !session )
6126 session = ssl->session;
6127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006129
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006130 mbedtls_md5_init( &md5 );
6131 mbedtls_sha1_init( &sha1 );
6132
6133 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6134 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006135
Paul Bakker1ef83d62012-04-11 12:09:53 +00006136 /*
6137 * TLSv1:
6138 * hash = PRF( master, finished_label,
6139 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6140 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006143 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6144 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006145#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006148 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6149 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006150#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006153 ? "client finished"
6154 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006155
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006156 mbedtls_md5_finish_ret( &md5, padbuf );
6157 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006158
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006159 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006160 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006163
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006164 mbedtls_md5_free( &md5 );
6165 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006166
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006167 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6174#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006175static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006177{
6178 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006179 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006180 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006181 unsigned char padbuf[32];
6182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006184 if( !session )
6185 session = ssl->session;
6186
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006187 mbedtls_sha256_init( &sha256 );
6188
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006190
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006191 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006192
6193 /*
6194 * TLSv1.2:
6195 * hash = PRF( master, finished_label,
6196 * Hash( handshake ) )[0.11]
6197 */
6198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199#if !defined(MBEDTLS_SHA256_ALT)
6200 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006201 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006202#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006205 ? "client finished"
6206 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006207
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006208 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006209
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006210 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006211 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006214
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006215 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006216
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006217 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006224static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006226{
6227 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006228 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006229 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006230 unsigned char padbuf[48];
6231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006233 if( !session )
6234 session = ssl->session;
6235
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006236 mbedtls_sha512_init( &sha512 );
6237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006239
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006240 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006241
6242 /*
6243 * TLSv1.2:
6244 * hash = PRF( master, finished_label,
6245 * Hash( handshake ) )[0.11]
6246 */
6247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006249 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6250 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006251#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006254 ? "client finished"
6255 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006256
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006257 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006258
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006259 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006260 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006263
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006264 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006265
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006266 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#endif /* MBEDTLS_SHA512_C */
6271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006276
6277 /*
6278 * Free our handshake params
6279 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006280 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006282 ssl->handshake = NULL;
6283
6284 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006285 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006286 */
6287 if( ssl->transform )
6288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 mbedtls_ssl_transform_free( ssl->transform );
6290 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006291 }
6292 ssl->transform = ssl->transform_negotiate;
6293 ssl->transform_negotiate = NULL;
6294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006296}
6297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006299{
6300 int resume = ssl->handshake->resume;
6301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304#if defined(MBEDTLS_SSL_RENEGOTIATION)
6305 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006308 ssl->renego_records_seen = 0;
6309 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006310#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006311
6312 /*
6313 * Free the previous session and switch in the current one
6314 */
Paul Bakker0a597072012-09-25 21:55:46 +00006315 if( ssl->session )
6316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006318 /* RFC 7366 3.1: keep the EtM state */
6319 ssl->session_negotiate->encrypt_then_mac =
6320 ssl->session->encrypt_then_mac;
6321#endif
6322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 mbedtls_ssl_session_free( ssl->session );
6324 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006325 }
6326 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006327 ssl->session_negotiate = NULL;
6328
Paul Bakker0a597072012-09-25 21:55:46 +00006329 /*
6330 * Add cache entry
6331 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006332 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006333 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006334 resume == 0 )
6335 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006336 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006338 }
Paul Bakker0a597072012-09-25 21:55:46 +00006339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006341 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006342 ssl->handshake->flight != NULL )
6343 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006344 /* Cancel handshake timer */
6345 ssl_set_timer( ssl, 0 );
6346
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006347 /* Keep last flight around in case we need to resend it:
6348 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006350 }
6351 else
6352#endif
6353 ssl_handshake_wrapup_free_hs_transform( ssl );
6354
Paul Bakker48916f92012-09-16 19:57:18 +00006355 ssl->state++;
6356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006358}
6359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006361{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006362 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006365
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006366 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006368 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006369
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006370 /*
6371 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6372 * may define some other value. Currently (early 2016), no defined
6373 * ciphersuite does this (and this is unlikely to change as activity has
6374 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006379 ssl->verify_data_len = hash_len;
6380 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006381#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006382
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6385 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006386
6387 /*
6388 * In case of session resuming, invert the client and server
6389 * ChangeCipherSpec messages order.
6390 */
Paul Bakker0a597072012-09-25 21:55:46 +00006391 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006394 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006398 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006400#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006401 }
6402 else
6403 ssl->state++;
6404
Paul Bakker48916f92012-09-16 19:57:18 +00006405 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006406 * Switch to our negotiated transform and session parameters for outbound
6407 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006412 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006413 {
6414 unsigned char i;
6415
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006416 /* Remember current epoch settings for resending */
6417 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006418 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006419
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006420 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006421 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006422
6423 /* Increment epoch */
6424 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006425 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006426 break;
6427
6428 /* The loop goes to its end iff the counter is wrapping */
6429 if( i == 0 )
6430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6432 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006433 }
6434 }
6435 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006437 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006438
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006439 ssl->transform_out = ssl->transform_negotiate;
6440 ssl->session_out = ssl->session_negotiate;
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6443 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6448 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006449 }
6450 }
6451#endif
6452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006456#endif
6457
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006458 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006459 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006461 return( ret );
6462 }
6463
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006464#if defined(MBEDTLS_SSL_PROTO_DTLS)
6465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6466 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6467 {
6468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6469 return( ret );
6470 }
6471#endif
6472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006474
6475 return( 0 );
6476}
6477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006479#define SSL_MAX_HASH_LEN 36
6480#else
6481#define SSL_MAX_HASH_LEN 12
6482#endif
6483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006485{
Paul Bakker23986e52011-04-24 08:57:21 +00006486 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006487 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006488 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006491
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006492 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006493
Hanno Becker327c93b2018-08-15 13:56:18 +01006494 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006497 return( ret );
6498 }
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6504 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006506 }
6507
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006508 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509#if defined(MBEDTLS_SSL_PROTO_SSL3)
6510 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006511 hash_len = 36;
6512 else
6513#endif
6514 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6517 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006520 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6521 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 }
6524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006526 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006529 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6530 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 }
6533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006535 ssl->verify_data_len = hash_len;
6536 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006537#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006538
Paul Bakker0a597072012-09-25 21:55:46 +00006539 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006542 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006546 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006549 }
6550 else
6551 ssl->state++;
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006554 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006556#endif
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006559
6560 return( 0 );
6561}
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006564{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6568 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6569 mbedtls_md5_init( &handshake->fin_md5 );
6570 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006571 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6572 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006573#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6575#if defined(MBEDTLS_SHA256_C)
6576 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006577 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579#if defined(MBEDTLS_SHA512_C)
6580 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006581 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006584
6585 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006586
6587#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6588 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6589 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6590#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592#if defined(MBEDTLS_DHM_C)
6593 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_ECDH_C)
6596 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006597#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006598#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006599 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006600#if defined(MBEDTLS_SSL_CLI_C)
6601 handshake->ecjpake_cache = NULL;
6602 handshake->ecjpake_cache_len = 0;
6603#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006604#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006605
6606#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6607 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6608#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006609}
6610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6616 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618 mbedtls_md_init( &transform->md_ctx_enc );
6619 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006620}
6621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006625}
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006628{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006629 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006630 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006632 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006634 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006635 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006636
6637 /*
6638 * Either the pointers are now NULL or cleared properly and can be freed.
6639 * Now allocate missing structures.
6640 */
6641 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006642 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006643 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006644 }
Paul Bakker48916f92012-09-16 19:57:18 +00006645
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006646 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006647 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006648 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006649 }
Paul Bakker48916f92012-09-16 19:57:18 +00006650
Paul Bakker82788fb2014-10-20 13:59:19 +02006651 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006652 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006653 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006654 }
Paul Bakker48916f92012-09-16 19:57:18 +00006655
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006656 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006657 if( ssl->handshake == NULL ||
6658 ssl->transform_negotiate == NULL ||
6659 ssl->session_negotiate == NULL )
6660 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 mbedtls_free( ssl->handshake );
6664 mbedtls_free( ssl->transform_negotiate );
6665 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006666
6667 ssl->handshake = NULL;
6668 ssl->transform_negotiate = NULL;
6669 ssl->session_negotiate = NULL;
6670
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006671 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006672 }
6673
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006674 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006676 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006677 ssl_handshake_params_init( ssl->handshake );
6678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6681 {
6682 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006683
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006684 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6685 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6686 else
6687 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006688
6689 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006690 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006691#endif
6692
Paul Bakker48916f92012-09-16 19:57:18 +00006693 return( 0 );
6694}
6695
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006696#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006697/* Dummy cookie callbacks for defaults */
6698static int ssl_cookie_write_dummy( void *ctx,
6699 unsigned char **p, unsigned char *end,
6700 const unsigned char *cli_id, size_t cli_id_len )
6701{
6702 ((void) ctx);
6703 ((void) p);
6704 ((void) end);
6705 ((void) cli_id);
6706 ((void) cli_id_len);
6707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006709}
6710
6711static int ssl_cookie_check_dummy( void *ctx,
6712 const unsigned char *cookie, size_t cookie_len,
6713 const unsigned char *cli_id, size_t cli_id_len )
6714{
6715 ((void) ctx);
6716 ((void) cookie);
6717 ((void) cookie_len);
6718 ((void) cli_id);
6719 ((void) cli_id_len);
6720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006722}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006723#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006724
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006725/* Once ssl->out_hdr as the address of the beginning of the
6726 * next outgoing record is set, deduce the other pointers.
6727 *
6728 * Note: For TLS, we save the implicit record sequence number
6729 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6730 * and the caller has to make sure there's space for this.
6731 */
6732
6733static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6734 mbedtls_ssl_transform *transform )
6735{
6736#if defined(MBEDTLS_SSL_PROTO_DTLS)
6737 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6738 {
6739 ssl->out_ctr = ssl->out_hdr + 3;
6740 ssl->out_len = ssl->out_hdr + 11;
6741 ssl->out_iv = ssl->out_hdr + 13;
6742 }
6743 else
6744#endif
6745 {
6746 ssl->out_ctr = ssl->out_hdr - 8;
6747 ssl->out_len = ssl->out_hdr + 3;
6748 ssl->out_iv = ssl->out_hdr + 5;
6749 }
6750
6751 /* Adjust out_msg to make space for explicit IV, if used. */
6752 if( transform != NULL &&
6753 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6754 {
6755 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6756 }
6757 else
6758 ssl->out_msg = ssl->out_iv;
6759}
6760
6761/* Once ssl->in_hdr as the address of the beginning of the
6762 * next incoming record is set, deduce the other pointers.
6763 *
6764 * Note: For TLS, we save the implicit record sequence number
6765 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6766 * and the caller has to make sure there's space for this.
6767 */
6768
6769static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6770 mbedtls_ssl_transform *transform )
6771{
6772#if defined(MBEDTLS_SSL_PROTO_DTLS)
6773 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6774 {
6775 ssl->in_ctr = ssl->in_hdr + 3;
6776 ssl->in_len = ssl->in_hdr + 11;
6777 ssl->in_iv = ssl->in_hdr + 13;
6778 }
6779 else
6780#endif
6781 {
6782 ssl->in_ctr = ssl->in_hdr - 8;
6783 ssl->in_len = ssl->in_hdr + 3;
6784 ssl->in_iv = ssl->in_hdr + 5;
6785 }
6786
6787 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6788 if( transform != NULL &&
6789 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6790 {
6791 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6792 }
6793 else
6794 ssl->in_msg = ssl->in_iv;
6795}
6796
Paul Bakker5121ce52009-01-03 21:22:43 +00006797/*
6798 * Initialize an SSL context
6799 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006800void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6801{
6802 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6803}
6804
6805/*
6806 * Setup an SSL context
6807 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006808
6809static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6810{
6811 /* Set the incoming and outgoing record pointers. */
6812#if defined(MBEDTLS_SSL_PROTO_DTLS)
6813 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6814 {
6815 ssl->out_hdr = ssl->out_buf;
6816 ssl->in_hdr = ssl->in_buf;
6817 }
6818 else
6819#endif /* MBEDTLS_SSL_PROTO_DTLS */
6820 {
6821 ssl->out_hdr = ssl->out_buf + 8;
6822 ssl->in_hdr = ssl->in_buf + 8;
6823 }
6824
6825 /* Derive other internal pointers. */
6826 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6827 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6828}
6829
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006830int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006831 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006832{
Paul Bakker48916f92012-09-16 19:57:18 +00006833 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006835 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006836
6837 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006838 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006839 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006840
6841 /* Set to NULL in case of an error condition */
6842 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006843
Angus Grattond8213d02016-05-25 20:56:48 +10006844 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6845 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006846 {
Angus Grattond8213d02016-05-25 20:56:48 +10006847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006848 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006849 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006850 }
6851
6852 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6853 if( ssl->out_buf == NULL )
6854 {
6855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006856 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006857 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006858 }
6859
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006860 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006861
Paul Bakker48916f92012-09-16 19:57:18 +00006862 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006863 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
6865 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006866
6867error:
6868 mbedtls_free( ssl->in_buf );
6869 mbedtls_free( ssl->out_buf );
6870
6871 ssl->conf = NULL;
6872
6873 ssl->in_buf = NULL;
6874 ssl->out_buf = NULL;
6875
6876 ssl->in_hdr = NULL;
6877 ssl->in_ctr = NULL;
6878 ssl->in_len = NULL;
6879 ssl->in_iv = NULL;
6880 ssl->in_msg = NULL;
6881
6882 ssl->out_hdr = NULL;
6883 ssl->out_ctr = NULL;
6884 ssl->out_len = NULL;
6885 ssl->out_iv = NULL;
6886 ssl->out_msg = NULL;
6887
k-stachowiak9f7798e2018-07-31 16:52:32 +02006888 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006889}
6890
6891/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006892 * Reset an initialized and used SSL context for re-use while retaining
6893 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006894 *
6895 * If partial is non-zero, keep data in the input buffer and client ID.
6896 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006897 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006898static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006899{
Paul Bakker48916f92012-09-16 19:57:18 +00006900 int ret;
6901
Hanno Becker7e772132018-08-10 12:38:21 +01006902#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6903 !defined(MBEDTLS_SSL_SRV_C)
6904 ((void) partial);
6905#endif
6906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006908
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006909 /* Cancel any possibly running timer */
6910 ssl_set_timer( ssl, 0 );
6911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_SSL_RENEGOTIATION)
6913 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006914 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006915
6916 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6918 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006921
Paul Bakker7eb013f2011-10-06 12:37:39 +00006922 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006923 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006924
6925 ssl->in_msgtype = 0;
6926 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006928 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006929 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006932 ssl_dtls_replay_reset( ssl );
6933#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006934
6935 ssl->in_hslen = 0;
6936 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006937
6938 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006939
6940 ssl->out_msgtype = 0;
6941 ssl->out_msglen = 0;
6942 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6944 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006945 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006946#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006947
Hanno Becker19859472018-08-06 09:40:20 +01006948 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6949
Paul Bakker48916f92012-09-16 19:57:18 +00006950 ssl->transform_in = NULL;
6951 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006952
Hanno Becker78640902018-08-13 16:35:15 +01006953 ssl->session_in = NULL;
6954 ssl->session_out = NULL;
6955
Angus Grattond8213d02016-05-25 20:56:48 +10006956 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006957
6958#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006959 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006960#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
6961 {
6962 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10006963 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006964 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6967 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6970 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6973 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006974 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006975 }
6976#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006977
Paul Bakker48916f92012-09-16 19:57:18 +00006978 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 mbedtls_ssl_transform_free( ssl->transform );
6981 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006982 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006983 }
Paul Bakker48916f92012-09-16 19:57:18 +00006984
Paul Bakkerc0463502013-02-14 11:19:38 +01006985 if( ssl->session )
6986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 mbedtls_ssl_session_free( ssl->session );
6988 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006989 ssl->session = NULL;
6990 }
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006993 ssl->alpn_chosen = NULL;
6994#endif
6995
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006996#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01006997#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006998 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006999#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007000 {
7001 mbedtls_free( ssl->cli_id );
7002 ssl->cli_id = NULL;
7003 ssl->cli_id_len = 0;
7004 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007005#endif
7006
Paul Bakker48916f92012-09-16 19:57:18 +00007007 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7008 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007009
7010 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007011}
7012
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007013/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007014 * Reset an initialized and used SSL context for re-use while retaining
7015 * all application-set variables, function pointers and data.
7016 */
7017int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7018{
7019 return( ssl_session_reset_int( ssl, 0 ) );
7020}
7021
7022/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007023 * SSL set accessors
7024 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007025void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007026{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007027 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007028}
7029
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007030void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007032 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007033}
7034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007036void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007037{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007038 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007039}
7040#endif
7041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007043void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007044{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007045 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007046}
7047#endif
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007050
Hanno Becker1841b0a2018-08-24 11:13:57 +01007051void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7052 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007053{
7054 ssl->disable_datagram_packing = !allow_packing;
7055}
7056
7057void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7058 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007060 conf->hs_timeout_min = min;
7061 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007062}
7063#endif
7064
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007065void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007066{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007067 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007068}
7069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007071void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007072 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007073 void *p_vrfy )
7074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007075 conf->f_vrfy = f_vrfy;
7076 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007079
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007080void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007081 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007082 void *p_rng )
7083{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007084 conf->f_rng = f_rng;
7085 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007086}
7087
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007088void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007089 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007090 void *p_dbg )
7091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007092 conf->f_dbg = f_dbg;
7093 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007094}
7095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007097 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007098 mbedtls_ssl_send_t *f_send,
7099 mbedtls_ssl_recv_t *f_recv,
7100 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007101{
7102 ssl->p_bio = p_bio;
7103 ssl->f_send = f_send;
7104 ssl->f_recv = f_recv;
7105 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007106}
7107
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007108#if defined(MBEDTLS_SSL_PROTO_DTLS)
7109void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7110{
7111 ssl->mtu = mtu;
7112}
7113#endif
7114
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007115void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007116{
7117 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007118}
7119
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007120void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7121 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007122 mbedtls_ssl_set_timer_t *f_set_timer,
7123 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007124{
7125 ssl->p_timer = p_timer;
7126 ssl->f_set_timer = f_set_timer;
7127 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007128
7129 /* Make sure we start with no timer running */
7130 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007131}
7132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007134void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007135 void *p_cache,
7136 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7137 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007138{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007139 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007140 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007141 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#if defined(MBEDTLS_SSL_CLI_C)
7146int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007147{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007148 int ret;
7149
7150 if( ssl == NULL ||
7151 session == NULL ||
7152 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007153 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007156 }
7157
7158 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7159 return( ret );
7160
Paul Bakker0a597072012-09-25 21:55:46 +00007161 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007162
7163 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007166
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007167void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007168 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007169{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007170 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7171 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7172 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7173 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007174}
7175
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007176void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007177 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007178 int major, int minor )
7179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007181 return;
7182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007184 return;
7185
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007186 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007187}
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007190void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007191 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007192{
7193 conf->cert_profile = profile;
7194}
7195
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007196/* Append a new keycert entry to a (possibly empty) list */
7197static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7198 mbedtls_x509_crt *cert,
7199 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007200{
niisato8ee24222018-06-25 19:05:48 +09007201 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007202
niisato8ee24222018-06-25 19:05:48 +09007203 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7204 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007205 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007206
niisato8ee24222018-06-25 19:05:48 +09007207 new_cert->cert = cert;
7208 new_cert->key = key;
7209 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007210
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007211 /* Update head is the list was null, else add to the end */
7212 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007213 {
niisato8ee24222018-06-25 19:05:48 +09007214 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007215 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007216 else
7217 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007218 mbedtls_ssl_key_cert *cur = *head;
7219 while( cur->next != NULL )
7220 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007221 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007222 }
7223
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007224 return( 0 );
7225}
7226
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007227int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007228 mbedtls_x509_crt *own_cert,
7229 mbedtls_pk_context *pk_key )
7230{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007231 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007232}
7233
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007234void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007235 mbedtls_x509_crt *ca_chain,
7236 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007237{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007238 conf->ca_chain = ca_chain;
7239 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007242
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007243#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7244int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7245 mbedtls_x509_crt *own_cert,
7246 mbedtls_pk_context *pk_key )
7247{
7248 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7249 own_cert, pk_key ) );
7250}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007251
7252void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7253 mbedtls_x509_crt *ca_chain,
7254 mbedtls_x509_crl *ca_crl )
7255{
7256 ssl->handshake->sni_ca_chain = ca_chain;
7257 ssl->handshake->sni_ca_crl = ca_crl;
7258}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007259
7260void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7261 int authmode )
7262{
7263 ssl->handshake->sni_authmode = authmode;
7264}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007265#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7266
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007267#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007268/*
7269 * Set EC J-PAKE password for current handshake
7270 */
7271int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7272 const unsigned char *pw,
7273 size_t pw_len )
7274{
7275 mbedtls_ecjpake_role role;
7276
Janos Follath8eb64132016-06-03 15:40:57 +01007277 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007278 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7279
7280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7281 role = MBEDTLS_ECJPAKE_SERVER;
7282 else
7283 role = MBEDTLS_ECJPAKE_CLIENT;
7284
7285 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7286 role,
7287 MBEDTLS_MD_SHA256,
7288 MBEDTLS_ECP_DP_SECP256R1,
7289 pw, pw_len ) );
7290}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007291#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007294int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007295 const unsigned char *psk, size_t psk_len,
7296 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007297{
Paul Bakker6db455e2013-09-18 17:29:31 +02007298 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007303
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007304 /* Identity len will be encoded on two bytes */
7305 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007306 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007307 {
7308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7309 }
7310
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007311 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007312 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007313 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007314
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007315 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007316 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007317 conf->psk_len = 0;
7318 }
7319 if( conf->psk_identity != NULL )
7320 {
7321 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007322 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007323 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007324 }
7325
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007326 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7327 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007328 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007329 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007330 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007331 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007332 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007333 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007334 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007335
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007336 conf->psk_len = psk_len;
7337 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007338
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007339 memcpy( conf->psk, psk, conf->psk_len );
7340 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007341
7342 return( 0 );
7343}
7344
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007345int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7346 const unsigned char *psk, size_t psk_len )
7347{
7348 if( psk == NULL || ssl->handshake == NULL )
7349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7350
7351 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7353
7354 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007355 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007356 mbedtls_platform_zeroize( ssl->handshake->psk,
7357 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007358 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007359 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007360 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007361
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007362 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007363 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007364
7365 ssl->handshake->psk_len = psk_len;
7366 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7367
7368 return( 0 );
7369}
7370
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007371void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007373 size_t),
7374 void *p_psk )
7375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007376 conf->f_psk = f_psk;
7377 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007380
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007381#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007382
7383#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007384int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007385{
7386 int ret;
7387
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007388 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7389 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7390 {
7391 mbedtls_mpi_free( &conf->dhm_P );
7392 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007393 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007394 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007395
7396 return( 0 );
7397}
Hanno Becker470a8c42017-10-04 15:28:46 +01007398#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007399
Hanno Beckera90658f2017-10-04 15:29:08 +01007400int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7401 const unsigned char *dhm_P, size_t P_len,
7402 const unsigned char *dhm_G, size_t G_len )
7403{
7404 int ret;
7405
7406 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7407 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7408 {
7409 mbedtls_mpi_free( &conf->dhm_P );
7410 mbedtls_mpi_free( &conf->dhm_G );
7411 return( ret );
7412 }
7413
7414 return( 0 );
7415}
Paul Bakker5121ce52009-01-03 21:22:43 +00007416
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007417int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007418{
7419 int ret;
7420
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007421 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7422 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7423 {
7424 mbedtls_mpi_free( &conf->dhm_P );
7425 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007426 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007427 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007428
7429 return( 0 );
7430}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007431#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007432
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007433#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7434/*
7435 * Set the minimum length for Diffie-Hellman parameters
7436 */
7437void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7438 unsigned int bitlen )
7439{
7440 conf->dhm_min_bitlen = bitlen;
7441}
7442#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7443
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007444#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007445/*
7446 * Set allowed/preferred hashes for handshake signatures
7447 */
7448void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7449 const int *hashes )
7450{
7451 conf->sig_hashes = hashes;
7452}
Hanno Becker947194e2017-04-07 13:25:49 +01007453#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007454
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007455#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007456/*
7457 * Set the allowed elliptic curves
7458 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007459void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007460 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007461{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007462 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007463}
Hanno Becker947194e2017-04-07 13:25:49 +01007464#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007465
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007466#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007468{
Hanno Becker947194e2017-04-07 13:25:49 +01007469 /* Initialize to suppress unnecessary compiler warning */
7470 size_t hostname_len = 0;
7471
7472 /* Check if new hostname is valid before
7473 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007474 if( hostname != NULL )
7475 {
7476 hostname_len = strlen( hostname );
7477
7478 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7480 }
7481
7482 /* Now it's clear that we will overwrite the old hostname,
7483 * so we can free it safely */
7484
7485 if( ssl->hostname != NULL )
7486 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007487 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007488 mbedtls_free( ssl->hostname );
7489 }
7490
7491 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007492
Paul Bakker5121ce52009-01-03 21:22:43 +00007493 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007494 {
7495 ssl->hostname = NULL;
7496 }
7497 else
7498 {
7499 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007500 if( ssl->hostname == NULL )
7501 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007502
Hanno Becker947194e2017-04-07 13:25:49 +01007503 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007504
Hanno Becker947194e2017-04-07 13:25:49 +01007505 ssl->hostname[hostname_len] = '\0';
7506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007507
7508 return( 0 );
7509}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007510#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007511
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007512#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007513void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007515 const unsigned char *, size_t),
7516 void *p_sni )
7517{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007518 conf->f_sni = f_sni;
7519 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007524int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007525{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007526 size_t cur_len, tot_len;
7527 const char **p;
7528
7529 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007530 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7531 * MUST NOT be truncated."
7532 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007533 */
7534 tot_len = 0;
7535 for( p = protos; *p != NULL; p++ )
7536 {
7537 cur_len = strlen( *p );
7538 tot_len += cur_len;
7539
7540 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007542 }
7543
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007544 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007545
7546 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007547}
7548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007550{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007551 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007554
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007555void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007556{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007557 conf->max_major_ver = major;
7558 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007559}
7560
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007561void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007563 conf->min_major_ver = major;
7564 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007565}
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007568void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007569{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007570 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007571}
7572#endif
7573
Janos Follath088ce432017-04-10 12:42:31 +01007574#if defined(MBEDTLS_SSL_SRV_C)
7575void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7576 char cert_req_ca_list )
7577{
7578 conf->cert_req_ca_list = cert_req_ca_list;
7579}
7580#endif
7581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007583void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007585 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007586}
7587#endif
7588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007590void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007592 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007593}
7594#endif
7595
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007596#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007597void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007599 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007600}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007601#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007604int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007607 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007610 }
7611
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007612 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007613
7614 return( 0 );
7615}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007619void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007620{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007621 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007622}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007626void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007627{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007628 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007629}
7630#endif
7631
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007632void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007634 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007635}
7636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007638void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007640 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007641}
7642
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007643void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007644{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007645 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007646}
7647
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007648void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007649 const unsigned char period[8] )
7650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007651 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007656#if defined(MBEDTLS_SSL_CLI_C)
7657void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007658{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007659 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007660}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007661#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007662
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007663#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007664void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7665 mbedtls_ssl_ticket_write_t *f_ticket_write,
7666 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7667 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007668{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007669 conf->f_ticket_write = f_ticket_write;
7670 conf->f_ticket_parse = f_ticket_parse;
7671 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007672}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007675
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007676#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7677void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7678 mbedtls_ssl_export_keys_t *f_export_keys,
7679 void *p_export_keys )
7680{
7681 conf->f_export_keys = f_export_keys;
7682 conf->p_export_keys = p_export_keys;
7683}
7684#endif
7685
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007686#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007687void mbedtls_ssl_conf_async_private_cb(
7688 mbedtls_ssl_config *conf,
7689 mbedtls_ssl_async_sign_t *f_async_sign,
7690 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7691 mbedtls_ssl_async_resume_t *f_async_resume,
7692 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007693 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007694{
7695 conf->f_async_sign_start = f_async_sign;
7696 conf->f_async_decrypt_start = f_async_decrypt;
7697 conf->f_async_resume = f_async_resume;
7698 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007699 conf->p_async_config_data = async_config_data;
7700}
7701
Gilles Peskine8f97af72018-04-26 11:46:10 +02007702void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7703{
7704 return( conf->p_async_config_data );
7705}
7706
Gilles Peskine1febfef2018-04-30 11:54:39 +02007707void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007708{
7709 if( ssl->handshake == NULL )
7710 return( NULL );
7711 else
7712 return( ssl->handshake->user_async_ctx );
7713}
7714
Gilles Peskine1febfef2018-04-30 11:54:39 +02007715void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007716 void *ctx )
7717{
7718 if( ssl->handshake != NULL )
7719 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007720}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007721#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007722
Paul Bakker5121ce52009-01-03 21:22:43 +00007723/*
7724 * SSL get accessors
7725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007727{
7728 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7729}
7730
Hanno Becker8b170a02017-10-10 11:51:19 +01007731int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7732{
7733 /*
7734 * Case A: We're currently holding back
7735 * a message for further processing.
7736 */
7737
7738 if( ssl->keep_current_message == 1 )
7739 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007741 return( 1 );
7742 }
7743
7744 /*
7745 * Case B: Further records are pending in the current datagram.
7746 */
7747
7748#if defined(MBEDTLS_SSL_PROTO_DTLS)
7749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7750 ssl->in_left > ssl->next_record_offset )
7751 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007753 return( 1 );
7754 }
7755#endif /* MBEDTLS_SSL_PROTO_DTLS */
7756
7757 /*
7758 * Case C: A handshake message is being processed.
7759 */
7760
Hanno Becker8b170a02017-10-10 11:51:19 +01007761 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7762 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007763 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007764 return( 1 );
7765 }
7766
7767 /*
7768 * Case D: An application data message is being processed
7769 */
7770 if( ssl->in_offt != NULL )
7771 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007773 return( 1 );
7774 }
7775
7776 /*
7777 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007778 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007779 * we implement support for multiple alerts in single records.
7780 */
7781
7782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7783 return( 0 );
7784}
7785
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007786uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007787{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007788 if( ssl->session != NULL )
7789 return( ssl->session->verify_result );
7790
7791 if( ssl->session_negotiate != NULL )
7792 return( ssl->session_negotiate->verify_result );
7793
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007794 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007795}
7796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007798{
Paul Bakker926c8e42013-03-06 10:23:34 +01007799 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007800 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007803}
7804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007806{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007808 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007809 {
7810 switch( ssl->minor_ver )
7811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007813 return( "DTLSv1.0" );
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007816 return( "DTLSv1.2" );
7817
7818 default:
7819 return( "unknown (DTLS)" );
7820 }
7821 }
7822#endif
7823
Paul Bakker43ca69c2011-01-15 17:35:19 +00007824 switch( ssl->minor_ver )
7825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007827 return( "SSLv3.0" );
7828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007830 return( "TLSv1.0" );
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007833 return( "TLSv1.1" );
7834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007836 return( "TLSv1.2" );
7837
Paul Bakker43ca69c2011-01-15 17:35:19 +00007838 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007839 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007840 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007841}
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007844{
Hanno Becker3136ede2018-08-17 15:28:19 +01007845 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007847 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007848
Hanno Becker78640902018-08-13 16:35:15 +01007849 if( transform == NULL )
7850 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#if defined(MBEDTLS_ZLIB_SUPPORT)
7853 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7854 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007855#endif
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 case MBEDTLS_MODE_GCM:
7860 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007861 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007863 transform_expansion = transform->minlen;
7864 break;
7865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007867
7868 block_size = mbedtls_cipher_get_block_size(
7869 &transform->cipher_ctx_enc );
7870
Hanno Becker3136ede2018-08-17 15:28:19 +01007871 /* Expansion due to the addition of the MAC. */
7872 transform_expansion += transform->maclen;
7873
7874 /* Expansion due to the addition of CBC padding;
7875 * Theoretically up to 256 bytes, but we never use
7876 * more than the block size of the underlying cipher. */
7877 transform_expansion += block_size;
7878
7879 /* For TLS 1.1 or higher, an explicit IV is added
7880 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007881#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7882 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007883 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007884#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007885
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007886 break;
7887
7888 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007891 }
7892
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007893 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007894}
7895
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007896#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7897size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7898{
7899 size_t max_len;
7900
7901 /*
7902 * Assume mfl_code is correct since it was checked when set
7903 */
Angus Grattond8213d02016-05-25 20:56:48 +10007904 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007905
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007906 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007907 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007908 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007909 {
Angus Grattond8213d02016-05-25 20:56:48 +10007910 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007911 }
7912
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007913 /* During a handshake, use the value being negotiated */
7914 if( ssl->session_negotiate != NULL &&
7915 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7916 {
7917 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7918 }
7919
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007920 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007921}
7922#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7923
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007924#if defined(MBEDTLS_SSL_PROTO_DTLS)
7925static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7926{
7927 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7928 return( ssl->mtu );
7929
7930 if( ssl->mtu == 0 )
7931 return( ssl->handshake->mtu );
7932
7933 return( ssl->mtu < ssl->handshake->mtu ?
7934 ssl->mtu : ssl->handshake->mtu );
7935}
7936#endif /* MBEDTLS_SSL_PROTO_DTLS */
7937
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007938int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7939{
7940 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7941
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007942#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7943 !defined(MBEDTLS_SSL_PROTO_DTLS)
7944 (void) ssl;
7945#endif
7946
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007947#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7948 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7949
7950 if( max_len > mfl )
7951 max_len = mfl;
7952#endif
7953
7954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007955 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007956 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007957 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007958 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7959 const size_t overhead = (size_t) ret;
7960
7961 if( ret < 0 )
7962 return( ret );
7963
7964 if( mtu <= overhead )
7965 {
7966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7967 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7968 }
7969
7970 if( max_len > mtu - overhead )
7971 max_len = mtu - overhead;
7972 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007973#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007974
Hanno Becker0defedb2018-08-10 12:35:02 +01007975#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7976 !defined(MBEDTLS_SSL_PROTO_DTLS)
7977 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007978#endif
7979
7980 return( (int) max_len );
7981}
7982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_X509_CRT_PARSE_C)
7984const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007985{
7986 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007987 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007988
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007989 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#if defined(MBEDTLS_SSL_CLI_C)
7994int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007995{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007996 if( ssl == NULL ||
7997 dst == NULL ||
7998 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007999 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008002 }
8003
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008004 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008007
Paul Bakker5121ce52009-01-03 21:22:43 +00008008/*
Paul Bakker1961b702013-01-25 14:49:24 +01008009 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008014
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008015 if( ssl == NULL || ssl->conf == NULL )
8016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008019 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008021#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008023 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008025#endif
8026
Paul Bakker1961b702013-01-25 14:49:24 +01008027 return( ret );
8028}
8029
8030/*
8031 * Perform the SSL handshake
8032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008034{
8035 int ret = 0;
8036
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008037 if( ssl == NULL || ssl->conf == NULL )
8038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008045
8046 if( ret != 0 )
8047 break;
8048 }
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008051
8052 return( ret );
8053}
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055#if defined(MBEDTLS_SSL_RENEGOTIATION)
8056#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008057/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008058 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008061{
8062 int ret;
8063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008065
8066 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8068 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008069
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008070 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008071 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008073 return( ret );
8074 }
8075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008077
8078 return( 0 );
8079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008081
8082/*
8083 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084 * - any side: calling mbedtls_ssl_renegotiate(),
8085 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8086 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008087 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008088 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008092{
8093 int ret;
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008096
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008097 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8098 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008099
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008100 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8101 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008103 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008105 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008106 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008107 ssl->handshake->out_msg_seq = 1;
8108 else
8109 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008110 }
8111#endif
8112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8114 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008119 return( ret );
8120 }
8121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008123
8124 return( 0 );
8125}
8126
8127/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008128 * Renegotiate current connection on client,
8129 * or request renegotiation on server
8130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008134
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008135 if( ssl == NULL || ssl->conf == NULL )
8136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008139 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008140 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008146
8147 /* Did we already try/start sending HelloRequest? */
8148 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008150
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008151 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008152 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008156 /*
8157 * On client, either start the renegotiation process or,
8158 * if already in progress, continue the handshake
8159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008164
8165 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008168 return( ret );
8169 }
8170 }
8171 else
8172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008176 return( ret );
8177 }
8178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008180
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008181 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008182}
8183
8184/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008185 * Check record counters and renegotiate if they're above the limit.
8186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008188{
Andres AG2196c7f2016-12-15 17:01:16 +00008189 size_t ep_len = ssl_ep_len( ssl );
8190 int in_ctr_cmp;
8191 int out_ctr_cmp;
8192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8194 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008195 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008196 {
8197 return( 0 );
8198 }
8199
Andres AG2196c7f2016-12-15 17:01:16 +00008200 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8201 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008202 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008203 ssl->conf->renego_period + ep_len, 8 - ep_len );
8204
8205 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008206 {
8207 return( 0 );
8208 }
8209
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008214
8215/*
8216 * Receive application data decrypted from the SSL layer
8217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008219{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008220 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008221 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008222
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008223 if( ssl == NULL || ssl->conf == NULL )
8224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008232 return( ret );
8233
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008234 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008236 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008237 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008238 return( ret );
8239 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008240 }
8241#endif
8242
Hanno Becker4a810fb2017-05-24 16:27:30 +01008243 /*
8244 * Check if renegotiation is necessary and/or handshake is
8245 * in process. If yes, perform/continue, and fall through
8246 * if an unexpected packet is received while the client
8247 * is waiting for the ServerHello.
8248 *
8249 * (There is no equivalent to the last condition on
8250 * the server-side as it is not treated as within
8251 * a handshake while waiting for the ClientHello
8252 * after a renegotiation request.)
8253 */
8254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008256 ret = ssl_check_ctr_renegotiate( ssl );
8257 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8258 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008261 return( ret );
8262 }
8263#endif
8264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008267 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008268 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8269 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008272 return( ret );
8273 }
8274 }
8275
Hanno Beckere41158b2017-10-23 13:30:32 +01008276 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008277 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008278 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008279 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008280 if( ssl->f_get_timer != NULL &&
8281 ssl->f_get_timer( ssl->p_timer ) == -1 )
8282 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008283 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008284 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008285
Hanno Becker327c93b2018-08-15 13:56:18 +01008286 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008287 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008288 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8289 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008290
Hanno Becker4a810fb2017-05-24 16:27:30 +01008291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8292 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008293 }
8294
8295 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008297 {
8298 /*
8299 * OpenSSL sends empty messages to randomize the IV
8300 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008301 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008304 return( 0 );
8305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008307 return( ret );
8308 }
8309 }
8310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008314
Hanno Becker4a810fb2017-05-24 16:27:30 +01008315 /*
8316 * - For client-side, expect SERVER_HELLO_REQUEST.
8317 * - For server-side, expect CLIENT_HELLO.
8318 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8319 */
8320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008322 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008324 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008327
8328 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008330 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008331 {
8332 continue;
8333 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008336 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008337#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008338
Hanno Becker4a810fb2017-05-24 16:27:30 +01008339#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008344
8345 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008347 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008348 {
8349 continue;
8350 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008351#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008353 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008354#endif /* MBEDTLS_SSL_SRV_C */
8355
Hanno Becker21df7f92017-10-17 11:03:26 +01008356#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008357 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008358 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8359 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8360 ssl->conf->allow_legacy_renegotiation ==
8361 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8362 {
8363 /*
8364 * Accept renegotiation request
8365 */
Paul Bakker48916f92012-09-16 19:57:18 +00008366
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008367 /* DTLS clients need to know renego is server-initiated */
8368#if defined(MBEDTLS_SSL_PROTO_DTLS)
8369 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8370 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8371 {
8372 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8373 }
8374#endif
8375 ret = ssl_start_renegotiation( ssl );
8376 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8377 ret != 0 )
8378 {
8379 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8380 return( ret );
8381 }
8382 }
8383 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008384#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008385 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008386 /*
8387 * Refuse renegotiation
8388 */
8389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_SSL_PROTO_SSL3)
8393 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008394 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008395 /* SSLv3 does not have a "no_renegotiation" warning, so
8396 we send a fatal alert and abort the connection. */
8397 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8398 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8399 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008400 }
8401 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8403#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8404 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8405 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8408 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8409 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008410 {
8411 return( ret );
8412 }
Paul Bakker48916f92012-09-16 19:57:18 +00008413 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008414 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8416 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008420 }
Paul Bakker48916f92012-09-16 19:57:18 +00008421 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008422
Hanno Becker90333da2017-10-10 11:27:13 +01008423 /* At this point, we don't know whether the renegotiation has been
8424 * completed or not. The cases to consider are the following:
8425 * 1) The renegotiation is complete. In this case, no new record
8426 * has been read yet.
8427 * 2) The renegotiation is incomplete because the client received
8428 * an application data record while awaiting the ServerHello.
8429 * 3) The renegotiation is incomplete because the client received
8430 * a non-handshake, non-application data message while awaiting
8431 * the ServerHello.
8432 * In each of these case, looping will be the proper action:
8433 * - For 1), the next iteration will read a new record and check
8434 * if it's application data.
8435 * - For 2), the loop condition isn't satisfied as application data
8436 * is present, hence continue is the same as break
8437 * - For 3), the loop condition is satisfied and read_record
8438 * will re-deliver the message that was held back by the client
8439 * when expecting the ServerHello.
8440 */
8441 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008442 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008443#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008445 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008446 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008447 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008448 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008451 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008453 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008454 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8459 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008462 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008463 }
8464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8468 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008469 }
8470
8471 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008472
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008473 /* We're going to return something now, cancel timer,
8474 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008476 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008477
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008479 /* If we requested renego but received AppData, resend HelloRequest.
8480 * Do it now, after setting in_offt, to avoid taking this branch
8481 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008483 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008485 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008486 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008489 return( ret );
8490 }
8491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008492#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008494 }
8495
8496 n = ( len < ssl->in_msglen )
8497 ? len : ssl->in_msglen;
8498
8499 memcpy( buf, ssl->in_offt, n );
8500 ssl->in_msglen -= n;
8501
8502 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008503 {
8504 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008505 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008506 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008508 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008509 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008510 /* more data available */
8511 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008515
Paul Bakker23986e52011-04-24 08:57:21 +00008516 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008517}
8518
8519/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008520 * Send application data to be encrypted by the SSL layer, taking care of max
8521 * fragment length and buffer size.
8522 *
8523 * According to RFC 5246 Section 6.2.1:
8524 *
8525 * Zero-length fragments of Application data MAY be sent as they are
8526 * potentially useful as a traffic analysis countermeasure.
8527 *
8528 * Therefore, it is possible that the input message length is 0 and the
8529 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008530 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008531static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008532 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008533{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008534 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8535 const size_t max_len = (size_t) ret;
8536
8537 if( ret < 0 )
8538 {
8539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8540 return( ret );
8541 }
8542
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008543 if( len > max_len )
8544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008546 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008549 "maximum fragment length: %d > %d",
8550 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008552 }
8553 else
8554#endif
8555 len = max_len;
8556 }
Paul Bakker887bd502011-06-08 13:10:54 +00008557
Paul Bakker5121ce52009-01-03 21:22:43 +00008558 if( ssl->out_left != 0 )
8559 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008560 /*
8561 * The user has previously tried to send the data and
8562 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8563 * written. In this case, we expect the high-level write function
8564 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008569 return( ret );
8570 }
8571 }
Paul Bakker887bd502011-06-08 13:10:54 +00008572 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008573 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008574 /*
8575 * The user is trying to send a message the first time, so we need to
8576 * copy the data into the internal buffers and setup the data structure
8577 * to keep track of partial writes
8578 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008579 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008581 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008582
Hanno Becker67bc7c32018-08-06 11:33:50 +01008583 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008586 return( ret );
8587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008588 }
8589
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008590 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008591}
8592
8593/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008594 * Write application data, doing 1/n-1 splitting if necessary.
8595 *
8596 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008597 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008598 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008601static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008602 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008603{
8604 int ret;
8605
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008606 if( ssl->conf->cbc_record_splitting ==
8607 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008608 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8610 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8611 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008612 {
8613 return( ssl_write_real( ssl, buf, len ) );
8614 }
8615
8616 if( ssl->split_done == 0 )
8617 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008618 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008619 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008620 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008621 }
8622
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008623 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8624 return( ret );
8625 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008626
8627 return( ret + 1 );
8628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008630
8631/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008632 * Write application data (public-facing wrapper)
8633 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008634int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008635{
8636 int ret;
8637
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008639
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008640 if( ssl == NULL || ssl->conf == NULL )
8641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8642
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008643#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008644 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8645 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008646 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008647 return( ret );
8648 }
8649#endif
8650
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008651 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008652 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008653 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008654 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008656 return( ret );
8657 }
8658 }
8659
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008660#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008661 ret = ssl_write_split( ssl, buf, len );
8662#else
8663 ret = ssl_write_real( ssl, buf, len );
8664#endif
8665
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008667
8668 return( ret );
8669}
8670
8671/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008672 * Notify the peer that the connection is being closed
8673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008675{
8676 int ret;
8677
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008678 if( ssl == NULL || ssl->conf == NULL )
8679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008682
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008683 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8689 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8690 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008693 return( ret );
8694 }
8695 }
8696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008698
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008699 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008700}
8701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008703{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008704 if( transform == NULL )
8705 return;
8706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008708 deflateEnd( &transform->ctx_deflate );
8709 inflateEnd( &transform->ctx_inflate );
8710#endif
8711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8713 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715 mbedtls_md_free( &transform->md_ctx_enc );
8716 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008717
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008718 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008719}
8720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#if defined(MBEDTLS_X509_CRT_PARSE_C)
8722static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008725
8726 while( cur != NULL )
8727 {
8728 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008730 cur = next;
8731 }
8732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008734
Hanno Becker0271f962018-08-16 13:23:47 +01008735#if defined(MBEDTLS_SSL_PROTO_DTLS)
8736
8737static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8738{
8739 unsigned offset;
8740 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8741
8742 if( hs == NULL )
8743 return;
8744
Hanno Becker283f5ef2018-08-24 09:34:47 +01008745 ssl_free_buffered_record( ssl );
8746
Hanno Becker0271f962018-08-16 13:23:47 +01008747 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008748 ssl_buffering_free_slot( ssl, offset );
8749}
8750
8751static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8752 uint8_t slot )
8753{
8754 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8755 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008756
8757 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8758 return;
8759
Hanno Beckere605b192018-08-21 15:59:07 +01008760 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008761 {
Hanno Beckere605b192018-08-21 15:59:07 +01008762 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
8763 mbedtls_free( hs_buf->data );
8764 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008765 }
8766}
8767
8768#endif /* MBEDTLS_SSL_PROTO_DTLS */
8769
Gilles Peskine9b562d52018-04-25 20:32:43 +02008770void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008771{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008772 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8773
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008774 if( handshake == NULL )
8775 return;
8776
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008777#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8778 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8779 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008780 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008781 handshake->async_in_progress = 0;
8782 }
8783#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8784
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008785#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8786 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8787 mbedtls_md5_free( &handshake->fin_md5 );
8788 mbedtls_sha1_free( &handshake->fin_sha1 );
8789#endif
8790#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8791#if defined(MBEDTLS_SHA256_C)
8792 mbedtls_sha256_free( &handshake->fin_sha256 );
8793#endif
8794#if defined(MBEDTLS_SHA512_C)
8795 mbedtls_sha512_free( &handshake->fin_sha512 );
8796#endif
8797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#if defined(MBEDTLS_DHM_C)
8800 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_ECDH_C)
8803 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008804#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008805#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008806 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008807#if defined(MBEDTLS_SSL_CLI_C)
8808 mbedtls_free( handshake->ecjpake_cache );
8809 handshake->ecjpake_cache = NULL;
8810 handshake->ecjpake_cache_len = 0;
8811#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008812#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008813
Janos Follath4ae5c292016-02-10 11:27:43 +00008814#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8815 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008816 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008818#endif
8819
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008820#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8821 if( handshake->psk != NULL )
8822 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008823 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008824 mbedtls_free( handshake->psk );
8825 }
8826#endif
8827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008828#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8829 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008830 /*
8831 * Free only the linked list wrapper, not the keys themselves
8832 * since the belong to the SNI callback
8833 */
8834 if( handshake->sni_key_cert != NULL )
8835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008837
8838 while( cur != NULL )
8839 {
8840 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008842 cur = next;
8843 }
8844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847#if defined(MBEDTLS_SSL_PROTO_DTLS)
8848 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008849 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008850 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008851#endif
8852
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008853 mbedtls_platform_zeroize( handshake,
8854 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008855}
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008858{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008859 if( session == NULL )
8860 return;
8861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008863 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865 mbedtls_x509_crt_free( session->peer_cert );
8866 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008867 }
Paul Bakkered27a042013-04-18 22:46:23 +02008868#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008869
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008870#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008872#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008873
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008874 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008875}
8876
Paul Bakker5121ce52009-01-03 21:22:43 +00008877/*
8878 * Free an SSL context
8879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008881{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008882 if( ssl == NULL )
8883 return;
8884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008886
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008887 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008888 {
Angus Grattond8213d02016-05-25 20:56:48 +10008889 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008891 }
8892
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008893 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008894 {
Angus Grattond8213d02016-05-25 20:56:48 +10008895 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008897 }
8898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008899#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008900 if( ssl->compress_buf != NULL )
8901 {
Angus Grattond8213d02016-05-25 20:56:48 +10008902 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008904 }
8905#endif
8906
Paul Bakker48916f92012-09-16 19:57:18 +00008907 if( ssl->transform )
8908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909 mbedtls_ssl_transform_free( ssl->transform );
8910 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008911 }
8912
8913 if( ssl->handshake )
8914 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008915 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8917 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919 mbedtls_free( ssl->handshake );
8920 mbedtls_free( ssl->transform_negotiate );
8921 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008922 }
8923
Paul Bakkerc0463502013-02-14 11:19:38 +01008924 if( ssl->session )
8925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926 mbedtls_ssl_session_free( ssl->session );
8927 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008928 }
8929
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008930#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008931 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008932 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008933 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008935 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008936#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8939 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8942 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008943 }
8944#endif
8945
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008946#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008948#endif
8949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008951
Paul Bakker86f04f42013-02-14 11:20:09 +01008952 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008953 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008954}
8955
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008956/*
8957 * Initialze mbedtls_ssl_config
8958 */
8959void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8960{
8961 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8962}
8963
Simon Butcherc97b6972015-12-27 23:48:17 +00008964#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008965static int ssl_preset_default_hashes[] = {
8966#if defined(MBEDTLS_SHA512_C)
8967 MBEDTLS_MD_SHA512,
8968 MBEDTLS_MD_SHA384,
8969#endif
8970#if defined(MBEDTLS_SHA256_C)
8971 MBEDTLS_MD_SHA256,
8972 MBEDTLS_MD_SHA224,
8973#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008974#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008975 MBEDTLS_MD_SHA1,
8976#endif
8977 MBEDTLS_MD_NONE
8978};
Simon Butcherc97b6972015-12-27 23:48:17 +00008979#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008980
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008981static int ssl_preset_suiteb_ciphersuites[] = {
8982 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8983 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8984 0
8985};
8986
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008987#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008988static int ssl_preset_suiteb_hashes[] = {
8989 MBEDTLS_MD_SHA256,
8990 MBEDTLS_MD_SHA384,
8991 MBEDTLS_MD_NONE
8992};
8993#endif
8994
8995#if defined(MBEDTLS_ECP_C)
8996static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8997 MBEDTLS_ECP_DP_SECP256R1,
8998 MBEDTLS_ECP_DP_SECP384R1,
8999 MBEDTLS_ECP_DP_NONE
9000};
9001#endif
9002
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009003/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009004 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009005 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009006int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009007 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009008{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009009#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009010 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009011#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009012
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009013 /* Use the functions here so that they are covered in tests,
9014 * but otherwise access member directly for efficiency */
9015 mbedtls_ssl_conf_endpoint( conf, endpoint );
9016 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009017
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009018 /*
9019 * Things that are common to all presets
9020 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009021#if defined(MBEDTLS_SSL_CLI_C)
9022 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9023 {
9024 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9025#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9026 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9027#endif
9028 }
9029#endif
9030
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009031#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009032 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009033#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009034
9035#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9036 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9037#endif
9038
9039#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9040 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9041#endif
9042
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009043#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9044 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9045#endif
9046
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009047#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009048 conf->f_cookie_write = ssl_cookie_write_dummy;
9049 conf->f_cookie_check = ssl_cookie_check_dummy;
9050#endif
9051
9052#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9053 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9054#endif
9055
Janos Follath088ce432017-04-10 12:42:31 +01009056#if defined(MBEDTLS_SSL_SRV_C)
9057 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9058#endif
9059
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009060#if defined(MBEDTLS_SSL_PROTO_DTLS)
9061 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9062 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9063#endif
9064
9065#if defined(MBEDTLS_SSL_RENEGOTIATION)
9066 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009067 memset( conf->renego_period, 0x00, 2 );
9068 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009069#endif
9070
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009071#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9072 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9073 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009074 const unsigned char dhm_p[] =
9075 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9076 const unsigned char dhm_g[] =
9077 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9078
Hanno Beckera90658f2017-10-04 15:29:08 +01009079 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9080 dhm_p, sizeof( dhm_p ),
9081 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009082 {
9083 return( ret );
9084 }
9085 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009086#endif
9087
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009088 /*
9089 * Preset-specific defaults
9090 */
9091 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009092 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009093 /*
9094 * NSA Suite B
9095 */
9096 case MBEDTLS_SSL_PRESET_SUITEB:
9097 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9098 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9099 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9100 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9101
9102 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9103 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9104 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9105 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9106 ssl_preset_suiteb_ciphersuites;
9107
9108#if defined(MBEDTLS_X509_CRT_PARSE_C)
9109 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009110#endif
9111
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009112#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009113 conf->sig_hashes = ssl_preset_suiteb_hashes;
9114#endif
9115
9116#if defined(MBEDTLS_ECP_C)
9117 conf->curve_list = ssl_preset_suiteb_curves;
9118#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009119 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009120
9121 /*
9122 * Default
9123 */
9124 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009125 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9126 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9127 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9128 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9129 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9130 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9131 MBEDTLS_SSL_MIN_MINOR_VERSION :
9132 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009133 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9134 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9135
9136#if defined(MBEDTLS_SSL_PROTO_DTLS)
9137 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9138 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9139#endif
9140
9141 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9142 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9143 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9144 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9145 mbedtls_ssl_list_ciphersuites();
9146
9147#if defined(MBEDTLS_X509_CRT_PARSE_C)
9148 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9149#endif
9150
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009151#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009152 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009153#endif
9154
9155#if defined(MBEDTLS_ECP_C)
9156 conf->curve_list = mbedtls_ecp_grp_id_list();
9157#endif
9158
9159#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9160 conf->dhm_min_bitlen = 1024;
9161#endif
9162 }
9163
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009164 return( 0 );
9165}
9166
9167/*
9168 * Free mbedtls_ssl_config
9169 */
9170void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9171{
9172#if defined(MBEDTLS_DHM_C)
9173 mbedtls_mpi_free( &conf->dhm_P );
9174 mbedtls_mpi_free( &conf->dhm_G );
9175#endif
9176
9177#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9178 if( conf->psk != NULL )
9179 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009180 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009181 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009182 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009183 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009184 }
9185
9186 if( conf->psk_identity != NULL )
9187 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009188 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009189 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009190 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009191 conf->psk_identity_len = 0;
9192 }
9193#endif
9194
9195#if defined(MBEDTLS_X509_CRT_PARSE_C)
9196 ssl_key_cert_free( conf->key_cert );
9197#endif
9198
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009199 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009200}
9201
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009202#if defined(MBEDTLS_PK_C) && \
9203 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009204/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209#if defined(MBEDTLS_RSA_C)
9210 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9211 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213#if defined(MBEDTLS_ECDSA_C)
9214 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9215 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009218}
9219
Hanno Becker7e5437a2017-04-28 17:15:26 +01009220unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9221{
9222 switch( type ) {
9223 case MBEDTLS_PK_RSA:
9224 return( MBEDTLS_SSL_SIG_RSA );
9225 case MBEDTLS_PK_ECDSA:
9226 case MBEDTLS_PK_ECKEY:
9227 return( MBEDTLS_SSL_SIG_ECDSA );
9228 default:
9229 return( MBEDTLS_SSL_SIG_ANON );
9230 }
9231}
9232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009234{
9235 switch( sig )
9236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237#if defined(MBEDTLS_RSA_C)
9238 case MBEDTLS_SSL_SIG_RSA:
9239 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241#if defined(MBEDTLS_ECDSA_C)
9242 case MBEDTLS_SSL_SIG_ECDSA:
9243 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009244#endif
9245 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009247 }
9248}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009249#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009250
Hanno Becker7e5437a2017-04-28 17:15:26 +01009251#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9252 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9253
9254/* Find an entry in a signature-hash set matching a given hash algorithm. */
9255mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9256 mbedtls_pk_type_t sig_alg )
9257{
9258 switch( sig_alg )
9259 {
9260 case MBEDTLS_PK_RSA:
9261 return( set->rsa );
9262 case MBEDTLS_PK_ECDSA:
9263 return( set->ecdsa );
9264 default:
9265 return( MBEDTLS_MD_NONE );
9266 }
9267}
9268
9269/* Add a signature-hash-pair to a signature-hash set */
9270void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9271 mbedtls_pk_type_t sig_alg,
9272 mbedtls_md_type_t md_alg )
9273{
9274 switch( sig_alg )
9275 {
9276 case MBEDTLS_PK_RSA:
9277 if( set->rsa == MBEDTLS_MD_NONE )
9278 set->rsa = md_alg;
9279 break;
9280
9281 case MBEDTLS_PK_ECDSA:
9282 if( set->ecdsa == MBEDTLS_MD_NONE )
9283 set->ecdsa = md_alg;
9284 break;
9285
9286 default:
9287 break;
9288 }
9289}
9290
9291/* Allow exactly one hash algorithm for each signature. */
9292void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9293 mbedtls_md_type_t md_alg )
9294{
9295 set->rsa = md_alg;
9296 set->ecdsa = md_alg;
9297}
9298
9299#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9300 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9301
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009302/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009303 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009306{
9307 switch( hash )
9308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309#if defined(MBEDTLS_MD5_C)
9310 case MBEDTLS_SSL_HASH_MD5:
9311 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313#if defined(MBEDTLS_SHA1_C)
9314 case MBEDTLS_SSL_HASH_SHA1:
9315 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009316#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#if defined(MBEDTLS_SHA256_C)
9318 case MBEDTLS_SSL_HASH_SHA224:
9319 return( MBEDTLS_MD_SHA224 );
9320 case MBEDTLS_SSL_HASH_SHA256:
9321 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323#if defined(MBEDTLS_SHA512_C)
9324 case MBEDTLS_SSL_HASH_SHA384:
9325 return( MBEDTLS_MD_SHA384 );
9326 case MBEDTLS_SSL_HASH_SHA512:
9327 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009328#endif
9329 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009331 }
9332}
9333
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009334/*
9335 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9336 */
9337unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9338{
9339 switch( md )
9340 {
9341#if defined(MBEDTLS_MD5_C)
9342 case MBEDTLS_MD_MD5:
9343 return( MBEDTLS_SSL_HASH_MD5 );
9344#endif
9345#if defined(MBEDTLS_SHA1_C)
9346 case MBEDTLS_MD_SHA1:
9347 return( MBEDTLS_SSL_HASH_SHA1 );
9348#endif
9349#if defined(MBEDTLS_SHA256_C)
9350 case MBEDTLS_MD_SHA224:
9351 return( MBEDTLS_SSL_HASH_SHA224 );
9352 case MBEDTLS_MD_SHA256:
9353 return( MBEDTLS_SSL_HASH_SHA256 );
9354#endif
9355#if defined(MBEDTLS_SHA512_C)
9356 case MBEDTLS_MD_SHA384:
9357 return( MBEDTLS_SSL_HASH_SHA384 );
9358 case MBEDTLS_MD_SHA512:
9359 return( MBEDTLS_SSL_HASH_SHA512 );
9360#endif
9361 default:
9362 return( MBEDTLS_SSL_HASH_NONE );
9363 }
9364}
9365
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009366#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009367/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009368 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009369 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009370 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009371int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009374
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009375 if( ssl->conf->curve_list == NULL )
9376 return( -1 );
9377
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009378 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009379 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009380 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009381
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009382 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009383}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009384#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009385
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009386#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009387/*
9388 * Check if a hash proposed by the peer is in our list.
9389 * Return 0 if we're willing to use it, -1 otherwise.
9390 */
9391int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9392 mbedtls_md_type_t md )
9393{
9394 const int *cur;
9395
9396 if( ssl->conf->sig_hashes == NULL )
9397 return( -1 );
9398
9399 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9400 if( *cur == (int) md )
9401 return( 0 );
9402
9403 return( -1 );
9404}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009405#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#if defined(MBEDTLS_X509_CRT_PARSE_C)
9408int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9409 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009410 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009411 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009412{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009413 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009415 int usage = 0;
9416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009418 const char *ext_oid;
9419 size_t ext_len;
9420#endif
9421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9423 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009424 ((void) cert);
9425 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009426 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009427#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9430 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009431 {
9432 /* Server part of the key exchange */
9433 switch( ciphersuite->key_exchange )
9434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 case MBEDTLS_KEY_EXCHANGE_RSA:
9436 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009437 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009438 break;
9439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9441 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9442 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9443 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009444 break;
9445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9447 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009448 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009449 break;
9450
9451 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 case MBEDTLS_KEY_EXCHANGE_NONE:
9453 case MBEDTLS_KEY_EXCHANGE_PSK:
9454 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9455 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009456 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009457 usage = 0;
9458 }
9459 }
9460 else
9461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9463 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009464 }
9465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009467 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009468 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009469 ret = -1;
9470 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009471#else
9472 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9476 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9479 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009480 }
9481 else
9482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9484 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009485 }
9486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009488 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009489 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009490 ret = -1;
9491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009493
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009494 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009497
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009498/*
9499 * Convert version numbers to/from wire format
9500 * and, for DTLS, to/from TLS equivalent.
9501 *
9502 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009503 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009504 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9505 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009508 unsigned char ver[2] )
9509{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#if defined(MBEDTLS_SSL_PROTO_DTLS)
9511 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009514 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9515
9516 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9517 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9518 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009519 else
9520#else
9521 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009522#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009523 {
9524 ver[0] = (unsigned char) major;
9525 ver[1] = (unsigned char) minor;
9526 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009527}
9528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009530 const unsigned char ver[2] )
9531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_SSL_PROTO_DTLS)
9533 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009534 {
9535 *major = 255 - ver[0] + 2;
9536 *minor = 255 - ver[1] + 1;
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009539 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9540 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009541 else
9542#else
9543 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009544#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009545 {
9546 *major = ver[0];
9547 *minor = ver[1];
9548 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009549}
9550
Simon Butcher99000142016-10-13 17:21:01 +01009551int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9552{
9553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9554 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9555 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9556
9557 switch( md )
9558 {
9559#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9560#if defined(MBEDTLS_MD5_C)
9561 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009562 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009563#endif
9564#if defined(MBEDTLS_SHA1_C)
9565 case MBEDTLS_SSL_HASH_SHA1:
9566 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9567 break;
9568#endif
9569#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9570#if defined(MBEDTLS_SHA512_C)
9571 case MBEDTLS_SSL_HASH_SHA384:
9572 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9573 break;
9574#endif
9575#if defined(MBEDTLS_SHA256_C)
9576 case MBEDTLS_SSL_HASH_SHA256:
9577 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9578 break;
9579#endif
9580 default:
9581 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9582 }
9583
9584 return 0;
9585#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9586 (void) ssl;
9587 (void) md;
9588
9589 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9590#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9591}
9592
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009593#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9594 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9595int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9596 unsigned char *output,
9597 unsigned char *data, size_t data_len )
9598{
9599 int ret = 0;
9600 mbedtls_md5_context mbedtls_md5;
9601 mbedtls_sha1_context mbedtls_sha1;
9602
9603 mbedtls_md5_init( &mbedtls_md5 );
9604 mbedtls_sha1_init( &mbedtls_sha1 );
9605
9606 /*
9607 * digitally-signed struct {
9608 * opaque md5_hash[16];
9609 * opaque sha_hash[20];
9610 * };
9611 *
9612 * md5_hash
9613 * MD5(ClientHello.random + ServerHello.random
9614 * + ServerParams);
9615 * sha_hash
9616 * SHA(ClientHello.random + ServerHello.random
9617 * + ServerParams);
9618 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009619 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009620 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009622 goto exit;
9623 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009624 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009625 ssl->handshake->randbytes, 64 ) ) != 0 )
9626 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009628 goto exit;
9629 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009630 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009631 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009633 goto exit;
9634 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009635 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009636 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009638 goto exit;
9639 }
9640
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009641 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009642 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009644 goto exit;
9645 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009646 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009647 ssl->handshake->randbytes, 64 ) ) != 0 )
9648 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009650 goto exit;
9651 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009652 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009653 data_len ) ) != 0 )
9654 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009656 goto exit;
9657 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009658 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009659 output + 16 ) ) != 0 )
9660 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009662 goto exit;
9663 }
9664
9665exit:
9666 mbedtls_md5_free( &mbedtls_md5 );
9667 mbedtls_sha1_free( &mbedtls_sha1 );
9668
9669 if( ret != 0 )
9670 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9671 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9672
9673 return( ret );
9674
9675}
9676#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9677 MBEDTLS_SSL_PROTO_TLS1_1 */
9678
9679#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9680 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9681int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009682 unsigned char *hash, size_t *hashlen,
9683 unsigned char *data, size_t data_len,
9684 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009685{
9686 int ret = 0;
9687 mbedtls_md_context_t ctx;
9688 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009689 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009690
9691 mbedtls_md_init( &ctx );
9692
9693 /*
9694 * digitally-signed struct {
9695 * opaque client_random[32];
9696 * opaque server_random[32];
9697 * ServerDHParams params;
9698 * };
9699 */
9700 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9701 {
9702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9703 goto exit;
9704 }
9705 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9706 {
9707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9708 goto exit;
9709 }
9710 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9711 {
9712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9713 goto exit;
9714 }
9715 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9716 {
9717 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9718 goto exit;
9719 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009720 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009721 {
9722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9723 goto exit;
9724 }
9725
9726exit:
9727 mbedtls_md_free( &ctx );
9728
9729 if( ret != 0 )
9730 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9731 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9732
9733 return( ret );
9734}
9735#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9736 MBEDTLS_SSL_PROTO_TLS1_2 */
9737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738#endif /* MBEDTLS_SSL_TLS_C */