blob: 4bbe0d410cfb69bf6564f7c4bf610bd395af2e2b [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;
Andrzej Kurek748face2018-10-11 07:20:19 -0400154 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100155
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
Janos Follath3fbdada2018-08-15 10:26:53 +01001336 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1337 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001338 }
1339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1343 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001344 }
1345
1346 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001347 if( end - p < 2 )
1348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001349
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001350 *(p++) = (unsigned char)( psk_len >> 8 );
1351 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001352
1353 if( end < p || (size_t)( end - p ) < psk_len )
1354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1355
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001356 memcpy( p, psk, psk_len );
1357 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001358
1359 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1360
1361 return( 0 );
1362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001366/*
1367 * SSLv3.0 MAC functions
1368 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001369#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001370static void ssl_mac( mbedtls_md_context_t *md_ctx,
1371 const unsigned char *secret,
1372 const unsigned char *buf, size_t len,
1373 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001374 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375{
1376 unsigned char header[11];
1377 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001378 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1380 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001381
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001382 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001384 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001385 else
Paul Bakker68884e32013-01-07 18:20:04 +01001386 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
1388 memcpy( header, ctr, 8 );
1389 header[ 8] = (unsigned char) type;
1390 header[ 9] = (unsigned char)( len >> 8 );
1391 header[10] = (unsigned char)( len );
1392
Paul Bakker68884e32013-01-07 18:20:04 +01001393 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_md_starts( md_ctx );
1395 mbedtls_md_update( md_ctx, secret, md_size );
1396 mbedtls_md_update( md_ctx, padding, padlen );
1397 mbedtls_md_update( md_ctx, header, 11 );
1398 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001399 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Paul Bakker68884e32013-01-07 18:20:04 +01001401 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 mbedtls_md_starts( md_ctx );
1403 mbedtls_md_update( md_ctx, secret, md_size );
1404 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001405 mbedtls_md_update( md_ctx, out, md_size );
1406 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1411 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001412 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001413#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001414#endif
1415
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001416/* The function below is only used in the Lucky 13 counter-measure in
1417 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1418#if defined(SSL_SOME_MODES_USE_MAC) && \
1419 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1420 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1421 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1422/* This function makes sure every byte in the memory region is accessed
1423 * (in ascending addresses order) */
1424static void ssl_read_memory( unsigned char *p, size_t len )
1425{
1426 unsigned char acc = 0;
1427 volatile unsigned char force;
1428
1429 for( ; len != 0; p++, len-- )
1430 acc ^= *p;
1431
1432 force = acc;
1433 (void) force;
1434}
1435#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1436
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001437/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001443 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001447 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001451 }
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001456 ssl->out_msg, ssl->out_msglen );
1457
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001459 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001461#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 if( mode == MBEDTLS_MODE_STREAM ||
1463 ( mode == MBEDTLS_MODE_CBC
1464#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1465 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001466#endif
1467 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469#if defined(MBEDTLS_SSL_PROTO_SSL3)
1470 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001471 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001472 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001473
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001474 ssl_mac( &ssl->transform_out->md_ctx_enc,
1475 ssl->transform_out->mac_enc,
1476 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001477 ssl->out_ctr, ssl->out_msgtype,
1478 mac );
1479
1480 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001481 }
1482 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1485 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1486 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001487 {
Hanno Becker992b6872017-11-09 18:57:39 +00001488 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1491 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1492 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1493 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001494 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001495 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001497
1498 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001499 }
1500 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001501#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001505 }
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001508 ssl->out_msg + ssl->out_msglen,
1509 ssl->transform_out->maclen );
1510
1511 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001512 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001513 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001514#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001516 /*
1517 * Encrypt
1518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1520 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001522 int ret;
1523 size_t olen = 0;
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 "including %d bytes of padding",
1527 ssl->out_msglen, 0 ) );
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001530 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001531 ssl->transform_out->ivlen,
1532 ssl->out_msg, ssl->out_msglen,
1533 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001536 return( ret );
1537 }
1538
1539 if( ssl->out_msglen != olen )
1540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 }
Paul Bakker68884e32013-01-07 18:20:04 +01001545 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001547#if defined(MBEDTLS_GCM_C) || \
1548 defined(MBEDTLS_CCM_C) || \
1549 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001551 mode == MBEDTLS_MODE_CCM ||
1552 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001553 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001554 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001555 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001556 unsigned char *enc_msg;
1557 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001558 unsigned char iv[12];
1559 mbedtls_ssl_transform *transform = ssl->transform_out;
1560 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001562 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001563
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001564 /*
1565 * Prepare additional authenticated data
1566 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001567 memcpy( add_data, ssl->out_ctr, 8 );
1568 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001570 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001571 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1572 add_data[12] = ssl->out_msglen & 0xFF;
1573
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001574 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001575
Paul Bakker68884e32013-01-07 18:20:04 +01001576 /*
1577 * Generate IV
1578 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001579 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1580 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001581 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001582 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1583 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1584 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1585
1586 }
1587 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1588 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001589 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001590 unsigned char i;
1591
1592 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1593
1594 for( i = 0; i < 8; i++ )
1595 iv[i+4] ^= ssl->out_ctr[i];
1596 }
1597 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001598 {
1599 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001602 }
1603
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001604 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1605 iv, transform->ivlen );
1606 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1607 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001608
Paul Bakker68884e32013-01-07 18:20:04 +01001609 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001610 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001611 */
1612 enc_msg = ssl->out_msg;
1613 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001614 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001617 "including 0 bytes of padding",
1618 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001619
Paul Bakker68884e32013-01-07 18:20:04 +01001620 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001621 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001622 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001623 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1624 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001625 add_data, 13,
1626 enc_msg, enc_msglen,
1627 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001628 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001631 return( ret );
1632 }
1633
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001634 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1637 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001638 }
1639
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001640 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001641 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1647#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001648 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001651 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001653 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001654
Paul Bakker48916f92012-09-16 19:57:18 +00001655 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1656 ssl->transform_out->ivlen;
1657 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001658 padlen = 0;
1659
1660 for( i = 0; i <= padlen; i++ )
1661 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1662
1663 ssl->out_msglen += padlen + 1;
1664
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001665 enc_msglen = ssl->out_msglen;
1666 enc_msg = ssl->out_msg;
1667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001669 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001670 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1671 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001674 {
1675 /*
1676 * Generate IV
1677 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001678 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001679 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001680 if( ret != 0 )
1681 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001682
Paul Bakker92be97b2013-01-02 17:30:03 +01001683 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001684 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001685
1686 /*
1687 * Fix pointer positions and message length with added IV
1688 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001689 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001690 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001691 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001692 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001696 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001697 ssl->out_msglen, ssl->transform_out->ivlen,
1698 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001701 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001702 ssl->transform_out->ivlen,
1703 enc_msg, enc_msglen,
1704 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001707 return( ret );
1708 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001709
Paul Bakkercca5b812013-08-31 17:40:26 +02001710 if( enc_msglen != olen )
1711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001714 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1717 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001718 {
1719 /*
1720 * Save IV in SSL3 and TLS1
1721 */
1722 memcpy( ssl->transform_out->iv_enc,
1723 ssl->transform_out->cipher_ctx_enc.iv,
1724 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001726#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001729 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001730 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001731 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1732
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001733 /*
1734 * MAC(MAC_write_key, seq_num +
1735 * TLSCipherText.type +
1736 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001737 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001738 * IV + // except for TLS 1.0
1739 * ENC(content + padding + padding_length));
1740 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001741 unsigned char pseudo_hdr[13];
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001744
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001745 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1746 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001747 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1748 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1753 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001754 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001755 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001757
Hanno Becker3d8c9072018-01-05 16:24:22 +00001758 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1759 ssl->transform_out->maclen );
1760
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001761 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001762 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001766 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001768 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001772 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001774 /* Make extra sure authentication was performed, exactly once */
1775 if( auth_done != 1 )
1776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001779 }
1780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
1783 return( 0 );
1784}
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001789 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001790#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001791 size_t padlen = 0, correct = 1;
1792#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001796 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001800 }
1801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001803
Paul Bakker48916f92012-09-16 19:57:18 +00001804 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001807 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 }
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1812 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001813 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001814 int ret;
1815 size_t olen = 0;
1816
Paul Bakker68884e32013-01-07 18:20:04 +01001817 padlen = 0;
1818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001820 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001821 ssl->transform_in->ivlen,
1822 ssl->in_msg, ssl->in_msglen,
1823 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001826 return( ret );
1827 }
1828
1829 if( ssl->in_msglen != olen )
1830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1832 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001833 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 }
Paul Bakker68884e32013-01-07 18:20:04 +01001835 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001837#if defined(MBEDTLS_GCM_C) || \
1838 defined(MBEDTLS_CCM_C) || \
1839 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001841 mode == MBEDTLS_MODE_CCM ||
1842 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001843 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001844 int ret;
1845 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001846 unsigned char *dec_msg;
1847 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001848 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001849 unsigned char iv[12];
1850 mbedtls_ssl_transform *transform = ssl->transform_in;
1851 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001853 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001854
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 /*
1856 * Compute and update sizes
1857 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001858 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001861 "+ taglen (%d)", ssl->in_msglen,
1862 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001864 }
1865 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1866
Paul Bakker68884e32013-01-07 18:20:04 +01001867 dec_msg = ssl->in_msg;
1868 dec_msg_result = ssl->in_msg;
1869 ssl->in_msglen = dec_msglen;
1870
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001871 /*
1872 * Prepare additional authenticated data
1873 */
Paul Bakker68884e32013-01-07 18:20:04 +01001874 memcpy( add_data, ssl->in_ctr, 8 );
1875 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001877 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001878 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1879 add_data[12] = ssl->in_msglen & 0xFF;
1880
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001881 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001882
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001883 /*
1884 * Prepare IV
1885 */
1886 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1887 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001888 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001889 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1890 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001891
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001892 }
1893 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1894 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001895 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001896 unsigned char i;
1897
1898 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1899
1900 for( i = 0; i < 8; i++ )
1901 iv[i+4] ^= ssl->in_ctr[i];
1902 }
1903 else
1904 {
1905 /* Reminder if we ever add an AEAD mode with a different size */
1906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1908 }
1909
1910 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001912
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001913 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001914 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001915 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001917 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001918 add_data, 13,
1919 dec_msg, dec_msglen,
1920 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001921 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1926 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001927
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001928 return( ret );
1929 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001930 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001931
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001932 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001936 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1940#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001941 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 {
Paul Bakker45829992013-01-03 14:52:21 +01001944 /*
1945 * Decrypt and check the padding
1946 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001947 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001948 unsigned char *dec_msg;
1949 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001950 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001951 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001952 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001953
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 /*
Paul Bakker45829992013-01-03 14:52:21 +01001955 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1958 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001959 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001960#endif
Paul Bakker45829992013-01-03 14:52:21 +01001961
1962 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1963 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001966 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1967 ssl->transform_in->ivlen,
1968 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001970 }
1971
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001972 dec_msglen = ssl->in_msglen;
1973 dec_msg = ssl->in_msg;
1974 dec_msg_result = ssl->in_msg;
1975
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001976 /*
1977 * Authenticate before decrypt if enabled
1978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1980 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001981 {
Hanno Becker992b6872017-11-09 18:57:39 +00001982 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001983 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001986
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001987 dec_msglen -= ssl->transform_in->maclen;
1988 ssl->in_msglen -= ssl->transform_in->maclen;
1989
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001990 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1991 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1992 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1993 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1998 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001999 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002000 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002004 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002005 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002006 ssl->transform_in->maclen );
2007
Hanno Becker992b6872017-11-09 18:57:39 +00002008 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2009 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002014 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002015 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002016 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002018
2019 /*
2020 * Check length sanity
2021 */
2022 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002025 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002027 }
2028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002030 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002031 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002034 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002035 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002036 dec_msglen -= ssl->transform_in->ivlen;
2037 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002038
Paul Bakker48916f92012-09-16 19:57:18 +00002039 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002040 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002041 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002045 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002046 ssl->transform_in->ivlen,
2047 dec_msg, dec_msglen,
2048 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002051 return( ret );
2052 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002053
Paul Bakkercca5b812013-08-31 17:40:26 +02002054 if( dec_msglen != olen )
2055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002058 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2061 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002062 {
2063 /*
2064 * Save IV in SSL3 and TLS1
2065 */
2066 memcpy( ssl->transform_in->iv_dec,
2067 ssl->transform_in->cipher_ctx_dec.iv,
2068 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002073
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002074 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002075 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_SSL_DEBUG_ALL)
2078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002079 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002080#endif
Paul Bakker45829992013-01-03 14:52:21 +01002081 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002082 correct = 0;
2083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_SSL_PROTO_SSL3)
2086 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 {
Paul Bakker48916f92012-09-16 19:57:18 +00002088 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090#if defined(MBEDTLS_SSL_DEBUG_ALL)
2091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002093 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002094#endif
Paul Bakker45829992013-01-03 14:52:21 +01002095 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
2097 }
2098 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2100#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2101 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2102 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 {
2104 /*
Paul Bakker45829992013-01-03 14:52:21 +01002105 * TLSv1+: always check the padding up to the first failure
2106 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002108 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002109 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002110 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002111
Paul Bakker956c9e02013-12-19 14:42:28 +01002112 /*
2113 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002114 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002115 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002116 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002117 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002118 *
2119 * In both cases we reset padding_idx to a safe value (0) to
2120 * prevent out-of-buffer reads.
2121 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002122 correct &= ( padlen <= ssl->in_msglen );
2123 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002124 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002125
2126 padding_idx *= correct;
2127
Angus Grattonb512bc12018-06-19 15:57:50 +10002128 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002129 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002130 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002131 pad_count += real_count *
2132 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2133 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002134
2135 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002138 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002140#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002141 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2145 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002149 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002150
2151 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002153 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002155 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002160
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002161#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002164#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
2166 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002167 * Authenticate if not done yet.
2168 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002170#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002171 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002172 {
Hanno Becker992b6872017-11-09 18:57:39 +00002173 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002174
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002175 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002177 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2178 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#if defined(MBEDTLS_SSL_PROTO_SSL3)
2181 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002182 {
2183 ssl_mac( &ssl->transform_in->md_ctx_dec,
2184 ssl->transform_in->mac_dec,
2185 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002186 ssl->in_ctr, ssl->in_msgtype,
2187 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002188 }
2189 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2191#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2192 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2193 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002194 {
2195 /*
2196 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002197 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002198 *
2199 * Known timing attacks:
2200 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2201 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002202 * To compensate for different timings for the MAC calculation
2203 * depending on how much padding was removed (which is determined
2204 * by padlen), process extra_run more blocks through the hash
2205 * function.
2206 *
2207 * The formula in the paper is
2208 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2209 * where L1 is the size of the header plus the decrypted message
2210 * plus CBC padding and L2 is the size of the header plus the
2211 * decrypted message. This is for an underlying hash function
2212 * with 64-byte blocks.
2213 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2214 * correctly. We round down instead of up, so -56 is the correct
2215 * value for our calculations instead of -55.
2216 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002217 * Repeat the formula rather than defining a block_size variable.
2218 * This avoids requiring division by a variable at runtime
2219 * (which would be marginally less efficient and would require
2220 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002221 */
2222 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002223
2224 /*
2225 * The next two sizes are the minimum and maximum values of
2226 * in_msglen over all padlen values.
2227 *
2228 * They're independent of padlen, since we previously did
2229 * in_msglen -= padlen.
2230 *
2231 * Note that max_len + maclen is never more than the buffer
2232 * length, as we previously did in_msglen -= maclen too.
2233 */
2234 const size_t max_len = ssl->in_msglen + padlen;
2235 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2236
Gilles Peskine20b44082018-05-29 14:06:49 +02002237 switch( ssl->transform_in->ciphersuite_info->mac )
2238 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002239#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2240 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002241 case MBEDTLS_MD_MD5:
2242 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002243 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002244 /* 8 bytes of message size, 64-byte compression blocks */
2245 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2246 ( 13 + ssl->in_msglen + 8 ) / 64;
2247 break;
2248#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002249#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002250 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002251 /* 16 bytes of message size, 128-byte compression blocks */
2252 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2253 ( 13 + ssl->in_msglen + 16 ) / 128;
2254 break;
2255#endif
2256 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002258 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2259 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002260
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002261 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2264 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2265 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2266 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002267 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002268 /* Make sure we access everything even when padlen > 0. This
2269 * makes the synchronisation requirements for just-in-time
2270 * Prime+Probe attacks much tighter and hopefully impractical. */
2271 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002272 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002273
2274 /* Call mbedtls_md_process at least once due to cache attacks
2275 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002276 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002280
2281 /* Make sure we access all the memory that could contain the MAC,
2282 * before we check it in the next code block. This makes the
2283 * synchronisation requirements for just-in-time Prime+Probe
2284 * attacks much tighter and hopefully impractical. */
2285 ssl_read_memory( ssl->in_msg + min_len,
2286 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002287 }
2288 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2290 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002294 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002296#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002297 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2298 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2299 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
Hanno Becker992b6872017-11-09 18:57:39 +00002302 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2303 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#if defined(MBEDTLS_SSL_DEBUG_ALL)
2306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002307#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002308 correct = 0;
2309 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002310 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002311 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002312
2313 /*
2314 * Finally check the correct flag
2315 */
2316 if( correct == 0 )
2317 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002318#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002319
2320 /* Make extra sure authentication was performed, exactly once */
2321 if( auth_done != 1 )
2322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
2327 if( ssl->in_msglen == 0 )
2328 {
Angus Gratton34817922018-06-19 15:58:22 +10002329#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2330 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2331 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2332 {
2333 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2335 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2336 }
2337#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2338
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 ssl->nb_zero++;
2340
2341 /*
2342 * Three or more empty messages may be a DoS attack
2343 * (excessive CPU consumption).
2344 */
2345 if( ssl->nb_zero > 3 )
2346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 }
2351 }
2352 else
2353 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002357 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002358 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002359 }
2360 else
2361#endif
2362 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002364 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2365 if( ++ssl->in_ctr[i - 1] != 0 )
2366 break;
2367
2368 /* The loop goes to its end iff the counter is wrapping */
2369 if( i == ssl_ep_len( ssl ) )
2370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2372 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002373 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002374 }
2375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002377
2378 return( 0 );
2379}
2380
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002381#undef MAC_NONE
2382#undef MAC_PLAINTEXT
2383#undef MAC_CIPHERTEXT
2384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386/*
2387 * Compression/decompression functions
2388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002390{
2391 int ret;
2392 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002393 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002394 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002395 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002398
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002399 if( len_pre == 0 )
2400 return( 0 );
2401
Paul Bakker2770fbd2012-07-03 13:30:23 +00002402 memcpy( msg_pre, ssl->out_msg, len_pre );
2403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002405 ssl->out_msglen ) );
2406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002408 ssl->out_msg, ssl->out_msglen );
2409
Paul Bakker48916f92012-09-16 19:57:18 +00002410 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2411 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2412 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002413 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002414
Paul Bakker48916f92012-09-16 19:57:18 +00002415 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416 if( ret != Z_OK )
2417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2419 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002420 }
2421
Angus Grattond8213d02016-05-25 20:56:48 +10002422 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002423 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002426 ssl->out_msglen ) );
2427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002429 ssl->out_msg, ssl->out_msglen );
2430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002432
2433 return( 0 );
2434}
2435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002437{
2438 int ret;
2439 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002440 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002441 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002442 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002445
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002446 if( len_pre == 0 )
2447 return( 0 );
2448
Paul Bakker2770fbd2012-07-03 13:30:23 +00002449 memcpy( msg_pre, ssl->in_msg, len_pre );
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002452 ssl->in_msglen ) );
2453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002455 ssl->in_msg, ssl->in_msglen );
2456
Paul Bakker48916f92012-09-16 19:57:18 +00002457 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2458 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2459 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002460 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002461 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002462
Paul Bakker48916f92012-09-16 19:57:18 +00002463 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002464 if( ret != Z_OK )
2465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2467 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002468 }
2469
Angus Grattond8213d02016-05-25 20:56:48 +10002470 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002471 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002474 ssl->in_msglen ) );
2475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002477 ssl->in_msg, ssl->in_msglen );
2478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002480
2481 return( 0 );
2482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2486static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_SSL_PROTO_DTLS)
2489static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002490{
2491 /* If renegotiation is not enforced, retransmit until we would reach max
2492 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002493 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002494 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002495 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002496 unsigned char doublings = 1;
2497
2498 while( ratio != 0 )
2499 {
2500 ++doublings;
2501 ratio >>= 1;
2502 }
2503
2504 if( ++ssl->renego_records_seen > doublings )
2505 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002507 return( 0 );
2508 }
2509 }
2510
2511 return( ssl_write_hello_request( ssl ) );
2512}
2513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002515
Paul Bakker5121ce52009-01-03 21:22:43 +00002516/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002517 * Fill the input message buffer by appending data to it.
2518 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002519 *
2520 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2521 * available (from this read and/or a previous one). Otherwise, an error code
2522 * is returned (possibly EOF or WANT_READ).
2523 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002524 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2525 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2526 * since we always read a whole datagram at once.
2527 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002528 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002529 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002532{
Paul Bakker23986e52011-04-24 08:57:21 +00002533 int ret;
2534 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002538 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002541 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002543 }
2544
Angus Grattond8213d02016-05-25 20:56:48 +10002545 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002549 }
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002552 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002554 uint32_t timeout;
2555
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002556 /* Just to be sure */
2557 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2558 {
2559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2560 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2562 }
2563
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002564 /*
2565 * The point is, we need to always read a full datagram at once, so we
2566 * sometimes read more then requested, and handle the additional data.
2567 * It could be the rest of the current record (while fetching the
2568 * header) and/or some other records in the same datagram.
2569 */
2570
2571 /*
2572 * Move to the next record in the already read datagram if applicable
2573 */
2574 if( ssl->next_record_offset != 0 )
2575 {
2576 if( ssl->in_left < ssl->next_record_offset )
2577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002580 }
2581
2582 ssl->in_left -= ssl->next_record_offset;
2583
2584 if( ssl->in_left != 0 )
2585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002587 ssl->next_record_offset ) );
2588 memmove( ssl->in_hdr,
2589 ssl->in_hdr + ssl->next_record_offset,
2590 ssl->in_left );
2591 }
2592
2593 ssl->next_record_offset = 0;
2594 }
2595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002598
2599 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002600 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002601 */
2602 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002605 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002606 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002607
2608 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002609 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002610 * are not at the beginning of a new record, the caller did something
2611 * wrong.
2612 */
2613 if( ssl->in_left != 0 )
2614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002617 }
2618
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002619 /*
2620 * Don't even try to read if time's out already.
2621 * This avoids by-passing the timer when repeatedly receiving messages
2622 * that will end up being dropped.
2623 */
2624 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002625 {
2626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002627 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002628 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002629 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002630 {
Angus Grattond8213d02016-05-25 20:56:48 +10002631 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002634 timeout = ssl->handshake->retransmit_timeout;
2635 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002636 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002639
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002640 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002641 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2642 timeout );
2643 else
2644 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002647
2648 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002650 }
2651
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002652 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002655 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002658 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002659 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002662 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002663 }
2664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002668 return( ret );
2669 }
2670
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002671 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002672 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002674 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002676 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002677 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002680 return( ret );
2681 }
2682
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002683 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002686 }
2687
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 if( ret < 0 )
2689 return( ret );
2690
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002691 ssl->in_left = ret;
2692 }
2693 else
2694#endif
2695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002697 ssl->in_left, nb_want ) );
2698
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002699 while( ssl->in_left < nb_want )
2700 {
2701 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002702
2703 if( ssl_check_timer( ssl ) != 0 )
2704 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2705 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002706 {
2707 if( ssl->f_recv_timeout != NULL )
2708 {
2709 ret = ssl->f_recv_timeout( ssl->p_bio,
2710 ssl->in_hdr + ssl->in_left, len,
2711 ssl->conf->read_timeout );
2712 }
2713 else
2714 {
2715 ret = ssl->f_recv( ssl->p_bio,
2716 ssl->in_hdr + ssl->in_left, len );
2717 }
2718 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002721 ssl->in_left, nb_want ) );
2722 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002723
2724 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002726
2727 if( ret < 0 )
2728 return( ret );
2729
mohammad160352aecb92018-03-28 23:41:40 -07002730 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002731 {
Darryl Green11999bb2018-03-13 15:22:58 +00002732 MBEDTLS_SSL_DEBUG_MSG( 1,
2733 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002734 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2736 }
2737
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002738 ssl->in_left += ret;
2739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
2744 return( 0 );
2745}
2746
2747/*
2748 * Flush any data not yet written
2749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002751{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002752 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002753 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002757 if( ssl->f_send == NULL )
2758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002760 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002762 }
2763
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002764 /* Avoid incrementing counter if data is flushed */
2765 if( ssl->out_left == 0 )
2766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002768 return( 0 );
2769 }
2770
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 while( ssl->out_left > 0 )
2772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2774 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Hanno Becker2b1e3542018-08-06 11:19:13 +01002776 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002777 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
2781 if( ret <= 0 )
2782 return( ret );
2783
mohammad160352aecb92018-03-28 23:41:40 -07002784 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002785 {
Darryl Green11999bb2018-03-13 15:22:58 +00002786 MBEDTLS_SSL_DEBUG_MSG( 1,
2787 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002788 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002789 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2790 }
2791
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 ssl->out_left -= ret;
2793 }
2794
Hanno Becker2b1e3542018-08-06 11:19:13 +01002795#if defined(MBEDTLS_SSL_PROTO_DTLS)
2796 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002797 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002798 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002799 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002800 else
2801#endif
2802 {
2803 ssl->out_hdr = ssl->out_buf + 8;
2804 }
2805 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
2809 return( 0 );
2810}
2811
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002812/*
2813 * Functions to handle the DTLS retransmission state machine
2814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002816/*
2817 * Append current handshake message to current outgoing flight
2818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2823 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2824 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002825
2826 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002827 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002828 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002831 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832 }
2833
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002834 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002835 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002838 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002839 }
2840
2841 /* Copy current handshake message with headers */
2842 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2843 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002844 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002845 msg->next = NULL;
2846
2847 /* Append to the current flight */
2848 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002849 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002850 else
2851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002853 while( cur->next != NULL )
2854 cur = cur->next;
2855 cur->next = msg;
2856 }
2857
Hanno Becker3b235902018-08-06 09:54:53 +01002858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002859 return( 0 );
2860}
2861
2862/*
2863 * Free the current flight of handshake messages
2864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 mbedtls_ssl_flight_item *cur = flight;
2868 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002869
2870 while( cur != NULL )
2871 {
2872 next = cur->next;
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 mbedtls_free( cur->p );
2875 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002876
2877 cur = next;
2878 }
2879}
2880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2882static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002883#endif
2884
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002885/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002886 * Swap transform_out and out_ctr with the alternative ones
2887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002891 unsigned char tmp_out_ctr[8];
2892
2893 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002896 return;
2897 }
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002900
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002901 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002902 tmp_transform = ssl->transform_out;
2903 ssl->transform_out = ssl->handshake->alt_transform_out;
2904 ssl->handshake->alt_transform_out = tmp_transform;
2905
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002906 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002907 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2908 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002909 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002910
2911 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002912 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2915 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2920 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002921 }
2922 }
2923#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002924}
2925
2926/*
2927 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002928 */
2929int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2930{
2931 int ret = 0;
2932
2933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2934
2935 ret = mbedtls_ssl_flight_transmit( ssl );
2936
2937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2938
2939 return( ret );
2940}
2941
2942/*
2943 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002944 *
2945 * Need to remember the current message in case flush_output returns
2946 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002947 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002948 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002949int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002950{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002951 int ret;
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
Hanno Becker67bc7c32018-08-06 11:33:50 +01002986 ret = ssl_get_remaining_payload_in_datagram( ssl );
2987 if( ret < 0 )
2988 return( ret );
2989 max_frag_len = (size_t) ret;
2990
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002991 /* CCS is copied as is, while HS messages may need fragmentation */
2992 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2993 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002994 if( max_frag_len == 0 )
2995 {
2996 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2997 return( ret );
2998
2999 continue;
3000 }
3001
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003002 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003003 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003004 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003005
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003006 /* Update position inside current message */
3007 ssl->handshake->cur_msg_p += cur->len;
3008 }
3009 else
3010 {
3011 const unsigned char * const p = ssl->handshake->cur_msg_p;
3012 const size_t hs_len = cur->len - 12;
3013 const size_t frag_off = p - ( cur->p + 12 );
3014 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003015 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003016
Hanno Beckere1dcb032018-08-17 16:47:58 +01003017 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003018 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003019 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003020 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003021
Hanno Becker67bc7c32018-08-06 11:33:50 +01003022 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3023 return( ret );
3024
3025 continue;
3026 }
3027 max_hs_frag_len = max_frag_len - 12;
3028
3029 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3030 max_hs_frag_len : rem_len;
3031
3032 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003033 {
3034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003035 (unsigned) cur_hs_frag_len,
3036 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003037 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003038
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003039 /* Messages are stored with handshake headers as if not fragmented,
3040 * copy beginning of headers then fill fragmentation fields.
3041 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3042 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003043
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003044 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3045 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3046 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3047
Hanno Becker67bc7c32018-08-06 11:33:50 +01003048 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3049 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3050 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003051
3052 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3053
Hanno Becker3f7b9732018-08-28 09:53:25 +01003054 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003055 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3056 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003057 ssl->out_msgtype = cur->type;
3058
3059 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003060 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003061 }
3062
3063 /* If done with the current message move to the next one if any */
3064 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3065 {
3066 if( cur->next != NULL )
3067 {
3068 ssl->handshake->cur_msg = cur->next;
3069 ssl->handshake->cur_msg_p = cur->next->p + 12;
3070 }
3071 else
3072 {
3073 ssl->handshake->cur_msg = NULL;
3074 ssl->handshake->cur_msg_p = NULL;
3075 }
3076 }
3077
3078 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003079 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003082 return( ret );
3083 }
3084 }
3085
Hanno Becker67bc7c32018-08-06 11:33:50 +01003086 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3087 return( ret );
3088
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003089 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3091 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003092 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003095 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3096 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003097
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003099
3100 return( 0 );
3101}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003102
3103/*
3104 * To be called when the last message of an incoming flight is received.
3105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003107{
3108 /* We won't need to resend that one any more */
3109 ssl_flight_free( ssl->handshake->flight );
3110 ssl->handshake->flight = NULL;
3111 ssl->handshake->cur_msg = NULL;
3112
3113 /* The next incoming flight will start with this msg_seq */
3114 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3115
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003116 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003117 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003118
Hanno Becker0271f962018-08-16 13:23:47 +01003119 /* Clear future message buffering structure. */
3120 ssl_buffering_free( ssl );
3121
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003122 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003123 ssl_set_timer( ssl, 0 );
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3126 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003129 }
3130 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003132}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003133
3134/*
3135 * To be called when the last message of an outgoing flight is send.
3136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003138{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003139 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003140 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3143 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003146 }
3147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003151
Paul Bakker5121ce52009-01-03 21:22:43 +00003152/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003153 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003154 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155
3156/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003157 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003158 *
3159 * - fill in handshake headers
3160 * - update handshake checksum
3161 * - DTLS: save message for resending
3162 * - then pass to the record layer
3163 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003164 * DTLS: except for HelloRequest, messages are only queued, and will only be
3165 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003166 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003167 * Inputs:
3168 * - ssl->out_msglen: 4 + actual handshake message len
3169 * (4 is the size of handshake headers for TLS)
3170 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3171 * - ssl->out_msg + 4: the handshake message body
3172 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003173 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003174 * - ssl->out_msglen: the length of the record contents
3175 * (including handshake headers but excluding record headers)
3176 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003177 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003178int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003179{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003180 int ret;
3181 const size_t hs_len = ssl->out_msglen - 4;
3182 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003183
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3185
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003186 /*
3187 * Sanity checks
3188 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003189 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003190 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3191 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003192 /* In SSLv3, the client might send a NoCertificate alert. */
3193#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3194 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3195 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3196 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3197#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3198 {
3199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3201 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003202 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003204 /* Whenever we send anything different from a
3205 * HelloRequest we should be in a handshake - double check. */
3206 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3207 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003208 ssl->handshake == NULL )
3209 {
3210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003215 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003216 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003218 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003221 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003222#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003223
Hanno Beckerb50a2532018-08-06 11:52:54 +01003224 /* Double-check that we did not exceed the bounds
3225 * of the outgoing record buffer.
3226 * This should never fail as the various message
3227 * writing functions must obey the bounds of the
3228 * outgoing record buffer, but better be safe.
3229 *
3230 * Note: We deliberately do not check for the MTU or MFL here.
3231 */
3232 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3233 {
3234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3235 "size %u, maximum %u",
3236 (unsigned) ssl->out_msglen,
3237 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3238 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3239 }
3240
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003241 /*
3242 * Fill handshake headers
3243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003246 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3247 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3248 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003250 /*
3251 * DTLS has additional fields in the Handshake layer,
3252 * between the length field and the actual payload:
3253 * uint16 message_seq;
3254 * uint24 fragment_offset;
3255 * uint24 fragment_length;
3256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003259 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003260 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003261 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003262 {
3263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3264 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003265 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003266 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3268 }
3269
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003270 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003271 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003272
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003273 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003274 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003275 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003276 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3277 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3278 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003279 }
3280 else
3281 {
3282 ssl->out_msg[4] = 0;
3283 ssl->out_msg[5] = 0;
3284 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003285
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003286 /* Handshake hashes are computed without fragmentation,
3287 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003288 memset( ssl->out_msg + 6, 0x00, 3 );
3289 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003292
Hanno Becker0207e532018-08-28 10:28:28 +01003293 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003294 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3295 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 }
3297
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003298 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003300 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003301 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3302 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003303 {
3304 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003307 return( ret );
3308 }
3309 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003310 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003312 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003313 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003314 {
3315 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3316 return( ret );
3317 }
3318 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003319
3320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3321
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003322 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003323}
3324
3325/*
3326 * Record layer functions
3327 */
3328
3329/*
3330 * Write current record.
3331 *
3332 * Uses:
3333 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3334 * - ssl->out_msglen: length of the record content (excl headers)
3335 * - ssl->out_msg: record content
3336 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003337int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003338{
3339 int ret, done = 0;
3340 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003341 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003342
3343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003346 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003348 {
3349 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003352 return( ret );
3353 }
3354
3355 len = ssl->out_msglen;
3356 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3360 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 ret = mbedtls_ssl_hw_record_write( ssl );
3365 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3368 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003369 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003370
3371 if( ret == 0 )
3372 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003375 if( !done )
3376 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003377 unsigned i;
3378 size_t protected_record_size;
3379
Paul Bakker05ef8352012-05-08 09:17:57 +00003380 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003382 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003383
Hanno Becker19859472018-08-06 09:40:20 +01003384 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003385 ssl->out_len[0] = (unsigned char)( len >> 8 );
3386 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003387
Paul Bakker48916f92012-09-16 19:57:18 +00003388 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003389 {
3390 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003393 return( ret );
3394 }
3395
3396 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003397 ssl->out_len[0] = (unsigned char)( len >> 8 );
3398 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003399 }
3400
Hanno Becker2b1e3542018-08-06 11:19:13 +01003401 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3402
3403#if defined(MBEDTLS_SSL_PROTO_DTLS)
3404 /* In case of DTLS, double-check that we don't exceed
3405 * the remaining space in the datagram. */
3406 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3407 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003408 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003409 if( ret < 0 )
3410 return( ret );
3411
3412 if( protected_record_size > (size_t) ret )
3413 {
3414 /* Should never happen */
3415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3416 }
3417 }
3418#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003421 "version = [%d:%d], msglen = %d",
3422 ssl->out_hdr[0], ssl->out_hdr[1],
3423 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003426 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003427
3428 ssl->out_left += protected_record_size;
3429 ssl->out_hdr += protected_record_size;
3430 ssl_update_out_pointers( ssl, ssl->transform_out );
3431
Hanno Becker04484622018-08-06 09:49:38 +01003432 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3433 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3434 break;
3435
3436 /* The loop goes to its end iff the counter is wrapping */
3437 if( i == ssl_ep_len( ssl ) )
3438 {
3439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3440 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 }
3443
Hanno Becker67bc7c32018-08-06 11:33:50 +01003444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3446 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003447 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003448 size_t remaining;
3449 ret = ssl_get_remaining_payload_in_datagram( ssl );
3450 if( ret < 0 )
3451 {
3452 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3453 ret );
3454 return( ret );
3455 }
3456
3457 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003458 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003459 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003460 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003461 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003462 else
3463 {
Hanno Becker513815a2018-08-20 11:56:09 +01003464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 }
3466 }
3467#endif /* MBEDTLS_SSL_PROTO_DTLS */
3468
3469 if( ( flush == SSL_FORCE_FLUSH ) &&
3470 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 return( ret );
3474 }
3475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003477
3478 return( 0 );
3479}
3480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003482
3483static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3484{
3485 if( ssl->in_msglen < ssl->in_hslen ||
3486 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3487 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3488 {
3489 return( 1 );
3490 }
3491 return( 0 );
3492}
Hanno Becker44650b72018-08-16 12:51:11 +01003493
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003494static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003495{
3496 return( ( ssl->in_msg[9] << 16 ) |
3497 ( ssl->in_msg[10] << 8 ) |
3498 ssl->in_msg[11] );
3499}
3500
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003501static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003502{
3503 return( ( ssl->in_msg[6] << 16 ) |
3504 ( ssl->in_msg[7] << 8 ) |
3505 ssl->in_msg[8] );
3506}
3507
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003508static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003509{
3510 uint32_t msg_len, frag_off, frag_len;
3511
3512 msg_len = ssl_get_hs_total_len( ssl );
3513 frag_off = ssl_get_hs_frag_off( ssl );
3514 frag_len = ssl_get_hs_frag_len( ssl );
3515
3516 if( frag_off > msg_len )
3517 return( -1 );
3518
3519 if( frag_len > msg_len - frag_off )
3520 return( -1 );
3521
3522 if( frag_len + 12 > ssl->in_msglen )
3523 return( -1 );
3524
3525 return( 0 );
3526}
3527
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003528/*
3529 * Mark bits in bitmask (used for DTLS HS reassembly)
3530 */
3531static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3532{
3533 unsigned int start_bits, end_bits;
3534
3535 start_bits = 8 - ( offset % 8 );
3536 if( start_bits != 8 )
3537 {
3538 size_t first_byte_idx = offset / 8;
3539
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003540 /* Special case */
3541 if( len <= start_bits )
3542 {
3543 for( ; len != 0; len-- )
3544 mask[first_byte_idx] |= 1 << ( start_bits - len );
3545
3546 /* Avoid potential issues with offset or len becoming invalid */
3547 return;
3548 }
3549
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003550 offset += start_bits; /* Now offset % 8 == 0 */
3551 len -= start_bits;
3552
3553 for( ; start_bits != 0; start_bits-- )
3554 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3555 }
3556
3557 end_bits = len % 8;
3558 if( end_bits != 0 )
3559 {
3560 size_t last_byte_idx = ( offset + len ) / 8;
3561
3562 len -= end_bits; /* Now len % 8 == 0 */
3563
3564 for( ; end_bits != 0; end_bits-- )
3565 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3566 }
3567
3568 memset( mask + offset / 8, 0xFF, len / 8 );
3569}
3570
3571/*
3572 * Check that bitmask is full
3573 */
3574static int ssl_bitmask_check( unsigned char *mask, size_t len )
3575{
3576 size_t i;
3577
3578 for( i = 0; i < len / 8; i++ )
3579 if( mask[i] != 0xFF )
3580 return( -1 );
3581
3582 for( i = 0; i < len % 8; i++ )
3583 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3584 return( -1 );
3585
3586 return( 0 );
3587}
3588
Hanno Becker56e205e2018-08-16 09:06:12 +01003589/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003590static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003591 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003592{
Hanno Becker56e205e2018-08-16 09:06:12 +01003593 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003594
Hanno Becker56e205e2018-08-16 09:06:12 +01003595 alloc_len = 12; /* Handshake header */
3596 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003597
Hanno Beckerd07df862018-08-16 09:14:58 +01003598 if( add_bitmap )
3599 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003600
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003601 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003602}
Hanno Becker56e205e2018-08-16 09:06:12 +01003603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003605
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003606static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003607{
3608 return( ( ssl->in_msg[1] << 16 ) |
3609 ( ssl->in_msg[2] << 8 ) |
3610 ssl->in_msg[3] );
3611}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003612
Simon Butcher99000142016-10-13 17:21:01 +01003613int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003618 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003620 }
3621
Hanno Becker12555c62018-08-16 12:47:53 +01003622 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003625 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003626 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003629 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003630 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003631 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003632 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003633
Hanno Becker44650b72018-08-16 12:51:11 +01003634 if( ssl_check_hs_header( ssl ) != 0 )
3635 {
3636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3637 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3638 }
3639
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003640 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003641 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3642 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3643 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3644 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003645 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003646 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3647 {
3648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3649 recv_msg_seq,
3650 ssl->handshake->in_msg_seq ) );
3651 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3652 }
3653
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003654 /* Retransmit only on last message from previous flight, to avoid
3655 * too many retransmissions.
3656 * Besides, No sane server ever retransmits HelloVerifyRequest */
3657 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003661 "message_seq = %d, start_of_flight = %d",
3662 recv_msg_seq,
3663 ssl->handshake->in_flight_start_seq ) );
3664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003668 return( ret );
3669 }
3670 }
3671 else
3672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003674 "message_seq = %d, expected = %d",
3675 recv_msg_seq,
3676 ssl->handshake->in_msg_seq ) );
3677 }
3678
Hanno Becker90333da2017-10-10 11:27:13 +01003679 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003680 }
3681 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003682
Hanno Becker6d97ef52018-08-16 13:09:04 +01003683 /* Message reassembly is handled alongside buffering of future
3684 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003685 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003686 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003687 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003690 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003691 }
3692 }
3693 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003695 /* With TLS we don't handle fragmentation (for now) */
3696 if( ssl->in_msglen < ssl->in_hslen )
3697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3699 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003700 }
3701
Simon Butcher99000142016-10-13 17:21:01 +01003702 return( 0 );
3703}
3704
3705void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3706{
Hanno Becker0271f962018-08-16 13:23:47 +01003707 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003708
Hanno Becker0271f962018-08-16 13:23:47 +01003709 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003710 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003711 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003712 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003713
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003714 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003716 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003717 ssl->handshake != NULL )
3718 {
Hanno Becker0271f962018-08-16 13:23:47 +01003719 unsigned offset;
3720 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003721
Hanno Becker0271f962018-08-16 13:23:47 +01003722 /* Increment handshake sequence number */
3723 hs->in_msg_seq++;
3724
3725 /*
3726 * Clear up handshake buffering and reassembly structure.
3727 */
3728
3729 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003730 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003731
3732 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003733 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3734 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003735 offset++, hs_buf++ )
3736 {
3737 *hs_buf = *(hs_buf + 1);
3738 }
3739
3740 /* Create a fresh last entry */
3741 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003742 }
3743#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003744}
3745
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003746/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003747 * DTLS anti-replay: RFC 6347 4.1.2.6
3748 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003749 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3750 * Bit n is set iff record number in_window_top - n has been seen.
3751 *
3752 * Usually, in_window_top is the last record number seen and the lsb of
3753 * in_window is set. The only exception is the initial state (record number 0
3754 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3757static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003758{
3759 ssl->in_window_top = 0;
3760 ssl->in_window = 0;
3761}
3762
3763static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3764{
3765 return( ( (uint64_t) buf[0] << 40 ) |
3766 ( (uint64_t) buf[1] << 32 ) |
3767 ( (uint64_t) buf[2] << 24 ) |
3768 ( (uint64_t) buf[3] << 16 ) |
3769 ( (uint64_t) buf[4] << 8 ) |
3770 ( (uint64_t) buf[5] ) );
3771}
3772
3773/*
3774 * Return 0 if sequence number is acceptable, -1 otherwise
3775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003777{
3778 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3779 uint64_t bit;
3780
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003781 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003782 return( 0 );
3783
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003784 if( rec_seqnum > ssl->in_window_top )
3785 return( 0 );
3786
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003787 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003788
3789 if( bit >= 64 )
3790 return( -1 );
3791
3792 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3793 return( -1 );
3794
3795 return( 0 );
3796}
3797
3798/*
3799 * Update replay window on new validated record
3800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003802{
3803 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3804
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003805 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003806 return;
3807
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003808 if( rec_seqnum > ssl->in_window_top )
3809 {
3810 /* Update window_top and the contents of the window */
3811 uint64_t shift = rec_seqnum - ssl->in_window_top;
3812
3813 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003814 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003815 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003816 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003817 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003818 ssl->in_window |= 1;
3819 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003820
3821 ssl->in_window_top = rec_seqnum;
3822 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003823 else
3824 {
3825 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003826 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003827
3828 if( bit < 64 ) /* Always true, but be extra sure */
3829 ssl->in_window |= (uint64_t) 1 << bit;
3830 }
3831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003833
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003834#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003835/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003836static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3837
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003838/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003839 * Without any SSL context, check if a datagram looks like a ClientHello with
3840 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003841 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003842 *
3843 * - if cookie is valid, return 0
3844 * - if ClientHello looks superficially valid but cookie is not,
3845 * fill obuf and set olen, then
3846 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3847 * - otherwise return a specific error code
3848 */
3849static int ssl_check_dtls_clihlo_cookie(
3850 mbedtls_ssl_cookie_write_t *f_cookie_write,
3851 mbedtls_ssl_cookie_check_t *f_cookie_check,
3852 void *p_cookie,
3853 const unsigned char *cli_id, size_t cli_id_len,
3854 const unsigned char *in, size_t in_len,
3855 unsigned char *obuf, size_t buf_len, size_t *olen )
3856{
3857 size_t sid_len, cookie_len;
3858 unsigned char *p;
3859
3860 if( f_cookie_write == NULL || f_cookie_check == NULL )
3861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3862
3863 /*
3864 * Structure of ClientHello with record and handshake headers,
3865 * and expected values. We don't need to check a lot, more checks will be
3866 * done when actually parsing the ClientHello - skipping those checks
3867 * avoids code duplication and does not make cookie forging any easier.
3868 *
3869 * 0-0 ContentType type; copied, must be handshake
3870 * 1-2 ProtocolVersion version; copied
3871 * 3-4 uint16 epoch; copied, must be 0
3872 * 5-10 uint48 sequence_number; copied
3873 * 11-12 uint16 length; (ignored)
3874 *
3875 * 13-13 HandshakeType msg_type; (ignored)
3876 * 14-16 uint24 length; (ignored)
3877 * 17-18 uint16 message_seq; copied
3878 * 19-21 uint24 fragment_offset; copied, must be 0
3879 * 22-24 uint24 fragment_length; (ignored)
3880 *
3881 * 25-26 ProtocolVersion client_version; (ignored)
3882 * 27-58 Random random; (ignored)
3883 * 59-xx SessionID session_id; 1 byte len + sid_len content
3884 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3885 * ...
3886 *
3887 * Minimum length is 61 bytes.
3888 */
3889 if( in_len < 61 ||
3890 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3891 in[3] != 0 || in[4] != 0 ||
3892 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3893 {
3894 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3895 }
3896
3897 sid_len = in[59];
3898 if( sid_len > in_len - 61 )
3899 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3900
3901 cookie_len = in[60 + sid_len];
3902 if( cookie_len > in_len - 60 )
3903 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3904
3905 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3906 cli_id, cli_id_len ) == 0 )
3907 {
3908 /* Valid cookie */
3909 return( 0 );
3910 }
3911
3912 /*
3913 * If we get here, we've got an invalid cookie, let's prepare HVR.
3914 *
3915 * 0-0 ContentType type; copied
3916 * 1-2 ProtocolVersion version; copied
3917 * 3-4 uint16 epoch; copied
3918 * 5-10 uint48 sequence_number; copied
3919 * 11-12 uint16 length; olen - 13
3920 *
3921 * 13-13 HandshakeType msg_type; hello_verify_request
3922 * 14-16 uint24 length; olen - 25
3923 * 17-18 uint16 message_seq; copied
3924 * 19-21 uint24 fragment_offset; copied
3925 * 22-24 uint24 fragment_length; olen - 25
3926 *
3927 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3928 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3929 *
3930 * Minimum length is 28.
3931 */
3932 if( buf_len < 28 )
3933 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3934
3935 /* Copy most fields and adapt others */
3936 memcpy( obuf, in, 25 );
3937 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3938 obuf[25] = 0xfe;
3939 obuf[26] = 0xff;
3940
3941 /* Generate and write actual cookie */
3942 p = obuf + 28;
3943 if( f_cookie_write( p_cookie,
3944 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3945 {
3946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3947 }
3948
3949 *olen = p - obuf;
3950
3951 /* Go back and fill length fields */
3952 obuf[27] = (unsigned char)( *olen - 28 );
3953
3954 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3955 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3956 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3957
3958 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3959 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3960
3961 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3962}
3963
3964/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003965 * Handle possible client reconnect with the same UDP quadruplet
3966 * (RFC 6347 Section 4.2.8).
3967 *
3968 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3969 * that looks like a ClientHello.
3970 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003971 * - if the input looks like a ClientHello without cookies,
3972 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003973 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003974 * - if the input looks like a ClientHello with a valid cookie,
3975 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003976 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003977 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003978 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003979 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003980 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3981 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003982 */
3983static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3984{
3985 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003986 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003987
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003988 ret = ssl_check_dtls_clihlo_cookie(
3989 ssl->conf->f_cookie_write,
3990 ssl->conf->f_cookie_check,
3991 ssl->conf->p_cookie,
3992 ssl->cli_id, ssl->cli_id_len,
3993 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003994 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003995
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003996 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3997
3998 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003999 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004000 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004001 * If the error is permanent we'll catch it later,
4002 * if it's not, then hopefully it'll work next time. */
4003 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4004
4005 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004006 }
4007
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004008 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004009 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004010 /* Got a valid cookie, partially reset context */
4011 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4012 {
4013 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4014 return( ret );
4015 }
4016
4017 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004018 }
4019
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004020 return( ret );
4021}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004022#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004023
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004024/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004025 * ContentType type;
4026 * ProtocolVersion version;
4027 * uint16 epoch; // DTLS only
4028 * uint48 sequence_number; // DTLS only
4029 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004030 *
4031 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004032 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004033 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4034 *
4035 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004036 * 1. proceed with the record if this function returns 0
4037 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4038 * 3. return CLIENT_RECONNECT if this function return that value
4039 * 4. drop the whole datagram if this function returns anything else.
4040 * Point 2 is needed when the peer is resending, and we have already received
4041 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004044{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004045 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 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 +02004048
Paul Bakker5121ce52009-01-03 21:22:43 +00004049 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004050 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004051 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004054 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004055 ssl->in_msgtype,
4056 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004057
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004058 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4060 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4061 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4062 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004065
4066#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004067 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4068 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004069 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4070#endif /* MBEDTLS_SSL_PROTO_DTLS */
4071 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4072 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004075 }
4076
4077 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004078 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4081 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004082 }
4083
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004084 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4087 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004088 }
4089
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004090 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004091 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004092 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4095 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004096 }
4097
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004098 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004099 * DTLS-related tests.
4100 * Check epoch before checking length constraint because
4101 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4102 * message gets duplicated before the corresponding Finished message,
4103 * the second ChangeCipherSpec should be discarded because it belongs
4104 * to an old epoch, but not because its length is shorter than
4105 * the minimum record length for packets using the new record transform.
4106 * Note that these two kinds of failures are handled differently,
4107 * as an unexpected record is silently skipped but an invalid
4108 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004109 */
4110#if defined(MBEDTLS_SSL_PROTO_DTLS)
4111 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4112 {
4113 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4114
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004115 /* Check epoch (and sequence number) with DTLS */
4116 if( rec_epoch != ssl->in_epoch )
4117 {
4118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4119 "expected %d, received %d",
4120 ssl->in_epoch, rec_epoch ) );
4121
4122#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4123 /*
4124 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4125 * access the first byte of record content (handshake type), as we
4126 * have an active transform (possibly iv_len != 0), so use the
4127 * fact that the record header len is 13 instead.
4128 */
4129 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4130 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4131 rec_epoch == 0 &&
4132 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4133 ssl->in_left > 13 &&
4134 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4137 "from the same port" ) );
4138 return( ssl_handle_possible_reconnect( ssl ) );
4139 }
4140 else
4141#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004142 {
4143 /* Consider buffering the record. */
4144 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4145 {
4146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4147 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4148 }
4149
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004150 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004151 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004152 }
4153
4154#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4155 /* Replay detection only works for the current epoch */
4156 if( rec_epoch == ssl->in_epoch &&
4157 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4158 {
4159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4160 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4161 }
4162#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004163
Hanno Becker52c6dc62017-05-26 16:07:36 +01004164 /* Drop unexpected ApplicationData records,
4165 * except at the beginning of renegotiations */
4166 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4167 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4168#if defined(MBEDTLS_SSL_RENEGOTIATION)
4169 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4170 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4171#endif
4172 )
4173 {
4174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4175 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4176 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004177 }
4178#endif /* MBEDTLS_SSL_PROTO_DTLS */
4179
Hanno Becker52c6dc62017-05-26 16:07:36 +01004180
4181 /* Check length against bounds of the current transform and version */
4182 if( ssl->transform_in == NULL )
4183 {
4184 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004185 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004186 {
4187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4188 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4189 }
4190 }
4191 else
4192 {
4193 if( ssl->in_msglen < ssl->transform_in->minlen )
4194 {
4195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4196 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4197 }
4198
4199#if defined(MBEDTLS_SSL_PROTO_SSL3)
4200 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004201 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004202 {
4203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4204 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4205 }
4206#endif
4207#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4208 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4209 /*
4210 * TLS encrypted messages can have up to 256 bytes of padding
4211 */
4212 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4213 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004214 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
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 }
4221
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004222 return( 0 );
4223}
Paul Bakker5121ce52009-01-03 21:22:43 +00004224
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004225/*
4226 * If applicable, decrypt (and decompress) record content
4227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004229{
4230 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4233 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4236 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 ret = mbedtls_ssl_hw_record_read( ssl );
4241 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4244 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004245 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004246
4247 if( ret == 0 )
4248 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004251 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004252 {
4253 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004256 return( ret );
4257 }
4258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 ssl->in_msg, ssl->in_msglen );
4261
Angus Grattond8213d02016-05-25 20:56:48 +10004262 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4265 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 }
4267 }
4268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004270 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004272 {
4273 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004276 return( ret );
4277 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004278 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004282 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004285 }
4286#endif
4287
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004288 return( 0 );
4289}
4290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004292
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004293/*
4294 * Read a record.
4295 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004296 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4297 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4298 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004299 */
Hanno Becker1097b342018-08-15 14:09:41 +01004300
4301/* Helper functions for mbedtls_ssl_read_record(). */
4302static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004303static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4304static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004305
Hanno Becker327c93b2018-08-15 13:56:18 +01004306int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004307 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004308{
4309 int ret;
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004312
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004313 if( ssl->keep_current_message == 0 )
4314 {
4315 do {
Simon Butcher99000142016-10-13 17:21:01 +01004316
Hanno Becker26994592018-08-15 14:14:59 +01004317 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004318 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004319 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004320
Hanno Beckere74d5562018-08-15 14:26:08 +01004321 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004322 {
Hanno Becker40f50842018-08-15 14:48:01 +01004323#if defined(MBEDTLS_SSL_PROTO_DTLS)
4324 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004325
Hanno Becker40f50842018-08-15 14:48:01 +01004326 /* We only check for buffered messages if the
4327 * current datagram is fully consumed. */
4328 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004329 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004330 {
Hanno Becker40f50842018-08-15 14:48:01 +01004331 if( ssl_load_buffered_message( ssl ) == 0 )
4332 have_buffered = 1;
4333 }
4334
4335 if( have_buffered == 0 )
4336#endif /* MBEDTLS_SSL_PROTO_DTLS */
4337 {
4338 ret = ssl_get_next_record( ssl );
4339 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4340 continue;
4341
4342 if( ret != 0 )
4343 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004344 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004345 return( ret );
4346 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004347 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004348 }
4349
4350 ret = mbedtls_ssl_handle_message_type( ssl );
4351
Hanno Becker40f50842018-08-15 14:48:01 +01004352#if defined(MBEDTLS_SSL_PROTO_DTLS)
4353 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4354 {
4355 /* Buffer future message */
4356 ret = ssl_buffer_message( ssl );
4357 if( ret != 0 )
4358 return( ret );
4359
4360 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4361 }
4362#endif /* MBEDTLS_SSL_PROTO_DTLS */
4363
Hanno Becker90333da2017-10-10 11:27:13 +01004364 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4365 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004366
4367 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004368 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004369 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004370 return( ret );
4371 }
4372
Hanno Becker327c93b2018-08-15 13:56:18 +01004373 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004374 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004375 {
4376 mbedtls_ssl_update_handshake_status( ssl );
4377 }
Simon Butcher99000142016-10-13 17:21:01 +01004378 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004379 else
Simon Butcher99000142016-10-13 17:21:01 +01004380 {
Hanno Becker02f59072018-08-15 14:00:24 +01004381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004382 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004383 }
4384
4385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4386
4387 return( 0 );
4388}
4389
Hanno Becker40f50842018-08-15 14:48:01 +01004390#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004391static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004392{
Hanno Becker40f50842018-08-15 14:48:01 +01004393 if( ssl->in_left > ssl->next_record_offset )
4394 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004395
Hanno Becker40f50842018-08-15 14:48:01 +01004396 return( 0 );
4397}
4398
4399static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4400{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004401 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004402 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004403 int ret = 0;
4404
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004405 if( hs == NULL )
4406 return( -1 );
4407
Hanno Beckere00ae372018-08-20 09:39:42 +01004408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4409
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004410 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4411 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4412 {
4413 /* Check if we have seen a ChangeCipherSpec before.
4414 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004415 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004416 {
4417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4418 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004419 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004420 }
4421
Hanno Becker39b8bc92018-08-28 17:17:13 +01004422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004423 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4424 ssl->in_msglen = 1;
4425 ssl->in_msg[0] = 1;
4426
4427 /* As long as they are equal, the exact value doesn't matter. */
4428 ssl->in_left = 0;
4429 ssl->next_record_offset = 0;
4430
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004431 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004432 goto exit;
4433 }
Hanno Becker37f95322018-08-16 13:55:32 +01004434
Hanno Beckerb8f50142018-08-28 10:01:34 +01004435#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004436 /* Debug only */
4437 {
4438 unsigned offset;
4439 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4440 {
4441 hs_buf = &hs->buffering.hs[offset];
4442 if( hs_buf->is_valid == 1 )
4443 {
4444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4445 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004446 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004447 }
4448 }
4449 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004450#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004451
4452 /* Check if we have buffered and/or fully reassembled the
4453 * next handshake message. */
4454 hs_buf = &hs->buffering.hs[0];
4455 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4456 {
4457 /* Synthesize a record containing the buffered HS message. */
4458 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4459 ( hs_buf->data[2] << 8 ) |
4460 hs_buf->data[3];
4461
4462 /* Double-check that we haven't accidentally buffered
4463 * a message that doesn't fit into the input buffer. */
4464 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4465 {
4466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4468 }
4469
4470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4471 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4472 hs_buf->data, msg_len + 12 );
4473
4474 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4475 ssl->in_hslen = msg_len + 12;
4476 ssl->in_msglen = msg_len + 12;
4477 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4478
4479 ret = 0;
4480 goto exit;
4481 }
4482 else
4483 {
4484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4485 hs->in_msg_seq ) );
4486 }
4487
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004488 ret = -1;
4489
4490exit:
4491
4492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4493 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004494}
4495
Hanno Beckera02b0b42018-08-21 17:20:27 +01004496static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4497 size_t desired )
4498{
4499 int offset;
4500 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4502 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004503
Hanno Becker01315ea2018-08-21 17:22:17 +01004504 /* Get rid of future records epoch first, if such exist. */
4505 ssl_free_buffered_record( ssl );
4506
4507 /* Check if we have enough space available now. */
4508 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4509 hs->buffering.total_bytes_buffered ) )
4510 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004512 return( 0 );
4513 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004514
Hanno Becker4f432ad2018-08-28 10:02:32 +01004515 /* We don't have enough space to buffer the next expected handshake
4516 * message. Remove buffers used for future messages to gain space,
4517 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004518 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4519 offset >= 0; offset-- )
4520 {
4521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4522 offset ) );
4523
Hanno Beckerb309b922018-08-23 13:18:05 +01004524 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004525
4526 /* Check if we have enough space available now. */
4527 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4528 hs->buffering.total_bytes_buffered ) )
4529 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004531 return( 0 );
4532 }
4533 }
4534
4535 return( -1 );
4536}
4537
Hanno Becker40f50842018-08-15 14:48:01 +01004538static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4539{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004540 int ret = 0;
4541 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4542
4543 if( hs == NULL )
4544 return( 0 );
4545
4546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4547
4548 switch( ssl->in_msgtype )
4549 {
4550 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004552
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004553 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004554 break;
4555
4556 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004557 {
4558 unsigned recv_msg_seq_offset;
4559 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4560 mbedtls_ssl_hs_buffer *hs_buf;
4561 size_t msg_len = ssl->in_hslen - 12;
4562
4563 /* We should never receive an old handshake
4564 * message - double-check nonetheless. */
4565 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4566 {
4567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4568 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4569 }
4570
4571 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4572 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4573 {
4574 /* Silently ignore -- message too far in the future */
4575 MBEDTLS_SSL_DEBUG_MSG( 2,
4576 ( "Ignore future HS message with sequence number %u, "
4577 "buffering window %u - %u",
4578 recv_msg_seq, ssl->handshake->in_msg_seq,
4579 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4580
4581 goto exit;
4582 }
4583
4584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4585 recv_msg_seq, recv_msg_seq_offset ) );
4586
4587 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4588
4589 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004590 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004591 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004592 size_t reassembly_buf_sz;
4593
Hanno Becker37f95322018-08-16 13:55:32 +01004594 hs_buf->is_fragmented =
4595 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4596
4597 /* We copy the message back into the input buffer
4598 * after reassembly, so check that it's not too large.
4599 * This is an implementation-specific limitation
4600 * and not one from the standard, hence it is not
4601 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004602 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004603 {
4604 /* Ignore message */
4605 goto exit;
4606 }
4607
Hanno Beckere0b150f2018-08-21 15:51:03 +01004608 /* Check if we have enough space to buffer the message. */
4609 if( hs->buffering.total_bytes_buffered >
4610 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4611 {
4612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4614 }
4615
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004616 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4617 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004618
4619 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4620 hs->buffering.total_bytes_buffered ) )
4621 {
4622 if( recv_msg_seq_offset > 0 )
4623 {
4624 /* If we can't buffer a future message because
4625 * of space limitations -- ignore. */
4626 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",
4627 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4628 (unsigned) hs->buffering.total_bytes_buffered ) );
4629 goto exit;
4630 }
Hanno Beckere1801392018-08-21 16:51:05 +01004631 else
4632 {
4633 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",
4634 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4635 (unsigned) hs->buffering.total_bytes_buffered ) );
4636 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004637
Hanno Beckera02b0b42018-08-21 17:20:27 +01004638 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004639 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004640 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",
4641 (unsigned) msg_len,
4642 (unsigned) reassembly_buf_sz,
4643 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004644 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004645 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4646 goto exit;
4647 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004648 }
4649
4650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4651 msg_len ) );
4652
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004653 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4654 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004655 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004656 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004657 goto exit;
4658 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004659 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004660
4661 /* Prepare final header: copy msg_type, length and message_seq,
4662 * then add standardised fragment_offset and fragment_length */
4663 memcpy( hs_buf->data, ssl->in_msg, 6 );
4664 memset( hs_buf->data + 6, 0, 3 );
4665 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4666
4667 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004668
4669 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004670 }
4671 else
4672 {
4673 /* Make sure msg_type and length are consistent */
4674 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4675 {
4676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4677 /* Ignore */
4678 goto exit;
4679 }
4680 }
4681
Hanno Becker4422bbb2018-08-20 09:40:19 +01004682 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004683 {
4684 size_t frag_len, frag_off;
4685 unsigned char * const msg = hs_buf->data + 12;
4686
4687 /*
4688 * Check and copy current fragment
4689 */
4690
4691 /* Validation of header fields already done in
4692 * mbedtls_ssl_prepare_handshake_record(). */
4693 frag_off = ssl_get_hs_frag_off( ssl );
4694 frag_len = ssl_get_hs_frag_len( ssl );
4695
4696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4697 frag_off, frag_len ) );
4698 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4699
4700 if( hs_buf->is_fragmented )
4701 {
4702 unsigned char * const bitmask = msg + msg_len;
4703 ssl_bitmask_set( bitmask, frag_off, frag_len );
4704 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4705 msg_len ) == 0 );
4706 }
4707 else
4708 {
4709 hs_buf->is_complete = 1;
4710 }
4711
4712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4713 hs_buf->is_complete ? "" : "not yet " ) );
4714 }
4715
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004716 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004717 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004718
4719 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004720 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004721 break;
4722 }
4723
4724exit:
4725
4726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4727 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004728}
4729#endif /* MBEDTLS_SSL_PROTO_DTLS */
4730
Hanno Becker1097b342018-08-15 14:09:41 +01004731static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004732{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004733 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004734 * Consume last content-layer message and potentially
4735 * update in_msglen which keeps track of the contents'
4736 * consumption state.
4737 *
4738 * (1) Handshake messages:
4739 * Remove last handshake message, move content
4740 * and adapt in_msglen.
4741 *
4742 * (2) Alert messages:
4743 * Consume whole record content, in_msglen = 0.
4744 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004745 * (3) Change cipher spec:
4746 * Consume whole record content, in_msglen = 0.
4747 *
4748 * (4) Application data:
4749 * Don't do anything - the record layer provides
4750 * the application data as a stream transport
4751 * and consumes through mbedtls_ssl_read only.
4752 *
4753 */
4754
4755 /* Case (1): Handshake messages */
4756 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004757 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004758 /* Hard assertion to be sure that no application data
4759 * is in flight, as corrupting ssl->in_msglen during
4760 * ssl->in_offt != NULL is fatal. */
4761 if( ssl->in_offt != NULL )
4762 {
4763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4764 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4765 }
4766
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004767 /*
4768 * Get next Handshake message in the current record
4769 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004770
Hanno Becker4a810fb2017-05-24 16:27:30 +01004771 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004772 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004773 * current handshake content: If DTLS handshake
4774 * fragmentation is used, that's the fragment
4775 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004776 * size here is faulty and should be changed at
4777 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004778 * (2) While it doesn't seem to cause problems, one
4779 * has to be very careful not to assume that in_hslen
4780 * is always <= in_msglen in a sensible communication.
4781 * Again, it's wrong for DTLS handshake fragmentation.
4782 * The following check is therefore mandatory, and
4783 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004784 * Additionally, ssl->in_hslen might be arbitrarily out of
4785 * bounds after handling a DTLS message with an unexpected
4786 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004787 */
4788 if( ssl->in_hslen < ssl->in_msglen )
4789 {
4790 ssl->in_msglen -= ssl->in_hslen;
4791 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4792 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004793
Hanno Becker4a810fb2017-05-24 16:27:30 +01004794 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4795 ssl->in_msg, ssl->in_msglen );
4796 }
4797 else
4798 {
4799 ssl->in_msglen = 0;
4800 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004801
Hanno Becker4a810fb2017-05-24 16:27:30 +01004802 ssl->in_hslen = 0;
4803 }
4804 /* Case (4): Application data */
4805 else if( ssl->in_offt != NULL )
4806 {
4807 return( 0 );
4808 }
4809 /* Everything else (CCS & Alerts) */
4810 else
4811 {
4812 ssl->in_msglen = 0;
4813 }
4814
Hanno Becker1097b342018-08-15 14:09:41 +01004815 return( 0 );
4816}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004817
Hanno Beckere74d5562018-08-15 14:26:08 +01004818static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4819{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004820 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004821 return( 1 );
4822
4823 return( 0 );
4824}
4825
Hanno Becker5f066e72018-08-16 14:56:31 +01004826#if defined(MBEDTLS_SSL_PROTO_DTLS)
4827
4828static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4829{
4830 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4831 if( hs == NULL )
4832 return;
4833
Hanno Becker01315ea2018-08-21 17:22:17 +01004834 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004835 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004836 hs->buffering.total_bytes_buffered -=
4837 hs->buffering.future_record.len;
4838
4839 mbedtls_free( hs->buffering.future_record.data );
4840 hs->buffering.future_record.data = NULL;
4841 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004842}
4843
4844static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4845{
4846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4847 unsigned char * rec;
4848 size_t rec_len;
4849 unsigned rec_epoch;
4850
4851 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4852 return( 0 );
4853
4854 if( hs == NULL )
4855 return( 0 );
4856
Hanno Becker5f066e72018-08-16 14:56:31 +01004857 rec = hs->buffering.future_record.data;
4858 rec_len = hs->buffering.future_record.len;
4859 rec_epoch = hs->buffering.future_record.epoch;
4860
4861 if( rec == NULL )
4862 return( 0 );
4863
Hanno Becker4cb782d2018-08-20 11:19:05 +01004864 /* Only consider loading future records if the
4865 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004866 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004867 return( 0 );
4868
Hanno Becker5f066e72018-08-16 14:56:31 +01004869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4870
4871 if( rec_epoch != ssl->in_epoch )
4872 {
4873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4874 goto exit;
4875 }
4876
4877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4878
4879 /* Double-check that the record is not too large */
4880 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4881 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4882 {
4883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4885 }
4886
4887 memcpy( ssl->in_hdr, rec, rec_len );
4888 ssl->in_left = rec_len;
4889 ssl->next_record_offset = 0;
4890
4891 ssl_free_buffered_record( ssl );
4892
4893exit:
4894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4895 return( 0 );
4896}
4897
4898static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4899{
4900 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4901 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004902 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004903
4904 /* Don't buffer future records outside handshakes. */
4905 if( hs == NULL )
4906 return( 0 );
4907
4908 /* Only buffer handshake records (we are only interested
4909 * in Finished messages). */
4910 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4911 return( 0 );
4912
4913 /* Don't buffer more than one future epoch record. */
4914 if( hs->buffering.future_record.data != NULL )
4915 return( 0 );
4916
Hanno Becker01315ea2018-08-21 17:22:17 +01004917 /* Don't buffer record if there's not enough buffering space remaining. */
4918 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4919 hs->buffering.total_bytes_buffered ) )
4920 {
4921 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",
4922 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4923 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004924 return( 0 );
4925 }
4926
Hanno Becker5f066e72018-08-16 14:56:31 +01004927 /* Buffer record */
4928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4929 ssl->in_epoch + 1 ) );
4930 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4931 rec_hdr_len + ssl->in_msglen );
4932
4933 /* ssl_parse_record_header() only considers records
4934 * of the next epoch as candidates for buffering. */
4935 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004936 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004937
4938 hs->buffering.future_record.data =
4939 mbedtls_calloc( 1, hs->buffering.future_record.len );
4940 if( hs->buffering.future_record.data == NULL )
4941 {
4942 /* If we run out of RAM trying to buffer a
4943 * record from the next epoch, just ignore. */
4944 return( 0 );
4945 }
4946
Hanno Becker01315ea2018-08-21 17:22:17 +01004947 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004948
Hanno Becker01315ea2018-08-21 17:22:17 +01004949 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004950 return( 0 );
4951}
4952
4953#endif /* MBEDTLS_SSL_PROTO_DTLS */
4954
Hanno Beckere74d5562018-08-15 14:26:08 +01004955static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004956{
4957 int ret;
4958
Hanno Becker5f066e72018-08-16 14:56:31 +01004959#if defined(MBEDTLS_SSL_PROTO_DTLS)
4960 /* We might have buffered a future record; if so,
4961 * and if the epoch matches now, load it.
4962 * On success, this call will set ssl->in_left to
4963 * the length of the buffered record, so that
4964 * the calls to ssl_fetch_input() below will
4965 * essentially be no-ops. */
4966 ret = ssl_load_buffered_record( ssl );
4967 if( ret != 0 )
4968 return( ret );
4969#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004974 return( ret );
4975 }
4976
4977 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004980 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4981 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004982 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004983 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4984 {
4985 ret = ssl_buffer_future_record( ssl );
4986 if( ret != 0 )
4987 return( ret );
4988
4989 /* Fall through to handling of unexpected records */
4990 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4991 }
4992
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004993 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4994 {
4995 /* Skip unexpected record (but not whole datagram) */
4996 ssl->next_record_offset = ssl->in_msglen
4997 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004998
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5000 "(header)" ) );
5001 }
5002 else
5003 {
5004 /* Skip invalid record and the rest of the datagram */
5005 ssl->next_record_offset = 0;
5006 ssl->in_left = 0;
5007
5008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5009 "(header)" ) );
5010 }
5011
5012 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005013 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005014 }
5015#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005016 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005017 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005018
5019 /*
5020 * Read and optionally decrypt the message contents
5021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5023 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005026 return( ret );
5027 }
5028
5029 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005034 if( ssl->next_record_offset < ssl->in_left )
5035 {
5036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5037 }
5038 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005039 else
5040#endif
5041 ssl->in_left = 0;
5042
5043 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005047 {
5048 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005049 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5050 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005051 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005052 /* Except when waiting for Finished as a bad mac here
5053 * probably means something went wrong in the handshake
5054 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5055 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5056 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5057 {
5058#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5059 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5060 {
5061 mbedtls_ssl_send_alert_message( ssl,
5062 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5063 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5064 }
5065#endif
5066 return( ret );
5067 }
5068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005070 if( ssl->conf->badmac_limit != 0 &&
5071 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5074 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005075 }
5076#endif
5077
Hanno Becker4a810fb2017-05-24 16:27:30 +01005078 /* As above, invalid records cause
5079 * dismissal of the whole datagram. */
5080
5081 ssl->next_record_offset = 0;
5082 ssl->in_left = 0;
5083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005085 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005086 }
5087
5088 return( ret );
5089 }
5090 else
5091#endif
5092 {
5093 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5095 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 mbedtls_ssl_send_alert_message( ssl,
5098 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5099 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005100 }
5101#endif
5102 return( ret );
5103 }
5104 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005105
Simon Butcher99000142016-10-13 17:21:01 +01005106 return( 0 );
5107}
5108
5109int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5110{
5111 int ret;
5112
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005113 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005114 * Handle particular types of records
5115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 {
Simon Butcher99000142016-10-13 17:21:01 +01005118 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5119 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005120 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005122 }
5123
Hanno Beckere678eaa2018-08-21 14:57:46 +01005124 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005125 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005126 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005127 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5129 ssl->in_msglen ) );
5130 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005131 }
5132
Hanno Beckere678eaa2018-08-21 14:57:46 +01005133 if( ssl->in_msg[0] != 1 )
5134 {
5135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5136 ssl->in_msg[0] ) );
5137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5138 }
5139
5140#if defined(MBEDTLS_SSL_PROTO_DTLS)
5141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5142 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5143 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5144 {
5145 if( ssl->handshake == NULL )
5146 {
5147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5148 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5149 }
5150
5151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5152 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5153 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005154#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005155 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005158 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005159 if( ssl->in_msglen != 2 )
5160 {
5161 /* Note: Standard allows for more than one 2 byte alert
5162 to be packed in a single message, but Mbed TLS doesn't
5163 currently support this. */
5164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5165 ssl->in_msglen ) );
5166 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5167 }
5168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005170 ssl->in_msg[0], ssl->in_msg[1] ) );
5171
5172 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005173 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005178 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005180 }
5181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5183 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5186 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005187 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005188
5189#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5190 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5191 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5192 {
Hanno Becker90333da2017-10-10 11:27:13 +01005193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005194 /* Will be handled when trying to parse ServerHello */
5195 return( 0 );
5196 }
5197#endif
5198
5199#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5200 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5201 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5202 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5203 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5204 {
5205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5206 /* Will be handled in mbedtls_ssl_parse_certificate() */
5207 return( 0 );
5208 }
5209#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5210
5211 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005212 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 }
5214
Hanno Beckerc76c6192017-06-06 10:03:17 +01005215#if defined(MBEDTLS_SSL_PROTO_DTLS)
5216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5217 ssl->handshake != NULL &&
5218 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5219 {
5220 ssl_handshake_wrapup_free_hs_transform( ssl );
5221 }
5222#endif
5223
Paul Bakker5121ce52009-01-03 21:22:43 +00005224 return( 0 );
5225}
5226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005228{
5229 int ret;
5230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5232 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5233 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005234 {
5235 return( ret );
5236 }
5237
5238 return( 0 );
5239}
5240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005242 unsigned char level,
5243 unsigned char message )
5244{
5245 int ret;
5246
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005247 if( ssl == NULL || ssl->conf == NULL )
5248 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005254 ssl->out_msglen = 2;
5255 ssl->out_msg[0] = level;
5256 ssl->out_msg[1] = message;
5257
Hanno Becker67bc7c32018-08-06 11:33:50 +01005258 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005261 return( ret );
5262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005264
5265 return( 0 );
5266}
5267
Paul Bakker5121ce52009-01-03 21:22:43 +00005268/*
5269 * Handshake functions
5270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5272 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5273 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5274 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5275 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5276 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5277 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005278/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5286 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005291 ssl->state++;
5292 return( 0 );
5293 }
5294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005297}
5298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5306 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005307 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5308 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005311 ssl->state++;
5312 return( 0 );
5313 }
5314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005317}
Gilles Peskinef9828522017-05-03 12:28:43 +02005318
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005319#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005320/* Some certificate support -> implement write and parse */
5321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005323{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005325 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 const mbedtls_x509_crt *crt;
5327 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005331 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5332 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005333 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5334 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005337 ssl->state++;
5338 return( 0 );
5339 }
5340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005342 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005343 {
5344 if( ssl->client_auth == 0 )
5345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005347 ssl->state++;
5348 return( 0 );
5349 }
5350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005352 /*
5353 * If using SSLv3 and got no cert, send an Alert message
5354 * (otherwise an empty Certificate message will be sent).
5355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5357 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005358 {
5359 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5361 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5362 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005365 goto write_msg;
5366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369#endif /* MBEDTLS_SSL_CLI_C */
5370#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005371 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5376 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005377 }
5378 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005382
5383 /*
5384 * 0 . 0 handshake type
5385 * 1 . 3 handshake length
5386 * 4 . 6 length of all certs
5387 * 7 . 9 length of cert. 1
5388 * 10 . n-1 peer certificate
5389 * n . n+2 length of cert. 2
5390 * n+3 . ... upper level cert, etc.
5391 */
5392 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005394
Paul Bakker29087132010-03-21 21:03:34 +00005395 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 {
5397 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005398 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005401 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005403 }
5404
5405 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5406 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5407 ssl->out_msg[i + 2] = (unsigned char)( n );
5408
5409 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5410 i += n; crt = crt->next;
5411 }
5412
5413 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5414 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5415 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5416
5417 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5419 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005420
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005421#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005422write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005423#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005424
5425 ssl->state++;
5426
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005427 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005428 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005430 return( ret );
5431 }
5432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005434
Paul Bakkered27a042013-04-18 22:46:23 +02005435 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005436}
5437
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005438static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5439 unsigned char *crt_buf,
5440 size_t crt_buf_len )
5441{
5442 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5443
5444 if( peer_crt == NULL )
5445 return( -1 );
5446
5447 if( peer_crt->raw.len != crt_buf_len )
5448 return( -1 );
5449
5450 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len) );
5451}
5452
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005453/*
5454 * Once the certificate message is read, parse it into a cert chain and
5455 * perform basic checks, but leave actual verification to the caller
5456 */
5457static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005458{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005459 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005460 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005461 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463#if defined(MBEDTLS_SSL_SRV_C)
5464#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 /*
5466 * Check if the client sent an empty certificate
5467 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005471 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5473 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5474 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005477
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005478 /* The client was asked for a certificate but didn't send
5479 one. The client should know what's going on, so we
5480 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005481 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005482 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005483 }
5484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5488 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005489 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5493 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5494 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5495 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005499 /* The client was asked for a certificate but didn't send
5500 one. The client should know what's going on, so we
5501 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005502 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005503 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 }
5505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5507 MBEDTLS_SSL_PROTO_TLS1_2 */
5508#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5514 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 }
5517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5519 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005522 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5523 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 }
5526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005528
Paul Bakker5121ce52009-01-03 21:22:43 +00005529 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005531 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005532 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005534 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005538 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5539 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005541 }
5542
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005543 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5544 i += 3;
5545
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005546 /* In case we tried to reuse a session but it failed */
5547 if( ssl->session_negotiate->peer_cert != NULL )
5548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5550 mbedtls_free( ssl->session_negotiate->peer_cert );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005551 ssl->session_negotiate->peer_cert = NULL;
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005552 }
5553
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005554 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 while( i < ssl->in_hslen )
5556 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005557 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005558 if ( i + 3 > ssl->in_hslen ) {
5559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5560 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005561 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005562 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5563 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005564 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5565 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005566 if( ssl->in_msg[i] != 0 )
5567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005569 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5570 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 }
5573
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005574 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005575 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5576 | (unsigned int) ssl->in_msg[i + 2];
5577 i += 3;
5578
5579 if( n < 128 || i + n > ssl->in_hslen )
5580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005582 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5583 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005585 }
5586
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005587 /* Check if we're handling the first CRT in the chain. */
5588 if( ssl->session_negotiate->peer_cert == NULL )
5589 {
5590 /* During client-side renegotiation, check the server's end-CRTs
5591 * hasn't changed compared to the initial handshake, mitigating
5592 * the triple handshake attack. On success, reuse the original
5593 * end-CRT instead of parsing it again. */
5594#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5596 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5597 {
5598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5599 if( ssl_check_peer_crt_unchanged( ssl, &ssl->in_msg[i], n ) != 0 )
5600 {
5601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5603 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5604 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5605 }
5606
5607 /* Move CRT chain structure to new session instance. */
5608 ssl->session_negotiate->peer_cert = ssl->session->peer_cert;
5609 ssl->session->peer_cert = NULL;
5610
5611 /* Delete all remaining CRTs from the original CRT chain. */
5612 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert->next );
5613 ssl->session_negotiate->peer_cert->next = NULL;
5614
5615 i += n;
5616 continue;
5617 }
5618#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5619
5620 /* Outside of client-side renegotiation, create a fresh X.509 CRT
5621 * instance to parse the end-CRT into. */
5622
5623 ssl->session_negotiate->peer_cert =
5624 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
5625 if( ssl->session_negotiate->peer_cert == NULL )
5626 {
5627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
5628 sizeof( mbedtls_x509_crt ) ) );
5629 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5630 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
5631 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
5632 }
5633
5634 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
5635
5636 /* Intentional fall through */
5637 }
5638
5639 /* Parse the next certificate in the chain. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005641 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005642 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005643 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005644 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;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005649
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005650 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5651 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5652 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005653
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005654 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5655 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5656 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005657
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005658 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 );
5662 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5663 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005664 }
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 );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005670 return( 0 );
5671}
5672
5673int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5674{
5675 int ret;
5676 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5677 ssl->transform_negotiate->ciphersuite_info;
5678#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5679 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5680 ? ssl->handshake->sni_authmode
5681 : ssl->conf->authmode;
5682#else
5683 const int authmode = ssl->conf->authmode;
5684#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005685 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005686
5687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5688
5689 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5690 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5691 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5692 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5693 {
5694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5695 ssl->state++;
5696 return( 0 );
5697 }
5698
5699#if defined(MBEDTLS_SSL_SRV_C)
5700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5701 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5702 {
5703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5704 ssl->state++;
5705 return( 0 );
5706 }
5707
5708 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5709 authmode == MBEDTLS_SSL_VERIFY_NONE )
5710 {
5711 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005713
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005714 ssl->state++;
5715 return( 0 );
5716 }
5717#endif
5718
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005719#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5720 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005721 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005722 {
5723 goto crt_verify;
5724 }
5725#endif
5726
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005727 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005728 {
5729 /* mbedtls_ssl_read_record may have sent an alert already. We
5730 let it decide whether to alert. */
5731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5732 return( ret );
5733 }
5734
5735 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5736 {
5737#if defined(MBEDTLS_SSL_SRV_C)
5738 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5739 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5740 {
5741 ret = 0;
5742 }
5743#endif
5744
5745 ssl->state++;
5746 return( ret );
5747 }
5748
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005749#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5750 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005751 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005752
5753crt_verify:
5754 if( ssl->handshake->ecrs_enabled)
5755 rs_ctx = &ssl->handshake->ecrs_ctx;
5756#endif
5757
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005758 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005759 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005760 mbedtls_x509_crt *ca_chain;
5761 mbedtls_x509_crl *ca_crl;
5762
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005763#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005764 if( ssl->handshake->sni_ca_chain != NULL )
5765 {
5766 ca_chain = ssl->handshake->sni_ca_chain;
5767 ca_crl = ssl->handshake->sni_ca_crl;
5768 }
5769 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005770#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005771 {
5772 ca_chain = ssl->conf->ca_chain;
5773 ca_crl = ssl->conf->ca_crl;
5774 }
5775
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005776 /*
5777 * Main check: verify certificate
5778 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005779 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005780 ssl->session_negotiate->peer_cert,
5781 ca_chain, ca_crl,
5782 ssl->conf->cert_profile,
5783 ssl->hostname,
5784 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005785 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005786
5787 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005790 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005791
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005792#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5793 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005794 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005795#endif
5796
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005797 /*
5798 * Secondary checks: always done, but change 'ret' only if it was 0
5799 */
5800
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005801#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005804
5805 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005807 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005808 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005809 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005812 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005814 }
5815 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005816#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005819 ciphersuite_info,
5820 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005821 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005824 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005826 }
5827
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005828 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5829 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5830 * with details encoded in the verification flags. All other kinds
5831 * of error codes, including those from the user provided f_vrfy
5832 * functions, are treated as fatal and lead to a failure of
5833 * ssl_parse_certificate even if verification was optional. */
5834 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5835 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5836 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5837 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005838 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005839 }
5840
5841 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5842 {
5843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5844 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5845 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005846
5847 if( ret != 0 )
5848 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005849 uint8_t alert;
5850
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005851 /* The certificate may have been rejected for several reasons.
5852 Pick one and send the corresponding alert. Which alert to send
5853 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005854 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5855 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5856 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5857 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5858 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5859 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5860 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5861 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5862 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5863 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5864 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5865 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5866 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5867 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5868 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5869 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5870 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5871 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5872 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5873 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005874 else
5875 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005876 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5877 alert );
5878 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005879
Hanno Beckere6706e62017-05-15 16:05:15 +01005880#if defined(MBEDTLS_DEBUG_C)
5881 if( ssl->session_negotiate->verify_result != 0 )
5882 {
5883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5884 ssl->session_negotiate->verify_result ) );
5885 }
5886 else
5887 {
5888 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5889 }
5890#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 }
5892
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005893 ssl->state++;
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896
5897 return( ret );
5898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5900 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5901 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5902 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5903 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5904 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5905 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005908{
5909 int ret;
5910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005914 ssl->out_msglen = 1;
5915 ssl->out_msg[0] = 1;
5916
Paul Bakker5121ce52009-01-03 21:22:43 +00005917 ssl->state++;
5918
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005919 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005920 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 return( ret );
5923 }
5924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926
5927 return( 0 );
5928}
5929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005931{
5932 int ret;
5933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005935
Hanno Becker327c93b2018-08-15 13:56:18 +01005936 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939 return( ret );
5940 }
5941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005945 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5946 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005948 }
5949
Hanno Beckere678eaa2018-08-21 14:57:46 +01005950 /* CCS records are only accepted if they have length 1 and content '1',
5951 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005952
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005953 /*
5954 * Switch to our negotiated transform and session parameters for inbound
5955 * data.
5956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005958 ssl->transform_in = ssl->transform_negotiate;
5959 ssl->session_in = ssl->session_negotiate;
5960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005965 ssl_dtls_replay_reset( ssl );
5966#endif
5967
5968 /* Increment epoch */
5969 if( ++ssl->in_epoch == 0 )
5970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005972 /* This is highly unlikely to happen for legitimate reasons, so
5973 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005975 }
5976 }
5977 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005979 memset( ssl->in_ctr, 0, 8 );
5980
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005981 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5984 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005989 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5990 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005992 }
5993 }
5994#endif
5995
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 ssl->state++;
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005999
6000 return( 0 );
6001}
6002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6004 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006005{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006006 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6009 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6010 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006011 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006012 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6015#if defined(MBEDTLS_SHA512_C)
6016 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006017 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6018 else
6019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020#if defined(MBEDTLS_SHA256_C)
6021 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006022 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006023 else
6024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006028 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006029 }
Paul Bakker380da532012-04-18 16:10:25 +00006030}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6035 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006036 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6037 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6040#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006041 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006044 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006047}
6048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006050 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6053 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006054 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6055 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6058#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006059 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006060#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006062 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006065}
6066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6068 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6069static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006070 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006071{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006072 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6073 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006074}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006075#endif
Paul Bakker380da532012-04-18 16:10:25 +00006076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6078#if defined(MBEDTLS_SHA256_C)
6079static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006080 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006081{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006082 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006083}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006084#endif
Paul Bakker380da532012-04-18 16:10:25 +00006085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086#if defined(MBEDTLS_SHA512_C)
6087static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006088 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006089{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006090 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006091}
Paul Bakker769075d2012-11-24 11:26:46 +01006092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006096static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006098{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006099 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006100 mbedtls_md5_context md5;
6101 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006102
Paul Bakker5121ce52009-01-03 21:22:43 +00006103 unsigned char padbuf[48];
6104 unsigned char md5sum[16];
6105 unsigned char sha1sum[20];
6106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006108 if( !session )
6109 session = ssl->session;
6110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006112
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006113 mbedtls_md5_init( &md5 );
6114 mbedtls_sha1_init( &sha1 );
6115
6116 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6117 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006118
6119 /*
6120 * SSLv3:
6121 * hash =
6122 * MD5( master + pad2 +
6123 * MD5( handshake + sender + master + pad1 ) )
6124 * + SHA1( master + pad2 +
6125 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 */
6127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006129 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6130 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006134 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6135 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006139 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006140
Paul Bakker1ef83d62012-04-11 12:09:53 +00006141 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006142
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006143 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6144 mbedtls_md5_update_ret( &md5, session->master, 48 );
6145 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6146 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006148 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6149 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6150 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6151 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006152
Paul Bakker1ef83d62012-04-11 12:09:53 +00006153 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006155 mbedtls_md5_starts_ret( &md5 );
6156 mbedtls_md5_update_ret( &md5, session->master, 48 );
6157 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6158 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6159 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006160
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006161 mbedtls_sha1_starts_ret( &sha1 );
6162 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6163 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6164 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6165 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006169 mbedtls_md5_free( &md5 );
6170 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006172 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6173 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6174 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006181static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006183{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006184 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006185 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006186 mbedtls_md5_context md5;
6187 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006188 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006191 if( !session )
6192 session = ssl->session;
6193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006196 mbedtls_md5_init( &md5 );
6197 mbedtls_sha1_init( &sha1 );
6198
6199 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6200 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006201
Paul Bakker1ef83d62012-04-11 12:09:53 +00006202 /*
6203 * TLSv1:
6204 * hash = PRF( master, finished_label,
6205 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6206 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006209 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6210 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006211#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006214 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6215 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006216#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006219 ? "client finished"
6220 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006221
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006222 mbedtls_md5_finish_ret( &md5, padbuf );
6223 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006224
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006225 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006226 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006229
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006230 mbedtls_md5_free( &md5 );
6231 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006232
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006233 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6240#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006241static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006243{
6244 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006245 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006246 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006247 unsigned char padbuf[32];
6248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006250 if( !session )
6251 session = ssl->session;
6252
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006253 mbedtls_sha256_init( &sha256 );
6254
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006256
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006257 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006258
6259 /*
6260 * TLSv1.2:
6261 * hash = PRF( master, finished_label,
6262 * Hash( handshake ) )[0.11]
6263 */
6264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265#if !defined(MBEDTLS_SHA256_ALT)
6266 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006267 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006268#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006271 ? "client finished"
6272 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006273
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006274 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006275
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006276 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006277 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006280
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006281 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006282
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006283 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006286}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006290static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006292{
6293 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006294 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006295 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006296 unsigned char padbuf[48];
6297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006299 if( !session )
6300 session = ssl->session;
6301
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006302 mbedtls_sha512_init( &sha512 );
6303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006305
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006306 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006307
6308 /*
6309 * TLSv1.2:
6310 * hash = PRF( master, finished_label,
6311 * Hash( handshake ) )[0.11]
6312 */
6313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006315 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6316 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006317#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006320 ? "client finished"
6321 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006322
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006323 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006324
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006325 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006326 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006329
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006330 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006331
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006332 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#endif /* MBEDTLS_SHA512_C */
6337#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006342
6343 /*
6344 * Free our handshake params
6345 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006346 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006348 ssl->handshake = NULL;
6349
6350 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006351 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006352 */
6353 if( ssl->transform )
6354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 mbedtls_ssl_transform_free( ssl->transform );
6356 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006357 }
6358 ssl->transform = ssl->transform_negotiate;
6359 ssl->transform_negotiate = NULL;
6360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006362}
6363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006365{
6366 int resume = ssl->handshake->resume;
6367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370#if defined(MBEDTLS_SSL_RENEGOTIATION)
6371 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006374 ssl->renego_records_seen = 0;
6375 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006376#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006377
6378 /*
6379 * Free the previous session and switch in the current one
6380 */
Paul Bakker0a597072012-09-25 21:55:46 +00006381 if( ssl->session )
6382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006384 /* RFC 7366 3.1: keep the EtM state */
6385 ssl->session_negotiate->encrypt_then_mac =
6386 ssl->session->encrypt_then_mac;
6387#endif
6388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 mbedtls_ssl_session_free( ssl->session );
6390 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006391 }
6392 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006393 ssl->session_negotiate = NULL;
6394
Paul Bakker0a597072012-09-25 21:55:46 +00006395 /*
6396 * Add cache entry
6397 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006398 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006399 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006400 resume == 0 )
6401 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006402 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006404 }
Paul Bakker0a597072012-09-25 21:55:46 +00006405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006408 ssl->handshake->flight != NULL )
6409 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006410 /* Cancel handshake timer */
6411 ssl_set_timer( ssl, 0 );
6412
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006413 /* Keep last flight around in case we need to resend it:
6414 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006416 }
6417 else
6418#endif
6419 ssl_handshake_wrapup_free_hs_transform( ssl );
6420
Paul Bakker48916f92012-09-16 19:57:18 +00006421 ssl->state++;
6422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006424}
6425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006427{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006428 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006431
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006432 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006433
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006434 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006435
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006436 /*
6437 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6438 * may define some other value. Currently (early 2016), no defined
6439 * ciphersuite does this (and this is unlikely to change as activity has
6440 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006445 ssl->verify_data_len = hash_len;
6446 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006447#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006448
Paul Bakker5121ce52009-01-03 21:22:43 +00006449 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6451 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006452
6453 /*
6454 * In case of session resuming, invert the client and server
6455 * ChangeCipherSpec messages order.
6456 */
Paul Bakker0a597072012-09-25 21:55:46 +00006457 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006460 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006462#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006464 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006466#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006467 }
6468 else
6469 ssl->state++;
6470
Paul Bakker48916f92012-09-16 19:57:18 +00006471 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006472 * Switch to our negotiated transform and session parameters for outbound
6473 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006479 {
6480 unsigned char i;
6481
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006482 /* Remember current epoch settings for resending */
6483 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006484 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006485
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006486 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006487 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006488
6489 /* Increment epoch */
6490 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006491 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006492 break;
6493
6494 /* The loop goes to its end iff the counter is wrapping */
6495 if( i == 0 )
6496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6498 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006499 }
6500 }
6501 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006503 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006504
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006505 ssl->transform_out = ssl->transform_negotiate;
6506 ssl->session_out = ssl->session_negotiate;
6507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6509 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6514 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006515 }
6516 }
6517#endif
6518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006522#endif
6523
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006524 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006525 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006527 return( ret );
6528 }
6529
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006530#if defined(MBEDTLS_SSL_PROTO_DTLS)
6531 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6532 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6533 {
6534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6535 return( ret );
6536 }
6537#endif
6538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006540
6541 return( 0 );
6542}
6543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006545#define SSL_MAX_HASH_LEN 36
6546#else
6547#define SSL_MAX_HASH_LEN 12
6548#endif
6549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006551{
Paul Bakker23986e52011-04-24 08:57:21 +00006552 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006553 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006554 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006557
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006558 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006559
Hanno Becker327c93b2018-08-15 13:56:18 +01006560 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006563 return( ret );
6564 }
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006569 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6570 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006572 }
6573
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006574 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575#if defined(MBEDTLS_SSL_PROTO_SSL3)
6576 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006577 hash_len = 36;
6578 else
6579#endif
6580 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6583 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006586 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6587 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 }
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006592 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006595 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6596 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 }
6599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006601 ssl->verify_data_len = hash_len;
6602 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006603#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006604
Paul Bakker0a597072012-09-25 21:55:46 +00006605 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006614#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006615 }
6616 else
6617 ssl->state++;
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006622#endif
6623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006625
6626 return( 0 );
6627}
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6634 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6635 mbedtls_md5_init( &handshake->fin_md5 );
6636 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006637 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6638 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006639#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6641#if defined(MBEDTLS_SHA256_C)
6642 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006643 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006644#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645#if defined(MBEDTLS_SHA512_C)
6646 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006647 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006648#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006650
6651 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006652
6653#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6654 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6655 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6656#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658#if defined(MBEDTLS_DHM_C)
6659 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006660#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661#if defined(MBEDTLS_ECDH_C)
6662 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006663#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006664#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006665 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006666#if defined(MBEDTLS_SSL_CLI_C)
6667 handshake->ecjpake_cache = NULL;
6668 handshake->ecjpake_cache_len = 0;
6669#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006670#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006671
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006672#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006673 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006674#endif
6675
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006676#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6677 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6678#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006679}
6680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6686 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 mbedtls_md_init( &transform->md_ctx_enc );
6689 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006690}
6691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006695}
6696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006698{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006699 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006700 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006702 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006704 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006705 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006706
6707 /*
6708 * Either the pointers are now NULL or cleared properly and can be freed.
6709 * Now allocate missing structures.
6710 */
6711 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006712 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006713 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006714 }
Paul Bakker48916f92012-09-16 19:57:18 +00006715
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006716 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006717 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006718 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006719 }
Paul Bakker48916f92012-09-16 19:57:18 +00006720
Paul Bakker82788fb2014-10-20 13:59:19 +02006721 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006722 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006723 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006724 }
Paul Bakker48916f92012-09-16 19:57:18 +00006725
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006726 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006727 if( ssl->handshake == NULL ||
6728 ssl->transform_negotiate == NULL ||
6729 ssl->session_negotiate == NULL )
6730 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 mbedtls_free( ssl->handshake );
6734 mbedtls_free( ssl->transform_negotiate );
6735 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006736
6737 ssl->handshake = NULL;
6738 ssl->transform_negotiate = NULL;
6739 ssl->session_negotiate = NULL;
6740
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006741 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006742 }
6743
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006744 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006746 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006747 ssl_handshake_params_init( ssl->handshake );
6748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6751 {
6752 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006753
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006754 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6755 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6756 else
6757 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006758
6759 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006760 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006761#endif
6762
Paul Bakker48916f92012-09-16 19:57:18 +00006763 return( 0 );
6764}
6765
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006766#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006767/* Dummy cookie callbacks for defaults */
6768static int ssl_cookie_write_dummy( void *ctx,
6769 unsigned char **p, unsigned char *end,
6770 const unsigned char *cli_id, size_t cli_id_len )
6771{
6772 ((void) ctx);
6773 ((void) p);
6774 ((void) end);
6775 ((void) cli_id);
6776 ((void) cli_id_len);
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006779}
6780
6781static int ssl_cookie_check_dummy( void *ctx,
6782 const unsigned char *cookie, size_t cookie_len,
6783 const unsigned char *cli_id, size_t cli_id_len )
6784{
6785 ((void) ctx);
6786 ((void) cookie);
6787 ((void) cookie_len);
6788 ((void) cli_id);
6789 ((void) cli_id_len);
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006792}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006793#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006794
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006795/* Once ssl->out_hdr as the address of the beginning of the
6796 * next outgoing record is set, deduce the other pointers.
6797 *
6798 * Note: For TLS, we save the implicit record sequence number
6799 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6800 * and the caller has to make sure there's space for this.
6801 */
6802
6803static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6804 mbedtls_ssl_transform *transform )
6805{
6806#if defined(MBEDTLS_SSL_PROTO_DTLS)
6807 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6808 {
6809 ssl->out_ctr = ssl->out_hdr + 3;
6810 ssl->out_len = ssl->out_hdr + 11;
6811 ssl->out_iv = ssl->out_hdr + 13;
6812 }
6813 else
6814#endif
6815 {
6816 ssl->out_ctr = ssl->out_hdr - 8;
6817 ssl->out_len = ssl->out_hdr + 3;
6818 ssl->out_iv = ssl->out_hdr + 5;
6819 }
6820
6821 /* Adjust out_msg to make space for explicit IV, if used. */
6822 if( transform != NULL &&
6823 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6824 {
6825 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6826 }
6827 else
6828 ssl->out_msg = ssl->out_iv;
6829}
6830
6831/* Once ssl->in_hdr as the address of the beginning of the
6832 * next incoming record is set, deduce the other pointers.
6833 *
6834 * Note: For TLS, we save the implicit record sequence number
6835 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6836 * and the caller has to make sure there's space for this.
6837 */
6838
6839static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6840 mbedtls_ssl_transform *transform )
6841{
6842#if defined(MBEDTLS_SSL_PROTO_DTLS)
6843 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6844 {
6845 ssl->in_ctr = ssl->in_hdr + 3;
6846 ssl->in_len = ssl->in_hdr + 11;
6847 ssl->in_iv = ssl->in_hdr + 13;
6848 }
6849 else
6850#endif
6851 {
6852 ssl->in_ctr = ssl->in_hdr - 8;
6853 ssl->in_len = ssl->in_hdr + 3;
6854 ssl->in_iv = ssl->in_hdr + 5;
6855 }
6856
6857 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6858 if( transform != NULL &&
6859 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6860 {
6861 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6862 }
6863 else
6864 ssl->in_msg = ssl->in_iv;
6865}
6866
Paul Bakker5121ce52009-01-03 21:22:43 +00006867/*
6868 * Initialize an SSL context
6869 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006870void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6871{
6872 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6873}
6874
6875/*
6876 * Setup an SSL context
6877 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006878
6879static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6880{
6881 /* Set the incoming and outgoing record pointers. */
6882#if defined(MBEDTLS_SSL_PROTO_DTLS)
6883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6884 {
6885 ssl->out_hdr = ssl->out_buf;
6886 ssl->in_hdr = ssl->in_buf;
6887 }
6888 else
6889#endif /* MBEDTLS_SSL_PROTO_DTLS */
6890 {
6891 ssl->out_hdr = ssl->out_buf + 8;
6892 ssl->in_hdr = ssl->in_buf + 8;
6893 }
6894
6895 /* Derive other internal pointers. */
6896 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6897 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6898}
6899
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006900int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006901 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006902{
Paul Bakker48916f92012-09-16 19:57:18 +00006903 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006904
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006905 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006906
6907 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006908 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006909 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006910
6911 /* Set to NULL in case of an error condition */
6912 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006913
Angus Grattond8213d02016-05-25 20:56:48 +10006914 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6915 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006916 {
Angus Grattond8213d02016-05-25 20:56:48 +10006917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006918 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006919 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006920 }
6921
6922 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6923 if( ssl->out_buf == NULL )
6924 {
6925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006926 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006927 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006928 }
6929
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006930 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006931
Paul Bakker48916f92012-09-16 19:57:18 +00006932 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006933 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006934
6935 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006936
6937error:
6938 mbedtls_free( ssl->in_buf );
6939 mbedtls_free( ssl->out_buf );
6940
6941 ssl->conf = NULL;
6942
6943 ssl->in_buf = NULL;
6944 ssl->out_buf = NULL;
6945
6946 ssl->in_hdr = NULL;
6947 ssl->in_ctr = NULL;
6948 ssl->in_len = NULL;
6949 ssl->in_iv = NULL;
6950 ssl->in_msg = NULL;
6951
6952 ssl->out_hdr = NULL;
6953 ssl->out_ctr = NULL;
6954 ssl->out_len = NULL;
6955 ssl->out_iv = NULL;
6956 ssl->out_msg = NULL;
6957
k-stachowiak9f7798e2018-07-31 16:52:32 +02006958 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006959}
6960
6961/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006962 * Reset an initialized and used SSL context for re-use while retaining
6963 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006964 *
6965 * If partial is non-zero, keep data in the input buffer and client ID.
6966 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006967 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006968static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006969{
Paul Bakker48916f92012-09-16 19:57:18 +00006970 int ret;
6971
Hanno Becker7e772132018-08-10 12:38:21 +01006972#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6973 !defined(MBEDTLS_SSL_SRV_C)
6974 ((void) partial);
6975#endif
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006978
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006979 /* Cancel any possibly running timer */
6980 ssl_set_timer( ssl, 0 );
6981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_SSL_RENEGOTIATION)
6983 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006984 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006985
6986 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6988 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006989#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006991
Paul Bakker7eb013f2011-10-06 12:37:39 +00006992 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006993 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006994
6995 ssl->in_msgtype = 0;
6996 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006998 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006999 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007000#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007002 ssl_dtls_replay_reset( ssl );
7003#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007004
7005 ssl->in_hslen = 0;
7006 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007007
7008 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007009
7010 ssl->out_msgtype = 0;
7011 ssl->out_msglen = 0;
7012 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7014 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007015 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007016#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007017
Hanno Becker19859472018-08-06 09:40:20 +01007018 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7019
Paul Bakker48916f92012-09-16 19:57:18 +00007020 ssl->transform_in = NULL;
7021 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007022
Hanno Becker78640902018-08-13 16:35:15 +01007023 ssl->session_in = NULL;
7024 ssl->session_out = NULL;
7025
Angus Grattond8213d02016-05-25 20:56:48 +10007026 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007027
7028#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007029 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007030#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7031 {
7032 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007033 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007034 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7037 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7040 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7043 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007044 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007045 }
7046#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007047
Paul Bakker48916f92012-09-16 19:57:18 +00007048 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 mbedtls_ssl_transform_free( ssl->transform );
7051 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007052 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007053 }
Paul Bakker48916f92012-09-16 19:57:18 +00007054
Paul Bakkerc0463502013-02-14 11:19:38 +01007055 if( ssl->session )
7056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 mbedtls_ssl_session_free( ssl->session );
7058 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007059 ssl->session = NULL;
7060 }
7061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007063 ssl->alpn_chosen = NULL;
7064#endif
7065
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007066#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007067#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007068 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007069#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007070 {
7071 mbedtls_free( ssl->cli_id );
7072 ssl->cli_id = NULL;
7073 ssl->cli_id_len = 0;
7074 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007075#endif
7076
Paul Bakker48916f92012-09-16 19:57:18 +00007077 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7078 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007079
7080 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007081}
7082
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007083/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007084 * Reset an initialized and used SSL context for re-use while retaining
7085 * all application-set variables, function pointers and data.
7086 */
7087int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7088{
7089 return( ssl_session_reset_int( ssl, 0 ) );
7090}
7091
7092/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007093 * SSL set accessors
7094 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007095void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007096{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007097 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007098}
7099
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007100void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007101{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007102 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007103}
7104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007106void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007108 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007109}
7110#endif
7111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007113void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007114{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007115 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007116}
7117#endif
7118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007120
Hanno Becker1841b0a2018-08-24 11:13:57 +01007121void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7122 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007123{
7124 ssl->disable_datagram_packing = !allow_packing;
7125}
7126
7127void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7128 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007130 conf->hs_timeout_min = min;
7131 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007132}
7133#endif
7134
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007135void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007136{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007137 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007138}
7139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007141void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007142 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007143 void *p_vrfy )
7144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007145 conf->f_vrfy = f_vrfy;
7146 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007149
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007150void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007151 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007152 void *p_rng )
7153{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007154 conf->f_rng = f_rng;
7155 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007156}
7157
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007158void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007159 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007160 void *p_dbg )
7161{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007162 conf->f_dbg = f_dbg;
7163 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007164}
7165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007167 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007168 mbedtls_ssl_send_t *f_send,
7169 mbedtls_ssl_recv_t *f_recv,
7170 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007171{
7172 ssl->p_bio = p_bio;
7173 ssl->f_send = f_send;
7174 ssl->f_recv = f_recv;
7175 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007176}
7177
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007178#if defined(MBEDTLS_SSL_PROTO_DTLS)
7179void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7180{
7181 ssl->mtu = mtu;
7182}
7183#endif
7184
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007185void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007186{
7187 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007188}
7189
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007190void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7191 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007192 mbedtls_ssl_set_timer_t *f_set_timer,
7193 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007194{
7195 ssl->p_timer = p_timer;
7196 ssl->f_set_timer = f_set_timer;
7197 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007198
7199 /* Make sure we start with no timer running */
7200 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007201}
7202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007204void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007205 void *p_cache,
7206 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7207 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007208{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007209 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007210 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007211 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_SSL_CLI_C)
7216int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007217{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007218 int ret;
7219
7220 if( ssl == NULL ||
7221 session == NULL ||
7222 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007223 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007226 }
7227
7228 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7229 return( ret );
7230
Paul Bakker0a597072012-09-25 21:55:46 +00007231 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007232
7233 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007236
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007237void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007238 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007240 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7241 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7242 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7243 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007244}
7245
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007246void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007247 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007248 int major, int minor )
7249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007251 return;
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007254 return;
7255
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007256 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007257}
7258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007260void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007261 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007262{
7263 conf->cert_profile = profile;
7264}
7265
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007266/* Append a new keycert entry to a (possibly empty) list */
7267static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7268 mbedtls_x509_crt *cert,
7269 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007270{
niisato8ee24222018-06-25 19:05:48 +09007271 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007272
niisato8ee24222018-06-25 19:05:48 +09007273 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7274 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007275 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007276
niisato8ee24222018-06-25 19:05:48 +09007277 new_cert->cert = cert;
7278 new_cert->key = key;
7279 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007280
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007281 /* Update head is the list was null, else add to the end */
7282 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007283 {
niisato8ee24222018-06-25 19:05:48 +09007284 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007285 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007286 else
7287 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007288 mbedtls_ssl_key_cert *cur = *head;
7289 while( cur->next != NULL )
7290 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007291 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007292 }
7293
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007294 return( 0 );
7295}
7296
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007297int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007298 mbedtls_x509_crt *own_cert,
7299 mbedtls_pk_context *pk_key )
7300{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007301 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007302}
7303
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007304void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007305 mbedtls_x509_crt *ca_chain,
7306 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007307{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007308 conf->ca_chain = ca_chain;
7309 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007312
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007313#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7314int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7315 mbedtls_x509_crt *own_cert,
7316 mbedtls_pk_context *pk_key )
7317{
7318 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7319 own_cert, pk_key ) );
7320}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007321
7322void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7323 mbedtls_x509_crt *ca_chain,
7324 mbedtls_x509_crl *ca_crl )
7325{
7326 ssl->handshake->sni_ca_chain = ca_chain;
7327 ssl->handshake->sni_ca_crl = ca_crl;
7328}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007329
7330void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7331 int authmode )
7332{
7333 ssl->handshake->sni_authmode = authmode;
7334}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007335#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7336
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007337#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007338/*
7339 * Set EC J-PAKE password for current handshake
7340 */
7341int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7342 const unsigned char *pw,
7343 size_t pw_len )
7344{
7345 mbedtls_ecjpake_role role;
7346
Janos Follath8eb64132016-06-03 15:40:57 +01007347 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7349
7350 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7351 role = MBEDTLS_ECJPAKE_SERVER;
7352 else
7353 role = MBEDTLS_ECJPAKE_CLIENT;
7354
7355 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7356 role,
7357 MBEDTLS_MD_SHA256,
7358 MBEDTLS_ECP_DP_SECP256R1,
7359 pw, pw_len ) );
7360}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007361#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007364int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007365 const unsigned char *psk, size_t psk_len,
7366 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007367{
Paul Bakker6db455e2013-09-18 17:29:31 +02007368 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007373
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007374 /* Identity len will be encoded on two bytes */
7375 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007376 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007377 {
7378 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7379 }
7380
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007381 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007382 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007383 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007384
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007385 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007386 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007387 conf->psk_len = 0;
7388 }
7389 if( conf->psk_identity != NULL )
7390 {
7391 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007392 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007393 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007394 }
7395
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007396 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7397 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007398 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007399 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007400 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007401 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007402 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007404 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007405
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007406 conf->psk_len = psk_len;
7407 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007408
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007409 memcpy( conf->psk, psk, conf->psk_len );
7410 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007411
7412 return( 0 );
7413}
7414
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007415int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7416 const unsigned char *psk, size_t psk_len )
7417{
7418 if( psk == NULL || ssl->handshake == NULL )
7419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7420
7421 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7423
7424 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007425 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007426 mbedtls_platform_zeroize( ssl->handshake->psk,
7427 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007428 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007429 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007430 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007431
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007432 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007433 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007434
7435 ssl->handshake->psk_len = psk_len;
7436 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7437
7438 return( 0 );
7439}
7440
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007441void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007443 size_t),
7444 void *p_psk )
7445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007446 conf->f_psk = f_psk;
7447 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007450
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007451#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007452
7453#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007454int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007455{
7456 int ret;
7457
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007458 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7459 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7460 {
7461 mbedtls_mpi_free( &conf->dhm_P );
7462 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007463 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007465
7466 return( 0 );
7467}
Hanno Becker470a8c42017-10-04 15:28:46 +01007468#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007469
Hanno Beckera90658f2017-10-04 15:29:08 +01007470int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7471 const unsigned char *dhm_P, size_t P_len,
7472 const unsigned char *dhm_G, size_t G_len )
7473{
7474 int ret;
7475
7476 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7477 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7478 {
7479 mbedtls_mpi_free( &conf->dhm_P );
7480 mbedtls_mpi_free( &conf->dhm_G );
7481 return( ret );
7482 }
7483
7484 return( 0 );
7485}
Paul Bakker5121ce52009-01-03 21:22:43 +00007486
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007487int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007488{
7489 int ret;
7490
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007491 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7492 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7493 {
7494 mbedtls_mpi_free( &conf->dhm_P );
7495 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007496 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007497 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007498
7499 return( 0 );
7500}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007501#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007502
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007503#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7504/*
7505 * Set the minimum length for Diffie-Hellman parameters
7506 */
7507void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7508 unsigned int bitlen )
7509{
7510 conf->dhm_min_bitlen = bitlen;
7511}
7512#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7513
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007514#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007515/*
7516 * Set allowed/preferred hashes for handshake signatures
7517 */
7518void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7519 const int *hashes )
7520{
7521 conf->sig_hashes = hashes;
7522}
Hanno Becker947194e2017-04-07 13:25:49 +01007523#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007524
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007525#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007526/*
7527 * Set the allowed elliptic curves
7528 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007529void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007530 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007532 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007533}
Hanno Becker947194e2017-04-07 13:25:49 +01007534#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007535
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007536#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007538{
Hanno Becker947194e2017-04-07 13:25:49 +01007539 /* Initialize to suppress unnecessary compiler warning */
7540 size_t hostname_len = 0;
7541
7542 /* Check if new hostname is valid before
7543 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007544 if( hostname != NULL )
7545 {
7546 hostname_len = strlen( hostname );
7547
7548 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7550 }
7551
7552 /* Now it's clear that we will overwrite the old hostname,
7553 * so we can free it safely */
7554
7555 if( ssl->hostname != NULL )
7556 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007557 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007558 mbedtls_free( ssl->hostname );
7559 }
7560
7561 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007562
Paul Bakker5121ce52009-01-03 21:22:43 +00007563 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007564 {
7565 ssl->hostname = NULL;
7566 }
7567 else
7568 {
7569 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007570 if( ssl->hostname == NULL )
7571 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007572
Hanno Becker947194e2017-04-07 13:25:49 +01007573 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007574
Hanno Becker947194e2017-04-07 13:25:49 +01007575 ssl->hostname[hostname_len] = '\0';
7576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007577
7578 return( 0 );
7579}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007580#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007581
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007582#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007583void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007585 const unsigned char *, size_t),
7586 void *p_sni )
7587{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007588 conf->f_sni = f_sni;
7589 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007594int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007595{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007596 size_t cur_len, tot_len;
7597 const char **p;
7598
7599 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007600 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7601 * MUST NOT be truncated."
7602 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007603 */
7604 tot_len = 0;
7605 for( p = protos; *p != NULL; p++ )
7606 {
7607 cur_len = strlen( *p );
7608 tot_len += cur_len;
7609
7610 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007612 }
7613
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007614 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007615
7616 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007617}
7618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007620{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007621 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007622}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007624
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007625void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007626{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007627 conf->max_major_ver = major;
7628 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007629}
7630
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007631void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007633 conf->min_major_ver = major;
7634 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007635}
7636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007638void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007639{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007640 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007641}
7642#endif
7643
Janos Follath088ce432017-04-10 12:42:31 +01007644#if defined(MBEDTLS_SSL_SRV_C)
7645void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7646 char cert_req_ca_list )
7647{
7648 conf->cert_req_ca_list = cert_req_ca_list;
7649}
7650#endif
7651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007653void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007655 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007656}
7657#endif
7658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007660void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007662 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007663}
7664#endif
7665
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007666#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007667void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007669 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007670}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007671#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007674int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007677 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007680 }
7681
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007682 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007683
7684 return( 0 );
7685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007689void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007690{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007691 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007696void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007697{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007698 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007699}
7700#endif
7701
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007702void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007704 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007705}
7706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007708void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007709{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007710 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007711}
7712
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007713void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007714{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007715 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007716}
7717
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007718void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007719 const unsigned char period[8] )
7720{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007721 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007726#if defined(MBEDTLS_SSL_CLI_C)
7727void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007728{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007729 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007730}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007731#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007732
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007733#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007734void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7735 mbedtls_ssl_ticket_write_t *f_ticket_write,
7736 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7737 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007738{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007739 conf->f_ticket_write = f_ticket_write;
7740 conf->f_ticket_parse = f_ticket_parse;
7741 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007742}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007743#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007745
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007746#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7747void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7748 mbedtls_ssl_export_keys_t *f_export_keys,
7749 void *p_export_keys )
7750{
7751 conf->f_export_keys = f_export_keys;
7752 conf->p_export_keys = p_export_keys;
7753}
7754#endif
7755
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007756#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007757void mbedtls_ssl_conf_async_private_cb(
7758 mbedtls_ssl_config *conf,
7759 mbedtls_ssl_async_sign_t *f_async_sign,
7760 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7761 mbedtls_ssl_async_resume_t *f_async_resume,
7762 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007763 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007764{
7765 conf->f_async_sign_start = f_async_sign;
7766 conf->f_async_decrypt_start = f_async_decrypt;
7767 conf->f_async_resume = f_async_resume;
7768 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007769 conf->p_async_config_data = async_config_data;
7770}
7771
Gilles Peskine8f97af72018-04-26 11:46:10 +02007772void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7773{
7774 return( conf->p_async_config_data );
7775}
7776
Gilles Peskine1febfef2018-04-30 11:54:39 +02007777void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007778{
7779 if( ssl->handshake == NULL )
7780 return( NULL );
7781 else
7782 return( ssl->handshake->user_async_ctx );
7783}
7784
Gilles Peskine1febfef2018-04-30 11:54:39 +02007785void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007786 void *ctx )
7787{
7788 if( ssl->handshake != NULL )
7789 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007790}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007791#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007792
Paul Bakker5121ce52009-01-03 21:22:43 +00007793/*
7794 * SSL get accessors
7795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007797{
7798 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7799}
7800
Hanno Becker8b170a02017-10-10 11:51:19 +01007801int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7802{
7803 /*
7804 * Case A: We're currently holding back
7805 * a message for further processing.
7806 */
7807
7808 if( ssl->keep_current_message == 1 )
7809 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007811 return( 1 );
7812 }
7813
7814 /*
7815 * Case B: Further records are pending in the current datagram.
7816 */
7817
7818#if defined(MBEDTLS_SSL_PROTO_DTLS)
7819 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7820 ssl->in_left > ssl->next_record_offset )
7821 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007823 return( 1 );
7824 }
7825#endif /* MBEDTLS_SSL_PROTO_DTLS */
7826
7827 /*
7828 * Case C: A handshake message is being processed.
7829 */
7830
Hanno Becker8b170a02017-10-10 11:51:19 +01007831 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7832 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007834 return( 1 );
7835 }
7836
7837 /*
7838 * Case D: An application data message is being processed
7839 */
7840 if( ssl->in_offt != NULL )
7841 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007843 return( 1 );
7844 }
7845
7846 /*
7847 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007848 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007849 * we implement support for multiple alerts in single records.
7850 */
7851
7852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7853 return( 0 );
7854}
7855
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007856uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007857{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007858 if( ssl->session != NULL )
7859 return( ssl->session->verify_result );
7860
7861 if( ssl->session_negotiate != NULL )
7862 return( ssl->session_negotiate->verify_result );
7863
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007864 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007865}
7866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007868{
Paul Bakker926c8e42013-03-06 10:23:34 +01007869 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007870 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007873}
7874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007878 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007879 {
7880 switch( ssl->minor_ver )
7881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007883 return( "DTLSv1.0" );
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007886 return( "DTLSv1.2" );
7887
7888 default:
7889 return( "unknown (DTLS)" );
7890 }
7891 }
7892#endif
7893
Paul Bakker43ca69c2011-01-15 17:35:19 +00007894 switch( ssl->minor_ver )
7895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007897 return( "SSLv3.0" );
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007900 return( "TLSv1.0" );
7901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007903 return( "TLSv1.1" );
7904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007906 return( "TLSv1.2" );
7907
Paul Bakker43ca69c2011-01-15 17:35:19 +00007908 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007909 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007910 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007911}
7912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007914{
Hanno Becker3136ede2018-08-17 15:28:19 +01007915 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007917 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007918
Hanno Becker78640902018-08-13 16:35:15 +01007919 if( transform == NULL )
7920 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922#if defined(MBEDTLS_ZLIB_SUPPORT)
7923 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7924 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007925#endif
7926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 case MBEDTLS_MODE_GCM:
7930 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007931 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007933 transform_expansion = transform->minlen;
7934 break;
7935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007937
7938 block_size = mbedtls_cipher_get_block_size(
7939 &transform->cipher_ctx_enc );
7940
Hanno Becker3136ede2018-08-17 15:28:19 +01007941 /* Expansion due to the addition of the MAC. */
7942 transform_expansion += transform->maclen;
7943
7944 /* Expansion due to the addition of CBC padding;
7945 * Theoretically up to 256 bytes, but we never use
7946 * more than the block size of the underlying cipher. */
7947 transform_expansion += block_size;
7948
7949 /* For TLS 1.1 or higher, an explicit IV is added
7950 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007951#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7952 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007953 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007954#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007955
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007956 break;
7957
7958 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007961 }
7962
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007963 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007964}
7965
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007966#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7967size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7968{
7969 size_t max_len;
7970
7971 /*
7972 * Assume mfl_code is correct since it was checked when set
7973 */
Angus Grattond8213d02016-05-25 20:56:48 +10007974 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007975
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007976 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007977 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007978 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007979 {
Angus Grattond8213d02016-05-25 20:56:48 +10007980 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007981 }
7982
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007983 /* During a handshake, use the value being negotiated */
7984 if( ssl->session_negotiate != NULL &&
7985 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7986 {
7987 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7988 }
7989
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007990 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007991}
7992#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7993
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007994#if defined(MBEDTLS_SSL_PROTO_DTLS)
7995static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7996{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04007997 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
7998 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
7999 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8000 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8001 return ( 0 );
8002
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008003 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8004 return( ssl->mtu );
8005
8006 if( ssl->mtu == 0 )
8007 return( ssl->handshake->mtu );
8008
8009 return( ssl->mtu < ssl->handshake->mtu ?
8010 ssl->mtu : ssl->handshake->mtu );
8011}
8012#endif /* MBEDTLS_SSL_PROTO_DTLS */
8013
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008014int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8015{
8016 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8017
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008018#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8019 !defined(MBEDTLS_SSL_PROTO_DTLS)
8020 (void) ssl;
8021#endif
8022
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008023#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8024 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8025
8026 if( max_len > mfl )
8027 max_len = mfl;
8028#endif
8029
8030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008031 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008032 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008033 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008034 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8035 const size_t overhead = (size_t) ret;
8036
8037 if( ret < 0 )
8038 return( ret );
8039
8040 if( mtu <= overhead )
8041 {
8042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8043 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8044 }
8045
8046 if( max_len > mtu - overhead )
8047 max_len = mtu - overhead;
8048 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008049#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008050
Hanno Becker0defedb2018-08-10 12:35:02 +01008051#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8052 !defined(MBEDTLS_SSL_PROTO_DTLS)
8053 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008054#endif
8055
8056 return( (int) max_len );
8057}
8058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059#if defined(MBEDTLS_X509_CRT_PARSE_C)
8060const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008061{
8062 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008063 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008064
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008065 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069#if defined(MBEDTLS_SSL_CLI_C)
8070int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008071{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008072 if( ssl == NULL ||
8073 dst == NULL ||
8074 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008075 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008078 }
8079
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008080 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008083
Paul Bakker5121ce52009-01-03 21:22:43 +00008084/*
Paul Bakker1961b702013-01-25 14:49:24 +01008085 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008090
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008091 if( ssl == NULL || ssl->conf == NULL )
8092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008094#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008095 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008099 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008101#endif
8102
Paul Bakker1961b702013-01-25 14:49:24 +01008103 return( ret );
8104}
8105
8106/*
8107 * Perform the SSL handshake
8108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008110{
8111 int ret = 0;
8112
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008113 if( ssl == NULL || ssl->conf == NULL )
8114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008121
8122 if( ret != 0 )
8123 break;
8124 }
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008127
8128 return( ret );
8129}
8130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#if defined(MBEDTLS_SSL_RENEGOTIATION)
8132#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008133/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008134 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008137{
8138 int ret;
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008141
8142 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8144 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008145
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008146 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008147 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008149 return( ret );
8150 }
8151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008153
8154 return( 0 );
8155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008157
8158/*
8159 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 * - any side: calling mbedtls_ssl_renegotiate(),
8161 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8162 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008163 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008164 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008168{
8169 int ret;
8170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008172
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008173 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8174 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008175
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008176 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8177 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008181 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008182 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008183 ssl->handshake->out_msg_seq = 1;
8184 else
8185 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008186 }
8187#endif
8188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8190 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008195 return( ret );
8196 }
8197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008199
8200 return( 0 );
8201}
8202
8203/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008204 * Renegotiate current connection on client,
8205 * or request renegotiation on server
8206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008210
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008211 if( ssl == NULL || ssl->conf == NULL )
8212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008215 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008216 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008222
8223 /* Did we already try/start sending HelloRequest? */
8224 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008226
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008227 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008228 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008232 /*
8233 * On client, either start the renegotiation process or,
8234 * if already in progress, continue the handshake
8235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008240
8241 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008244 return( ret );
8245 }
8246 }
8247 else
8248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008252 return( ret );
8253 }
8254 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008256
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008257 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008258}
8259
8260/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008261 * Check record counters and renegotiate if they're above the limit.
8262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008264{
Andres AG2196c7f2016-12-15 17:01:16 +00008265 size_t ep_len = ssl_ep_len( ssl );
8266 int in_ctr_cmp;
8267 int out_ctr_cmp;
8268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8270 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008271 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008272 {
8273 return( 0 );
8274 }
8275
Andres AG2196c7f2016-12-15 17:01:16 +00008276 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8277 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008278 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008279 ssl->conf->renego_period + ep_len, 8 - ep_len );
8280
8281 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008282 {
8283 return( 0 );
8284 }
8285
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008290
8291/*
8292 * Receive application data decrypted from the SSL layer
8293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008295{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008296 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008297 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008298
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008299 if( ssl == NULL || ssl->conf == NULL )
8300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008308 return( ret );
8309
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008310 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008312 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008313 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008314 return( ret );
8315 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008316 }
8317#endif
8318
Hanno Becker4a810fb2017-05-24 16:27:30 +01008319 /*
8320 * Check if renegotiation is necessary and/or handshake is
8321 * in process. If yes, perform/continue, and fall through
8322 * if an unexpected packet is received while the client
8323 * is waiting for the ServerHello.
8324 *
8325 * (There is no equivalent to the last condition on
8326 * the server-side as it is not treated as within
8327 * a handshake while waiting for the ClientHello
8328 * after a renegotiation request.)
8329 */
8330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008332 ret = ssl_check_ctr_renegotiate( ssl );
8333 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8334 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008337 return( ret );
8338 }
8339#endif
8340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008344 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8345 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008348 return( ret );
8349 }
8350 }
8351
Hanno Beckere41158b2017-10-23 13:30:32 +01008352 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008353 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008354 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008355 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008356 if( ssl->f_get_timer != NULL &&
8357 ssl->f_get_timer( ssl->p_timer ) == -1 )
8358 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008359 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008360 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008361
Hanno Becker327c93b2018-08-15 13:56:18 +01008362 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008363 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008364 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8365 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008366
Hanno Becker4a810fb2017-05-24 16:27:30 +01008367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8368 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008369 }
8370
8371 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008373 {
8374 /*
8375 * OpenSSL sends empty messages to randomize the IV
8376 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008377 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008380 return( 0 );
8381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008383 return( ret );
8384 }
8385 }
8386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008390
Hanno Becker4a810fb2017-05-24 16:27:30 +01008391 /*
8392 * - For client-side, expect SERVER_HELLO_REQUEST.
8393 * - For server-side, expect CLIENT_HELLO.
8394 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8395 */
8396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008398 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008400 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008403
8404 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008406 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008407 {
8408 continue;
8409 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008412 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008413#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008414
Hanno Becker4a810fb2017-05-24 16:27:30 +01008415#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008416 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008420
8421 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008424 {
8425 continue;
8426 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008429 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008430#endif /* MBEDTLS_SSL_SRV_C */
8431
Hanno Becker21df7f92017-10-17 11:03:26 +01008432#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008433 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008434 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8435 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8436 ssl->conf->allow_legacy_renegotiation ==
8437 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8438 {
8439 /*
8440 * Accept renegotiation request
8441 */
Paul Bakker48916f92012-09-16 19:57:18 +00008442
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008443 /* DTLS clients need to know renego is server-initiated */
8444#if defined(MBEDTLS_SSL_PROTO_DTLS)
8445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8446 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8447 {
8448 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8449 }
8450#endif
8451 ret = ssl_start_renegotiation( ssl );
8452 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8453 ret != 0 )
8454 {
8455 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8456 return( ret );
8457 }
8458 }
8459 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008460#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008461 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008462 /*
8463 * Refuse renegotiation
8464 */
8465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468#if defined(MBEDTLS_SSL_PROTO_SSL3)
8469 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008470 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008471 /* SSLv3 does not have a "no_renegotiation" warning, so
8472 we send a fatal alert and abort the connection. */
8473 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8474 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8475 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008476 }
8477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8479#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8480 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8481 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8484 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8485 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008486 {
8487 return( ret );
8488 }
Paul Bakker48916f92012-09-16 19:57:18 +00008489 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008490 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8492 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008496 }
Paul Bakker48916f92012-09-16 19:57:18 +00008497 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008498
Hanno Becker90333da2017-10-10 11:27:13 +01008499 /* At this point, we don't know whether the renegotiation has been
8500 * completed or not. The cases to consider are the following:
8501 * 1) The renegotiation is complete. In this case, no new record
8502 * has been read yet.
8503 * 2) The renegotiation is incomplete because the client received
8504 * an application data record while awaiting the ServerHello.
8505 * 3) The renegotiation is incomplete because the client received
8506 * a non-handshake, non-application data message while awaiting
8507 * the ServerHello.
8508 * In each of these case, looping will be the proper action:
8509 * - For 1), the next iteration will read a new record and check
8510 * if it's application data.
8511 * - For 2), the loop condition isn't satisfied as application data
8512 * is present, hence continue is the same as break
8513 * - For 3), the loop condition is satisfied and read_record
8514 * will re-deliver the message that was held back by the client
8515 * when expecting the ServerHello.
8516 */
8517 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008518 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008519#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008521 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008522 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008523 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008524 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008527 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008529 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008530 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8535 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008538 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008539 }
8540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008541 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8544 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008545 }
8546
8547 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008548
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008549 /* We're going to return something now, cancel timer,
8550 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008552 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008553
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008555 /* If we requested renego but received AppData, resend HelloRequest.
8556 * Do it now, after setting in_offt, to avoid taking this branch
8557 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008561 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008562 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008565 return( ret );
8566 }
8567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008569#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008570 }
8571
8572 n = ( len < ssl->in_msglen )
8573 ? len : ssl->in_msglen;
8574
8575 memcpy( buf, ssl->in_offt, n );
8576 ssl->in_msglen -= n;
8577
8578 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008579 {
8580 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008581 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008582 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008584 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008585 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008586 /* more data available */
8587 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008591
Paul Bakker23986e52011-04-24 08:57:21 +00008592 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008593}
8594
8595/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008596 * Send application data to be encrypted by the SSL layer, taking care of max
8597 * fragment length and buffer size.
8598 *
8599 * According to RFC 5246 Section 6.2.1:
8600 *
8601 * Zero-length fragments of Application data MAY be sent as they are
8602 * potentially useful as a traffic analysis countermeasure.
8603 *
8604 * Therefore, it is possible that the input message length is 0 and the
8605 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008606 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008607static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008608 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008609{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008610 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8611 const size_t max_len = (size_t) ret;
8612
8613 if( ret < 0 )
8614 {
8615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8616 return( ret );
8617 }
8618
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008619 if( len > max_len )
8620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008625 "maximum fragment length: %d > %d",
8626 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008628 }
8629 else
8630#endif
8631 len = max_len;
8632 }
Paul Bakker887bd502011-06-08 13:10:54 +00008633
Paul Bakker5121ce52009-01-03 21:22:43 +00008634 if( ssl->out_left != 0 )
8635 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008636 /*
8637 * The user has previously tried to send the data and
8638 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8639 * written. In this case, we expect the high-level write function
8640 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008645 return( ret );
8646 }
8647 }
Paul Bakker887bd502011-06-08 13:10:54 +00008648 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008649 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008650 /*
8651 * The user is trying to send a message the first time, so we need to
8652 * copy the data into the internal buffers and setup the data structure
8653 * to keep track of partial writes
8654 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008655 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008657 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008658
Hanno Becker67bc7c32018-08-06 11:33:50 +01008659 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008662 return( ret );
8663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008664 }
8665
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008666 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008667}
8668
8669/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008670 * Write application data, doing 1/n-1 splitting if necessary.
8671 *
8672 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008673 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008674 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008677static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008678 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008679{
8680 int ret;
8681
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008682 if( ssl->conf->cbc_record_splitting ==
8683 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008684 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8686 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8687 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008688 {
8689 return( ssl_write_real( ssl, buf, len ) );
8690 }
8691
8692 if( ssl->split_done == 0 )
8693 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008694 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008695 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008696 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008697 }
8698
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008699 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8700 return( ret );
8701 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008702
8703 return( ret + 1 );
8704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008706
8707/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008708 * Write application data (public-facing wrapper)
8709 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008710int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008711{
8712 int ret;
8713
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008715
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008716 if( ssl == NULL || ssl->conf == NULL )
8717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8718
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008719#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008720 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8721 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008722 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008723 return( ret );
8724 }
8725#endif
8726
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008727 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008728 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008729 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008730 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008732 return( ret );
8733 }
8734 }
8735
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008736#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008737 ret = ssl_write_split( ssl, buf, len );
8738#else
8739 ret = ssl_write_real( ssl, buf, len );
8740#endif
8741
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008743
8744 return( ret );
8745}
8746
8747/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008748 * Notify the peer that the connection is being closed
8749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008751{
8752 int ret;
8753
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008754 if( ssl == NULL || ssl->conf == NULL )
8755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008758
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008759 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8765 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8766 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008769 return( ret );
8770 }
8771 }
8772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008774
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008775 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008776}
8777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008779{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008780 if( transform == NULL )
8781 return;
8782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008784 deflateEnd( &transform->ctx_deflate );
8785 inflateEnd( &transform->ctx_inflate );
8786#endif
8787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8789 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008791 mbedtls_md_free( &transform->md_ctx_enc );
8792 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008793
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008794 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008795}
8796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797#if defined(MBEDTLS_X509_CRT_PARSE_C)
8798static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008799{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008801
8802 while( cur != NULL )
8803 {
8804 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008806 cur = next;
8807 }
8808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008810
Hanno Becker0271f962018-08-16 13:23:47 +01008811#if defined(MBEDTLS_SSL_PROTO_DTLS)
8812
8813static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8814{
8815 unsigned offset;
8816 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8817
8818 if( hs == NULL )
8819 return;
8820
Hanno Becker283f5ef2018-08-24 09:34:47 +01008821 ssl_free_buffered_record( ssl );
8822
Hanno Becker0271f962018-08-16 13:23:47 +01008823 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008824 ssl_buffering_free_slot( ssl, offset );
8825}
8826
8827static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8828 uint8_t slot )
8829{
8830 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8831 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008832
8833 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8834 return;
8835
Hanno Beckere605b192018-08-21 15:59:07 +01008836 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008837 {
Hanno Beckere605b192018-08-21 15:59:07 +01008838 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008839 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008840 mbedtls_free( hs_buf->data );
8841 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008842 }
8843}
8844
8845#endif /* MBEDTLS_SSL_PROTO_DTLS */
8846
Gilles Peskine9b562d52018-04-25 20:32:43 +02008847void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008848{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008849 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8850
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008851 if( handshake == NULL )
8852 return;
8853
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008854#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8855 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8856 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008857 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008858 handshake->async_in_progress = 0;
8859 }
8860#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8861
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008862#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8863 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8864 mbedtls_md5_free( &handshake->fin_md5 );
8865 mbedtls_sha1_free( &handshake->fin_sha1 );
8866#endif
8867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8868#if defined(MBEDTLS_SHA256_C)
8869 mbedtls_sha256_free( &handshake->fin_sha256 );
8870#endif
8871#if defined(MBEDTLS_SHA512_C)
8872 mbedtls_sha512_free( &handshake->fin_sha512 );
8873#endif
8874#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876#if defined(MBEDTLS_DHM_C)
8877 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879#if defined(MBEDTLS_ECDH_C)
8880 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008881#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008882#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008883 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008884#if defined(MBEDTLS_SSL_CLI_C)
8885 mbedtls_free( handshake->ecjpake_cache );
8886 handshake->ecjpake_cache = NULL;
8887 handshake->ecjpake_cache_len = 0;
8888#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008889#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008890
Janos Follath4ae5c292016-02-10 11:27:43 +00008891#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8892 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008893 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008895#endif
8896
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008897#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8898 if( handshake->psk != NULL )
8899 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008900 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008901 mbedtls_free( handshake->psk );
8902 }
8903#endif
8904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8906 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008907 /*
8908 * Free only the linked list wrapper, not the keys themselves
8909 * since the belong to the SNI callback
8910 */
8911 if( handshake->sni_key_cert != NULL )
8912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008914
8915 while( cur != NULL )
8916 {
8917 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008918 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008919 cur = next;
8920 }
8921 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008922#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008923
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008924#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008925 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008926#endif
8927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928#if defined(MBEDTLS_SSL_PROTO_DTLS)
8929 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008930 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008931 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008932#endif
8933
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008934 mbedtls_platform_zeroize( handshake,
8935 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008936}
8937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008939{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008940 if( session == NULL )
8941 return;
8942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008944 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946 mbedtls_x509_crt_free( session->peer_cert );
8947 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008948 }
Paul Bakkered27a042013-04-18 22:46:23 +02008949#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008950
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008951#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008953#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008954
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008955 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008956}
8957
Paul Bakker5121ce52009-01-03 21:22:43 +00008958/*
8959 * Free an SSL context
8960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008962{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008963 if( ssl == NULL )
8964 return;
8965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008967
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008968 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008969 {
Angus Grattond8213d02016-05-25 20:56:48 +10008970 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008972 }
8973
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008974 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008975 {
Angus Grattond8213d02016-05-25 20:56:48 +10008976 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008978 }
8979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008981 if( ssl->compress_buf != NULL )
8982 {
Angus Grattond8213d02016-05-25 20:56:48 +10008983 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008985 }
8986#endif
8987
Paul Bakker48916f92012-09-16 19:57:18 +00008988 if( ssl->transform )
8989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 mbedtls_ssl_transform_free( ssl->transform );
8991 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008992 }
8993
8994 if( ssl->handshake )
8995 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008996 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8998 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 mbedtls_free( ssl->handshake );
9001 mbedtls_free( ssl->transform_negotiate );
9002 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009003 }
9004
Paul Bakkerc0463502013-02-14 11:19:38 +01009005 if( ssl->session )
9006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007 mbedtls_ssl_session_free( ssl->session );
9008 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009009 }
9010
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009011#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009012 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009013 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009014 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009016 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009017#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9020 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9023 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009024 }
9025#endif
9026
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009027#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009029#endif
9030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009032
Paul Bakker86f04f42013-02-14 11:20:09 +01009033 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009034 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009035}
9036
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009037/*
9038 * Initialze mbedtls_ssl_config
9039 */
9040void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9041{
9042 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9043}
9044
Simon Butcherc97b6972015-12-27 23:48:17 +00009045#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009046static int ssl_preset_default_hashes[] = {
9047#if defined(MBEDTLS_SHA512_C)
9048 MBEDTLS_MD_SHA512,
9049 MBEDTLS_MD_SHA384,
9050#endif
9051#if defined(MBEDTLS_SHA256_C)
9052 MBEDTLS_MD_SHA256,
9053 MBEDTLS_MD_SHA224,
9054#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009055#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009056 MBEDTLS_MD_SHA1,
9057#endif
9058 MBEDTLS_MD_NONE
9059};
Simon Butcherc97b6972015-12-27 23:48:17 +00009060#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009061
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009062static int ssl_preset_suiteb_ciphersuites[] = {
9063 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9064 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9065 0
9066};
9067
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009068#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009069static int ssl_preset_suiteb_hashes[] = {
9070 MBEDTLS_MD_SHA256,
9071 MBEDTLS_MD_SHA384,
9072 MBEDTLS_MD_NONE
9073};
9074#endif
9075
9076#if defined(MBEDTLS_ECP_C)
9077static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9078 MBEDTLS_ECP_DP_SECP256R1,
9079 MBEDTLS_ECP_DP_SECP384R1,
9080 MBEDTLS_ECP_DP_NONE
9081};
9082#endif
9083
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009084/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009085 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009086 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009087int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009088 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009089{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009090#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009091 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009092#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009093
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009094 /* Use the functions here so that they are covered in tests,
9095 * but otherwise access member directly for efficiency */
9096 mbedtls_ssl_conf_endpoint( conf, endpoint );
9097 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009098
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009099 /*
9100 * Things that are common to all presets
9101 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009102#if defined(MBEDTLS_SSL_CLI_C)
9103 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9104 {
9105 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9106#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9107 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9108#endif
9109 }
9110#endif
9111
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009112#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009113 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009114#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009115
9116#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9117 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9118#endif
9119
9120#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9121 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9122#endif
9123
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009124#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9125 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9126#endif
9127
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009128#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009129 conf->f_cookie_write = ssl_cookie_write_dummy;
9130 conf->f_cookie_check = ssl_cookie_check_dummy;
9131#endif
9132
9133#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9134 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9135#endif
9136
Janos Follath088ce432017-04-10 12:42:31 +01009137#if defined(MBEDTLS_SSL_SRV_C)
9138 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9139#endif
9140
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009141#if defined(MBEDTLS_SSL_PROTO_DTLS)
9142 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9143 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9144#endif
9145
9146#if defined(MBEDTLS_SSL_RENEGOTIATION)
9147 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009148 memset( conf->renego_period, 0x00, 2 );
9149 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009150#endif
9151
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009152#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9153 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9154 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009155 const unsigned char dhm_p[] =
9156 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9157 const unsigned char dhm_g[] =
9158 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9159
Hanno Beckera90658f2017-10-04 15:29:08 +01009160 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9161 dhm_p, sizeof( dhm_p ),
9162 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009163 {
9164 return( ret );
9165 }
9166 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009167#endif
9168
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009169 /*
9170 * Preset-specific defaults
9171 */
9172 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009173 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009174 /*
9175 * NSA Suite B
9176 */
9177 case MBEDTLS_SSL_PRESET_SUITEB:
9178 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9179 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9180 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9181 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9182
9183 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9184 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9185 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9186 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9187 ssl_preset_suiteb_ciphersuites;
9188
9189#if defined(MBEDTLS_X509_CRT_PARSE_C)
9190 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009191#endif
9192
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009193#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009194 conf->sig_hashes = ssl_preset_suiteb_hashes;
9195#endif
9196
9197#if defined(MBEDTLS_ECP_C)
9198 conf->curve_list = ssl_preset_suiteb_curves;
9199#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009200 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009201
9202 /*
9203 * Default
9204 */
9205 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009206 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9207 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9208 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9209 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9210 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9211 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9212 MBEDTLS_SSL_MIN_MINOR_VERSION :
9213 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009214 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9215 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9216
9217#if defined(MBEDTLS_SSL_PROTO_DTLS)
9218 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9219 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9220#endif
9221
9222 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9223 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9224 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9225 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9226 mbedtls_ssl_list_ciphersuites();
9227
9228#if defined(MBEDTLS_X509_CRT_PARSE_C)
9229 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9230#endif
9231
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009232#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009233 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009234#endif
9235
9236#if defined(MBEDTLS_ECP_C)
9237 conf->curve_list = mbedtls_ecp_grp_id_list();
9238#endif
9239
9240#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9241 conf->dhm_min_bitlen = 1024;
9242#endif
9243 }
9244
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009245 return( 0 );
9246}
9247
9248/*
9249 * Free mbedtls_ssl_config
9250 */
9251void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9252{
9253#if defined(MBEDTLS_DHM_C)
9254 mbedtls_mpi_free( &conf->dhm_P );
9255 mbedtls_mpi_free( &conf->dhm_G );
9256#endif
9257
9258#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9259 if( conf->psk != NULL )
9260 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009261 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009262 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009263 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009264 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009265 }
9266
9267 if( conf->psk_identity != NULL )
9268 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009269 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009270 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009271 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009272 conf->psk_identity_len = 0;
9273 }
9274#endif
9275
9276#if defined(MBEDTLS_X509_CRT_PARSE_C)
9277 ssl_key_cert_free( conf->key_cert );
9278#endif
9279
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009280 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009281}
9282
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009283#if defined(MBEDTLS_PK_C) && \
9284 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009285/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009289{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290#if defined(MBEDTLS_RSA_C)
9291 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9292 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294#if defined(MBEDTLS_ECDSA_C)
9295 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9296 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009299}
9300
Hanno Becker7e5437a2017-04-28 17:15:26 +01009301unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9302{
9303 switch( type ) {
9304 case MBEDTLS_PK_RSA:
9305 return( MBEDTLS_SSL_SIG_RSA );
9306 case MBEDTLS_PK_ECDSA:
9307 case MBEDTLS_PK_ECKEY:
9308 return( MBEDTLS_SSL_SIG_ECDSA );
9309 default:
9310 return( MBEDTLS_SSL_SIG_ANON );
9311 }
9312}
9313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009315{
9316 switch( sig )
9317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318#if defined(MBEDTLS_RSA_C)
9319 case MBEDTLS_SSL_SIG_RSA:
9320 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322#if defined(MBEDTLS_ECDSA_C)
9323 case MBEDTLS_SSL_SIG_ECDSA:
9324 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009325#endif
9326 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009328 }
9329}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009330#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009331
Hanno Becker7e5437a2017-04-28 17:15:26 +01009332#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9333 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9334
9335/* Find an entry in a signature-hash set matching a given hash algorithm. */
9336mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9337 mbedtls_pk_type_t sig_alg )
9338{
9339 switch( sig_alg )
9340 {
9341 case MBEDTLS_PK_RSA:
9342 return( set->rsa );
9343 case MBEDTLS_PK_ECDSA:
9344 return( set->ecdsa );
9345 default:
9346 return( MBEDTLS_MD_NONE );
9347 }
9348}
9349
9350/* Add a signature-hash-pair to a signature-hash set */
9351void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9352 mbedtls_pk_type_t sig_alg,
9353 mbedtls_md_type_t md_alg )
9354{
9355 switch( sig_alg )
9356 {
9357 case MBEDTLS_PK_RSA:
9358 if( set->rsa == MBEDTLS_MD_NONE )
9359 set->rsa = md_alg;
9360 break;
9361
9362 case MBEDTLS_PK_ECDSA:
9363 if( set->ecdsa == MBEDTLS_MD_NONE )
9364 set->ecdsa = md_alg;
9365 break;
9366
9367 default:
9368 break;
9369 }
9370}
9371
9372/* Allow exactly one hash algorithm for each signature. */
9373void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9374 mbedtls_md_type_t md_alg )
9375{
9376 set->rsa = md_alg;
9377 set->ecdsa = md_alg;
9378}
9379
9380#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9381 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9382
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009383/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009384 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009387{
9388 switch( hash )
9389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390#if defined(MBEDTLS_MD5_C)
9391 case MBEDTLS_SSL_HASH_MD5:
9392 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009393#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394#if defined(MBEDTLS_SHA1_C)
9395 case MBEDTLS_SSL_HASH_SHA1:
9396 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398#if defined(MBEDTLS_SHA256_C)
9399 case MBEDTLS_SSL_HASH_SHA224:
9400 return( MBEDTLS_MD_SHA224 );
9401 case MBEDTLS_SSL_HASH_SHA256:
9402 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404#if defined(MBEDTLS_SHA512_C)
9405 case MBEDTLS_SSL_HASH_SHA384:
9406 return( MBEDTLS_MD_SHA384 );
9407 case MBEDTLS_SSL_HASH_SHA512:
9408 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009409#endif
9410 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009412 }
9413}
9414
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009415/*
9416 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9417 */
9418unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9419{
9420 switch( md )
9421 {
9422#if defined(MBEDTLS_MD5_C)
9423 case MBEDTLS_MD_MD5:
9424 return( MBEDTLS_SSL_HASH_MD5 );
9425#endif
9426#if defined(MBEDTLS_SHA1_C)
9427 case MBEDTLS_MD_SHA1:
9428 return( MBEDTLS_SSL_HASH_SHA1 );
9429#endif
9430#if defined(MBEDTLS_SHA256_C)
9431 case MBEDTLS_MD_SHA224:
9432 return( MBEDTLS_SSL_HASH_SHA224 );
9433 case MBEDTLS_MD_SHA256:
9434 return( MBEDTLS_SSL_HASH_SHA256 );
9435#endif
9436#if defined(MBEDTLS_SHA512_C)
9437 case MBEDTLS_MD_SHA384:
9438 return( MBEDTLS_SSL_HASH_SHA384 );
9439 case MBEDTLS_MD_SHA512:
9440 return( MBEDTLS_SSL_HASH_SHA512 );
9441#endif
9442 default:
9443 return( MBEDTLS_SSL_HASH_NONE );
9444 }
9445}
9446
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009447#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009448/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009449 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009450 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009451 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009452int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009455
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009456 if( ssl->conf->curve_list == NULL )
9457 return( -1 );
9458
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009459 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009460 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009461 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009462
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009463 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009464}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009465#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009466
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009467#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009468/*
9469 * Check if a hash proposed by the peer is in our list.
9470 * Return 0 if we're willing to use it, -1 otherwise.
9471 */
9472int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9473 mbedtls_md_type_t md )
9474{
9475 const int *cur;
9476
9477 if( ssl->conf->sig_hashes == NULL )
9478 return( -1 );
9479
9480 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9481 if( *cur == (int) md )
9482 return( 0 );
9483
9484 return( -1 );
9485}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009486#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#if defined(MBEDTLS_X509_CRT_PARSE_C)
9489int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9490 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009491 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009492 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009493{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009494 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009496 int usage = 0;
9497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009499 const char *ext_oid;
9500 size_t ext_len;
9501#endif
9502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9504 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009505 ((void) cert);
9506 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009507 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009508#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9511 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009512 {
9513 /* Server part of the key exchange */
9514 switch( ciphersuite->key_exchange )
9515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 case MBEDTLS_KEY_EXCHANGE_RSA:
9517 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009518 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009519 break;
9520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9522 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9523 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9524 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009525 break;
9526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9528 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009529 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009530 break;
9531
9532 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533 case MBEDTLS_KEY_EXCHANGE_NONE:
9534 case MBEDTLS_KEY_EXCHANGE_PSK:
9535 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9536 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009537 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009538 usage = 0;
9539 }
9540 }
9541 else
9542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9544 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009545 }
9546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009548 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009549 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009550 ret = -1;
9551 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009552#else
9553 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9557 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9560 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009561 }
9562 else
9563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9565 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009566 }
9567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009569 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009570 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009571 ret = -1;
9572 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009574
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009575 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009578
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009579/*
9580 * Convert version numbers to/from wire format
9581 * and, for DTLS, to/from TLS equivalent.
9582 *
9583 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009584 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009585 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9586 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009589 unsigned char ver[2] )
9590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591#if defined(MBEDTLS_SSL_PROTO_DTLS)
9592 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009595 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9596
9597 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9598 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9599 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009600 else
9601#else
9602 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009603#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009604 {
9605 ver[0] = (unsigned char) major;
9606 ver[1] = (unsigned char) minor;
9607 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009608}
9609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009611 const unsigned char ver[2] )
9612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613#if defined(MBEDTLS_SSL_PROTO_DTLS)
9614 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009615 {
9616 *major = 255 - ver[0] + 2;
9617 *minor = 255 - ver[1] + 1;
9618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009620 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9621 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009622 else
9623#else
9624 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009625#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009626 {
9627 *major = ver[0];
9628 *minor = ver[1];
9629 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009630}
9631
Simon Butcher99000142016-10-13 17:21:01 +01009632int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9633{
9634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9635 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9636 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9637
9638 switch( md )
9639 {
9640#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9641#if defined(MBEDTLS_MD5_C)
9642 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009643 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009644#endif
9645#if defined(MBEDTLS_SHA1_C)
9646 case MBEDTLS_SSL_HASH_SHA1:
9647 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9648 break;
9649#endif
9650#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9651#if defined(MBEDTLS_SHA512_C)
9652 case MBEDTLS_SSL_HASH_SHA384:
9653 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9654 break;
9655#endif
9656#if defined(MBEDTLS_SHA256_C)
9657 case MBEDTLS_SSL_HASH_SHA256:
9658 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9659 break;
9660#endif
9661 default:
9662 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9663 }
9664
9665 return 0;
9666#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9667 (void) ssl;
9668 (void) md;
9669
9670 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9672}
9673
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9675 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9676int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9677 unsigned char *output,
9678 unsigned char *data, size_t data_len )
9679{
9680 int ret = 0;
9681 mbedtls_md5_context mbedtls_md5;
9682 mbedtls_sha1_context mbedtls_sha1;
9683
9684 mbedtls_md5_init( &mbedtls_md5 );
9685 mbedtls_sha1_init( &mbedtls_sha1 );
9686
9687 /*
9688 * digitally-signed struct {
9689 * opaque md5_hash[16];
9690 * opaque sha_hash[20];
9691 * };
9692 *
9693 * md5_hash
9694 * MD5(ClientHello.random + ServerHello.random
9695 * + ServerParams);
9696 * sha_hash
9697 * SHA(ClientHello.random + ServerHello.random
9698 * + ServerParams);
9699 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009700 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009701 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009703 goto exit;
9704 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009705 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009706 ssl->handshake->randbytes, 64 ) ) != 0 )
9707 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009709 goto exit;
9710 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009711 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009712 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009713 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009714 goto exit;
9715 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009716 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009717 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009719 goto exit;
9720 }
9721
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009722 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009723 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009725 goto exit;
9726 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009727 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009728 ssl->handshake->randbytes, 64 ) ) != 0 )
9729 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009731 goto exit;
9732 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009733 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009734 data_len ) ) != 0 )
9735 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009737 goto exit;
9738 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009739 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009740 output + 16 ) ) != 0 )
9741 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009743 goto exit;
9744 }
9745
9746exit:
9747 mbedtls_md5_free( &mbedtls_md5 );
9748 mbedtls_sha1_free( &mbedtls_sha1 );
9749
9750 if( ret != 0 )
9751 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9752 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9753
9754 return( ret );
9755
9756}
9757#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9758 MBEDTLS_SSL_PROTO_TLS1_1 */
9759
9760#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9761 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9762int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009763 unsigned char *hash, size_t *hashlen,
9764 unsigned char *data, size_t data_len,
9765 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009766{
9767 int ret = 0;
9768 mbedtls_md_context_t ctx;
9769 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009770 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009771
9772 mbedtls_md_init( &ctx );
9773
9774 /*
9775 * digitally-signed struct {
9776 * opaque client_random[32];
9777 * opaque server_random[32];
9778 * ServerDHParams params;
9779 * };
9780 */
9781 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9782 {
9783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9784 goto exit;
9785 }
9786 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9787 {
9788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9789 goto exit;
9790 }
9791 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9792 {
9793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9794 goto exit;
9795 }
9796 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9797 {
9798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9799 goto exit;
9800 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009801 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009802 {
9803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9804 goto exit;
9805 }
9806
9807exit:
9808 mbedtls_md_free( &ctx );
9809
9810 if( ret != 0 )
9811 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9812 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9813
9814 return( ret );
9815}
9816#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9817 MBEDTLS_SSL_PROTO_TLS1_2 */
9818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819#endif /* MBEDTLS_SSL_TLS_C */