blob: 981774954f5cbcfe80d1b9257af8e5d3c8f35ff8 [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;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000621 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000622 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 const mbedtls_cipher_info_t *cipher_info;
624 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_ssl_session *session = ssl->session_negotiate;
627 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
628 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Hanno Becker8759e162017-12-27 21:34:08 +0000632 ciphersuite_info = handshake->ciphersuite_info;
633 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100634 if( cipher_info == NULL )
635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000637 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100639 }
640
Hanno Becker8759e162017-12-27 21:34:08 +0000641 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100642 if( md_info == NULL )
643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000645 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100647 }
648
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000650 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_SSL3)
653 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000654 {
Paul Bakker48916f92012-09-16 19:57:18 +0000655 handshake->tls_prf = ssl3_prf;
656 handshake->calc_verify = ssl_calc_verify_ssl;
657 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000658 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200659 else
660#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
662 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000663 {
Paul Bakker48916f92012-09-16 19:57:18 +0000664 handshake->tls_prf = tls1_prf;
665 handshake->calc_verify = ssl_calc_verify_tls;
666 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000667 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668 else
669#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
671#if defined(MBEDTLS_SHA512_C)
672 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker8759e162017-12-27 21:34:08 +0000673 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000674 {
Paul Bakker48916f92012-09-16 19:57:18 +0000675 handshake->tls_prf = tls_prf_sha384;
676 handshake->calc_verify = ssl_calc_verify_tls_sha384;
677 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000678 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000679 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA256_C)
682 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000683 {
Paul Bakker48916f92012-09-16 19:57:18 +0000684 handshake->tls_prf = tls_prf_sha256;
685 handshake->calc_verify = ssl_calc_verify_tls_sha256;
686 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000687 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688 else
689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200694 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000695
696 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 * SSLv3:
698 * master =
699 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
700 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
701 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200702 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 * master = PRF( premaster, "master secret", randbytes )[0..47]
705 */
Paul Bakker0a597072012-09-25 21:55:46 +0000706 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000709 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
712 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200713 {
714 unsigned char session_hash[48];
715 size_t hash_len;
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200718
719 ssl->handshake->calc_verify( ssl, session_hash );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
722 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_SHA512_C)
Hanno Becker8759e162017-12-27 21:34:08 +0000725 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200726 {
727 hash_len = 48;
728 }
729 else
730#endif
731 hash_len = 32;
732 }
733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200735 hash_len = 36;
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200738
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100739 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
740 "extended master secret",
741 session_hash, hash_len,
742 session->master, 48 );
743 if( ret != 0 )
744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100746 return( ret );
747 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200748
749 }
750 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200751#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100752 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
753 "master secret",
754 handshake->randbytes, 64,
755 session->master, 48 );
756 if( ret != 0 )
757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100759 return( ret );
760 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200761
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500762 mbedtls_platform_zeroize( handshake->premaster,
763 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 }
765 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
768 /*
769 * Swap the client and server random values.
770 */
Paul Bakker48916f92012-09-16 19:57:18 +0000771 memcpy( tmp, handshake->randbytes, 64 );
772 memcpy( handshake->randbytes, tmp + 32, 32 );
773 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500774 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
776 /*
777 * SSLv3:
778 * key block =
779 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
780 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
781 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
782 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
783 * ...
784 *
785 * TLSv1:
786 * key block = PRF( master, "key expansion", randbytes )
787 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100788 ret = handshake->tls_prf( session->master, 48, "key expansion",
789 handshake->randbytes, 64, keyblk, 256 );
790 if( ret != 0 )
791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100793 return( ret );
794 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
797 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
798 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
799 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
800 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500802 mbedtls_platform_zeroize( handshake->randbytes,
803 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
805 /*
806 * Determine the appropriate key, IV and MAC length.
807 */
Paul Bakker68884e32013-01-07 18:20:04 +0100808
Hanno Beckere7f2df02017-12-27 08:17:40 +0000809 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200812 cipher_info->mode == MBEDTLS_MODE_CCM ||
813 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 {
Hanno Becker8759e162017-12-27 21:34:08 +0000815 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200816
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200817 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000818 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000819 transform->taglen =
820 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200821
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200822 /* All modes haves 96-bit IVs;
823 * GCM and CCM has 4 implicit and 8 explicit bytes
824 * ChachaPoly has all 12 bytes implicit
825 */
Paul Bakker68884e32013-01-07 18:20:04 +0100826 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200827 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
828 transform->fixed_ivlen = 12;
829 else
830 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200831
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200832 /* Minimum length of encrypted record */
833 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000834 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100835 }
836 else
837 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200838 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
840 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200843 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100844 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200846 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000847 mac_key_len = mbedtls_md_get_size( md_info );
848 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200851 /*
852 * If HMAC is to be truncated, we shall keep the leftmost bytes,
853 * (rfc 6066 page 13 or rfc 2104 section 4),
854 * so we only need to adjust the length here.
855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000859
860#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
861 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000862 * HMAC implementation which also truncates the key
863 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000864 mac_key_len = transform->maclen;
865#endif
866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200868
869 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100870 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200872 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874 transform->minlen = transform->maclen;
875 else
Paul Bakker68884e32013-01-07 18:20:04 +0100876 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200877 /*
878 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100879 * 1. if EtM is in use: one block plus MAC
880 * otherwise: * first multiple of blocklen greater than maclen
881 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
884 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100885 {
886 transform->minlen = transform->maclen
887 + cipher_info->block_size;
888 }
889 else
890#endif
891 {
892 transform->minlen = transform->maclen
893 + cipher_info->block_size
894 - transform->maclen % cipher_info->block_size;
895 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
898 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
899 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200900 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100901 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
904 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
905 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200906 {
907 transform->minlen += transform->ivlen;
908 }
909 else
910#endif
911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200914 }
Paul Bakker68884e32013-01-07 18:20:04 +0100915 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 }
917
Hanno Beckere7f2df02017-12-27 08:17:40 +0000918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
919 (unsigned) keylen,
920 (unsigned) transform->minlen,
921 (unsigned) transform->ivlen,
922 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000923
924 /*
925 * Finally setup the cipher contexts, IVs and MAC secrets.
926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200928 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000930 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000931 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Paul Bakker68884e32013-01-07 18:20:04 +0100933 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000934 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000936 /*
937 * This is not used in TLS v1.1.
938 */
Paul Bakker48916f92012-09-16 19:57:18 +0000939 iv_copy_len = ( transform->fixed_ivlen ) ?
940 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000941 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
942 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000943 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000944 }
945 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#endif /* MBEDTLS_SSL_CLI_C */
947#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200948 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000950 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000951 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Hanno Becker81c7b182017-11-09 18:39:33 +0000953 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100954 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000956 /*
957 * This is not used in TLS v1.1.
958 */
Paul Bakker48916f92012-09-16 19:57:18 +0000959 iv_copy_len = ( transform->fixed_ivlen ) ?
960 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000961 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
962 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000963 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100965 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
969 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100970 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_SSL_PROTO_SSL3)
973 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100974 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000975 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100979 }
980
Hanno Becker81c7b182017-11-09 18:39:33 +0000981 memcpy( transform->mac_enc, mac_enc, mac_key_len );
982 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100983 }
984 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_SSL_PROTO_SSL3 */
986#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
987 defined(MBEDTLS_SSL_PROTO_TLS1_2)
988 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100989 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100990 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
991 For AEAD-based ciphersuites, there is nothing to do here. */
992 if( mac_key_len != 0 )
993 {
994 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
995 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
996 }
Paul Bakker68884e32013-01-07 18:20:04 +0100997 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200998 else
999#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001003 }
Paul Bakker68884e32013-01-07 18:20:04 +01001004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1006 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001007 {
1008 int ret = 0;
1009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001011
Hanno Beckere7f2df02017-12-27 08:17:40 +00001012 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001013 transform->iv_enc, transform->iv_dec,
1014 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001015 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001016 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1019 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001020 }
1021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001023
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001024#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1025 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001026 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001027 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1028 session->master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001029 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001030 iv_copy_len );
1031 }
1032#endif
1033
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001034 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001035 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001038 return( ret );
1039 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001040
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001041 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001042 cipher_info ) ) != 0 )
1043 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001045 return( ret );
1046 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001049 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001053 return( ret );
1054 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001057 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001061 return( ret );
1062 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_CIPHER_MODE_CBC)
1065 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1068 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001071 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001072 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1075 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001078 return( ret );
1079 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001083 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001086 // Initialize compression
1087 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001089 {
Paul Bakker16770332013-10-11 09:59:44 +02001090 if( ssl->compress_buf == NULL )
1091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001093 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001094 if( ssl->compress_buf == NULL )
1095 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001097 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001098 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001099 }
1100 }
1101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001103
Paul Bakker48916f92012-09-16 19:57:18 +00001104 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1105 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001106
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001107 if( deflateInit( &transform->ctx_deflate,
1108 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001109 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1112 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001113 }
1114 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
1119 return( 0 );
1120}
1121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122#if defined(MBEDTLS_SSL_PROTO_SSL3)
1123void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001125 mbedtls_md5_context md5;
1126 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 unsigned char pad_1[48];
1128 unsigned char pad_2[48];
1129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001132 mbedtls_md5_init( &md5 );
1133 mbedtls_sha1_init( &sha1 );
1134
1135 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1136 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Paul Bakker380da532012-04-18 16:10:25 +00001138 memset( pad_1, 0x36, 48 );
1139 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001141 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1142 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1143 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001145 mbedtls_md5_starts_ret( &md5 );
1146 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1147 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1148 mbedtls_md5_update_ret( &md5, hash, 16 );
1149 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001151 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1152 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1153 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001154
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001155 mbedtls_sha1_starts_ret( &sha1 );
1156 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1157 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1158 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1159 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001163
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001164 mbedtls_md5_free( &md5 );
1165 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001166
Paul Bakker380da532012-04-18 16:10:25 +00001167 return;
1168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1172void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001173{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001174 mbedtls_md5_context md5;
1175 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001178
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001179 mbedtls_md5_init( &md5 );
1180 mbedtls_sha1_init( &sha1 );
1181
1182 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1183 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001184
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001185 mbedtls_md5_finish_ret( &md5, hash );
1186 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001190
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001191 mbedtls_md5_free( &md5 );
1192 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001193
Paul Bakker380da532012-04-18 16:10:25 +00001194 return;
1195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1199#if defined(MBEDTLS_SHA256_C)
1200void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001201{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001202 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001203
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001204 mbedtls_sha256_init( &sha256 );
1205
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001207
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001208 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001209 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001213
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001214 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001215
Paul Bakker380da532012-04-18 16:10:25 +00001216 return;
1217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220#if defined(MBEDTLS_SHA512_C)
1221void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001222{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001223 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001224
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001225 mbedtls_sha512_init( &sha512 );
1226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001228
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001229 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001230 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001235 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001236
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 return;
1238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#endif /* MBEDTLS_SHA512_C */
1240#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1243int 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 +02001244{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001245 unsigned char *p = ssl->handshake->premaster;
1246 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001247 const unsigned char *psk = ssl->conf->psk;
1248 size_t psk_len = ssl->conf->psk_len;
1249
1250 /* If the psk callback was called, use its result */
1251 if( ssl->handshake->psk != NULL )
1252 {
1253 psk = ssl->handshake->psk;
1254 psk_len = ssl->handshake->psk_len;
1255 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001256
1257 /*
1258 * PMS = struct {
1259 * opaque other_secret<0..2^16-1>;
1260 * opaque psk<0..2^16-1>;
1261 * };
1262 * with "other_secret" depending on the particular key exchange
1263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1265 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001266 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001267 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001269
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001270 *(p++) = (unsigned char)( psk_len >> 8 );
1271 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001272
1273 if( end < p || (size_t)( end - p ) < psk_len )
1274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1275
1276 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001277 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001278 }
1279 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1281#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1282 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001283 {
1284 /*
1285 * other_secret already set by the ClientKeyExchange message,
1286 * and is 48 bytes long
1287 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001288 if( end - p < 2 )
1289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1290
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001291 *p++ = 0;
1292 *p++ = 48;
1293 p += 48;
1294 }
1295 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1297#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1298 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001299 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001300 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001301 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001302
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001303 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001305 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001306 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001309 return( ret );
1310 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001311 *(p++) = (unsigned char)( len >> 8 );
1312 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001313 p += len;
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001316 }
1317 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1319#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1320 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001321 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001322 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001323 size_t zlen;
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001326 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001327 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001330 return( ret );
1331 }
1332
1333 *(p++) = (unsigned char)( zlen >> 8 );
1334 *(p++) = (unsigned char)( zlen );
1335 p += zlen;
1336
Janos Follath3fbdada2018-08-15 10:26:53 +01001337 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1338 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001339 }
1340 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001345 }
1346
1347 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001348 if( end - p < 2 )
1349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001350
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001351 *(p++) = (unsigned char)( psk_len >> 8 );
1352 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001353
1354 if( end < p || (size_t)( end - p ) < psk_len )
1355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1356
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001357 memcpy( p, psk, psk_len );
1358 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001359
1360 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1361
1362 return( 0 );
1363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001367/*
1368 * SSLv3.0 MAC functions
1369 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001370#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001371static void ssl_mac( mbedtls_md_context_t *md_ctx,
1372 const unsigned char *secret,
1373 const unsigned char *buf, size_t len,
1374 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001375 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001376{
1377 unsigned char header[11];
1378 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001379 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1381 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001382
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001383 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001385 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001386 else
Paul Bakker68884e32013-01-07 18:20:04 +01001387 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
1389 memcpy( header, ctr, 8 );
1390 header[ 8] = (unsigned char) type;
1391 header[ 9] = (unsigned char)( len >> 8 );
1392 header[10] = (unsigned char)( len );
1393
Paul Bakker68884e32013-01-07 18:20:04 +01001394 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_md_starts( md_ctx );
1396 mbedtls_md_update( md_ctx, secret, md_size );
1397 mbedtls_md_update( md_ctx, padding, padlen );
1398 mbedtls_md_update( md_ctx, header, 11 );
1399 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001400 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Paul Bakker68884e32013-01-07 18:20:04 +01001402 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_md_starts( md_ctx );
1404 mbedtls_md_update( md_ctx, secret, md_size );
1405 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001406 mbedtls_md_update( md_ctx, out, md_size );
1407 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1412 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001413 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001414#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001415#endif
1416
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001417/* The function below is only used in the Lucky 13 counter-measure in
1418 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1419#if defined(SSL_SOME_MODES_USE_MAC) && \
1420 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1421 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1422 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1423/* This function makes sure every byte in the memory region is accessed
1424 * (in ascending addresses order) */
1425static void ssl_read_memory( unsigned char *p, size_t len )
1426{
1427 unsigned char acc = 0;
1428 volatile unsigned char force;
1429
1430 for( ; len != 0; p++, len-- )
1431 acc ^= *p;
1432
1433 force = acc;
1434 (void) force;
1435}
1436#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1437
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001438/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001444 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001448 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001452 }
1453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001457 ssl->out_msg, ssl->out_msglen );
1458
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001460 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001462#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 if( mode == MBEDTLS_MODE_STREAM ||
1464 ( mode == MBEDTLS_MODE_CBC
1465#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1466 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001467#endif
1468 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_SSL_PROTO_SSL3)
1471 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001472 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001473 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001474
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001475 ssl_mac( &ssl->transform_out->md_ctx_enc,
1476 ssl->transform_out->mac_enc,
1477 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001478 ssl->out_ctr, ssl->out_msgtype,
1479 mac );
1480
1481 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001482 }
1483 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1486 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1487 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001488 {
Hanno Becker992b6872017-11-09 18:57:39 +00001489 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1492 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1493 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1494 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001495 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001496 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001498
1499 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001500 }
1501 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001502#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001506 }
1507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001509 ssl->out_msg + ssl->out_msglen,
1510 ssl->transform_out->maclen );
1511
1512 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001513 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001514 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001515#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001517 /*
1518 * Encrypt
1519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1521 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001523 int ret;
1524 size_t olen = 0;
1525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 "including %d bytes of padding",
1528 ssl->out_msglen, 0 ) );
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001531 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001532 ssl->transform_out->ivlen,
1533 ssl->out_msg, ssl->out_msglen,
1534 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001537 return( ret );
1538 }
1539
1540 if( ssl->out_msglen != olen )
1541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 }
Paul Bakker68884e32013-01-07 18:20:04 +01001546 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001548#if defined(MBEDTLS_GCM_C) || \
1549 defined(MBEDTLS_CCM_C) || \
1550 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001552 mode == MBEDTLS_MODE_CCM ||
1553 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001554 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001555 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001556 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001557 unsigned char *enc_msg;
1558 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001559 unsigned char iv[12];
1560 mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001561 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001562
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001563 /*
1564 * Prepare additional authenticated data
1565 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001566 memcpy( add_data, ssl->out_ctr, 8 );
1567 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001569 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001570 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1571 add_data[12] = ssl->out_msglen & 0xFF;
1572
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001573 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001574
Paul Bakker68884e32013-01-07 18:20:04 +01001575 /*
1576 * Generate IV
1577 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001578 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1579 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001580 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001581 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1582 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1583 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1584
1585 }
1586 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1587 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001588 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001589 unsigned char i;
1590
1591 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1592
1593 for( i = 0; i < 8; i++ )
1594 iv[i+4] ^= ssl->out_ctr[i];
1595 }
1596 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001597 {
1598 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001601 }
1602
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001603 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1604 iv, transform->ivlen );
1605 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1606 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001607
Paul Bakker68884e32013-01-07 18:20:04 +01001608 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001609 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001610 */
1611 enc_msg = ssl->out_msg;
1612 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001613 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001616 "including 0 bytes of padding",
1617 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001618
Paul Bakker68884e32013-01-07 18:20:04 +01001619 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001620 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001621 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001622 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1623 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001624 add_data, 13,
1625 enc_msg, enc_msglen,
1626 enc_msg, &olen,
Hanno Becker8759e162017-12-27 21:34:08 +00001627 enc_msg + enc_msglen,
1628 transform->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
Hanno Becker8759e162017-12-27 21:34:08 +00001640 ssl->out_msglen += ssl->transform_out->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001641 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001642
Hanno Becker8759e162017-12-27 21:34:08 +00001643 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen,
1644 ssl->transform_out->taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001645 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1648#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001649 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001651 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001652 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001654 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001655
Paul Bakker48916f92012-09-16 19:57:18 +00001656 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1657 ssl->transform_out->ivlen;
1658 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 padlen = 0;
1660
1661 for( i = 0; i <= padlen; i++ )
1662 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1663
1664 ssl->out_msglen += padlen + 1;
1665
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001666 enc_msglen = ssl->out_msglen;
1667 enc_msg = ssl->out_msg;
1668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001670 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001671 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1672 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001675 {
1676 /*
1677 * Generate IV
1678 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001679 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001680 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001681 if( ret != 0 )
1682 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001683
Paul Bakker92be97b2013-01-02 17:30:03 +01001684 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001685 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001686
1687 /*
1688 * Fix pointer positions and message length with added IV
1689 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001690 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001691 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001692 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001697 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001698 ssl->out_msglen, ssl->transform_out->ivlen,
1699 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001702 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001703 ssl->transform_out->ivlen,
1704 enc_msg, enc_msglen,
1705 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001708 return( ret );
1709 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001710
Paul Bakkercca5b812013-08-31 17:40:26 +02001711 if( enc_msglen != olen )
1712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1714 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001715 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1718 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001719 {
1720 /*
1721 * Save IV in SSL3 and TLS1
1722 */
1723 memcpy( ssl->transform_out->iv_enc,
1724 ssl->transform_out->cipher_ctx_enc.iv,
1725 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001727#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001730 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001731 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001732 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1733
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001734 /*
1735 * MAC(MAC_write_key, seq_num +
1736 * TLSCipherText.type +
1737 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001738 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001739 * IV + // except for TLS 1.0
1740 * ENC(content + padding + padding_length));
1741 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001742 unsigned char pseudo_hdr[13];
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001745
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001746 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1747 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001748 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1749 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1754 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001755 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001756 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001758
Hanno Becker3d8c9072018-01-05 16:24:22 +00001759 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1760 ssl->transform_out->maclen );
1761
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001763 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001764 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001767 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001769 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001775 /* Make extra sure authentication was performed, exactly once */
1776 if( auth_done != 1 )
1777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001780 }
1781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
1784 return( 0 );
1785}
1786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001790 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001791#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001792 size_t padlen = 0, correct = 1;
1793#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001797 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001801 }
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001804
Paul Bakker48916f92012-09-16 19:57:18 +00001805 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001808 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 }
1811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1813 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001814 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001815 int ret;
1816 size_t olen = 0;
1817
Paul Bakker68884e32013-01-07 18:20:04 +01001818 padlen = 0;
1819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001821 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001822 ssl->transform_in->ivlen,
1823 ssl->in_msg, ssl->in_msglen,
1824 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001827 return( ret );
1828 }
1829
1830 if( ssl->in_msglen != olen )
1831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1833 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 }
Paul Bakker68884e32013-01-07 18:20:04 +01001836 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001838#if defined(MBEDTLS_GCM_C) || \
1839 defined(MBEDTLS_CCM_C) || \
1840 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001842 mode == MBEDTLS_MODE_CCM ||
1843 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001844 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001845 int ret;
1846 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001847 unsigned char *dec_msg;
1848 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001849 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001850 unsigned char iv[12];
1851 mbedtls_ssl_transform *transform = ssl->transform_in;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001852 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001853
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001854 /*
1855 * Compute and update sizes
1856 */
Hanno Becker8759e162017-12-27 21:34:08 +00001857 if( ssl->in_msglen < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001860 "+ taglen (%d)", ssl->in_msglen,
Hanno Becker8759e162017-12-27 21:34:08 +00001861 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001863 }
Hanno Becker8759e162017-12-27 21:34:08 +00001864 dec_msglen = ssl->in_msglen - explicit_iv_len - transform->taglen;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001865
Paul Bakker68884e32013-01-07 18:20:04 +01001866 dec_msg = ssl->in_msg;
1867 dec_msg_result = ssl->in_msg;
1868 ssl->in_msglen = dec_msglen;
1869
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001870 /*
1871 * Prepare additional authenticated data
1872 */
Paul Bakker68884e32013-01-07 18:20:04 +01001873 memcpy( add_data, ssl->in_ctr, 8 );
1874 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001876 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001877 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1878 add_data[12] = ssl->in_msglen & 0xFF;
1879
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001880 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001881
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001882 /*
1883 * Prepare IV
1884 */
1885 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1886 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001887 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001888 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1889 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001891 }
1892 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1893 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001894 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001895 unsigned char i;
1896
1897 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1898
1899 for( i = 0; i < 8; i++ )
1900 iv[i+4] ^= ssl->in_ctr[i];
1901 }
1902 else
1903 {
1904 /* Reminder if we ever add an AEAD mode with a different size */
1905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1907 }
1908
1909 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker8759e162017-12-27 21:34:08 +00001910 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen,
1911 transform->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,
Hanno Becker8759e162017-12-27 21:34:08 +00001921 dec_msg + dec_msglen,
1922 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1927 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001928
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001929 return( ret );
1930 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001931 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001932
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001933 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001937 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1941#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001942 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 {
Paul Bakker45829992013-01-03 14:52:21 +01001945 /*
1946 * Decrypt and check the padding
1947 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001948 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001949 unsigned char *dec_msg;
1950 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001951 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001952 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001953 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001954
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 /*
Paul Bakker45829992013-01-03 14:52:21 +01001956 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1959 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001960 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001961#endif
Paul Bakker45829992013-01-03 14:52:21 +01001962
1963 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1964 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001967 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1968 ssl->transform_in->ivlen,
1969 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001971 }
1972
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001973 dec_msglen = ssl->in_msglen;
1974 dec_msg = ssl->in_msg;
1975 dec_msg_result = ssl->in_msg;
1976
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 /*
1978 * Authenticate before decrypt if enabled
1979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1981 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001982 {
Hanno Becker992b6872017-11-09 18:57:39 +00001983 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001984 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001987
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001988 dec_msglen -= ssl->transform_in->maclen;
1989 ssl->in_msglen -= ssl->transform_in->maclen;
1990
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001991 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1992 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1993 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1994 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1999 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002000 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002001 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002005 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002006 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002007 ssl->transform_in->maclen );
2008
Hanno Becker992b6872017-11-09 18:57:39 +00002009 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2010 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002015 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002016 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002017 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002019
2020 /*
2021 * Check length sanity
2022 */
2023 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002026 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002028 }
2029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002031 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002032 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002035 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002036 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002037 dec_msglen -= ssl->transform_in->ivlen;
2038 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002039
Paul Bakker48916f92012-09-16 19:57:18 +00002040 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002041 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002046 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002047 ssl->transform_in->ivlen,
2048 dec_msg, dec_msglen,
2049 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002052 return( ret );
2053 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002054
Paul Bakkercca5b812013-08-31 17:40:26 +02002055 if( dec_msglen != olen )
2056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002059 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2062 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002063 {
2064 /*
2065 * Save IV in SSL3 and TLS1
2066 */
2067 memcpy( ssl->transform_in->iv_dec,
2068 ssl->transform_in->cipher_ctx_dec.iv,
2069 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002072
2073 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002074
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002075 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002076 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078#if defined(MBEDTLS_SSL_DEBUG_ALL)
2079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002080 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002081#endif
Paul Bakker45829992013-01-03 14:52:21 +01002082 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002083 correct = 0;
2084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_SSL_PROTO_SSL3)
2087 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
Paul Bakker48916f92012-09-16 19:57:18 +00002089 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_SSL_DEBUG_ALL)
2092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002094 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002095#endif
Paul Bakker45829992013-01-03 14:52:21 +01002096 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098 }
2099 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2101#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2102 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2103 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 {
2105 /*
Paul Bakker45829992013-01-03 14:52:21 +01002106 * TLSv1+: always check the padding up to the first failure
2107 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002109 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002110 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002111 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002112
Paul Bakker956c9e02013-12-19 14:42:28 +01002113 /*
2114 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002115 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002116 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002117 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002118 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002119 *
2120 * In both cases we reset padding_idx to a safe value (0) to
2121 * prevent out-of-buffer reads.
2122 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002123 correct &= ( padlen <= ssl->in_msglen );
2124 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002125 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002126
2127 padding_idx *= correct;
2128
Angus Grattonb512bc12018-06-19 15:57:50 +10002129 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002130 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002131 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002132 pad_count += real_count *
2133 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2134 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002135
2136 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002139 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002141#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002142 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2146 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002150 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002151
2152 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002154 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002156 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002161
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002162#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002165#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
2167 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002168 * Authenticate if not done yet.
2169 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002171#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002172 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002173 {
Hanno Becker992b6872017-11-09 18:57:39 +00002174 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002175
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002176 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002178 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2179 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181#if defined(MBEDTLS_SSL_PROTO_SSL3)
2182 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002183 {
2184 ssl_mac( &ssl->transform_in->md_ctx_dec,
2185 ssl->transform_in->mac_dec,
2186 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002187 ssl->in_ctr, ssl->in_msgtype,
2188 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002189 }
2190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2192#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2193 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2194 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002195 {
2196 /*
2197 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002198 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002199 *
2200 * Known timing attacks:
2201 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2202 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002203 * To compensate for different timings for the MAC calculation
2204 * depending on how much padding was removed (which is determined
2205 * by padlen), process extra_run more blocks through the hash
2206 * function.
2207 *
2208 * The formula in the paper is
2209 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2210 * where L1 is the size of the header plus the decrypted message
2211 * plus CBC padding and L2 is the size of the header plus the
2212 * decrypted message. This is for an underlying hash function
2213 * with 64-byte blocks.
2214 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2215 * correctly. We round down instead of up, so -56 is the correct
2216 * value for our calculations instead of -55.
2217 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002218 * Repeat the formula rather than defining a block_size variable.
2219 * This avoids requiring division by a variable at runtime
2220 * (which would be marginally less efficient and would require
2221 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002222 */
2223 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002224
2225 /*
2226 * The next two sizes are the minimum and maximum values of
2227 * in_msglen over all padlen values.
2228 *
2229 * They're independent of padlen, since we previously did
2230 * in_msglen -= padlen.
2231 *
2232 * Note that max_len + maclen is never more than the buffer
2233 * length, as we previously did in_msglen -= maclen too.
2234 */
2235 const size_t max_len = ssl->in_msglen + padlen;
2236 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2237
Hanno Becker8759e162017-12-27 21:34:08 +00002238 switch( ssl->handshake->ciphersuite_info->mac )
Gilles Peskine20b44082018-05-29 14:06:49 +02002239 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002240#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2241 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002242 case MBEDTLS_MD_MD5:
2243 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002244 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002245 /* 8 bytes of message size, 64-byte compression blocks */
2246 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2247 ( 13 + ssl->in_msglen + 8 ) / 64;
2248 break;
2249#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002250#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002251 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002252 /* 16 bytes of message size, 128-byte compression blocks */
2253 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2254 ( 13 + ssl->in_msglen + 16 ) / 128;
2255 break;
2256#endif
2257 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2260 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002261
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002262 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2265 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2266 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2267 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002268 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002269 /* Make sure we access everything even when padlen > 0. This
2270 * makes the synchronisation requirements for just-in-time
2271 * Prime+Probe attacks much tighter and hopefully impractical. */
2272 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002273 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002274
2275 /* Call mbedtls_md_process at least once due to cache attacks
2276 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002277 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002281
2282 /* Make sure we access all the memory that could contain the MAC,
2283 * before we check it in the next code block. This makes the
2284 * synchronisation requirements for just-in-time Prime+Probe
2285 * attacks much tighter and hopefully impractical. */
2286 ssl_read_memory( ssl->in_msg + min_len,
2287 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002288 }
2289 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2291 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002297#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002298 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2299 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2300 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
Hanno Becker992b6872017-11-09 18:57:39 +00002303 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2304 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#if defined(MBEDTLS_SSL_DEBUG_ALL)
2307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002308#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002309 correct = 0;
2310 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002312 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002313
2314 /*
2315 * Finally check the correct flag
2316 */
2317 if( correct == 0 )
2318 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002319#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002320
2321 /* Make extra sure authentication was performed, exactly once */
2322 if( auth_done != 1 )
2323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
2328 if( ssl->in_msglen == 0 )
2329 {
Angus Gratton34817922018-06-19 15:58:22 +10002330#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2331 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2332 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2333 {
2334 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2336 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2337 }
2338#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2339
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 ssl->nb_zero++;
2341
2342 /*
2343 * Three or more empty messages may be a DoS attack
2344 * (excessive CPU consumption).
2345 */
2346 if( ssl->nb_zero > 3 )
2347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 }
2352 }
2353 else
2354 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002357 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002358 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002359 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002360 }
2361 else
2362#endif
2363 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002364 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002365 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2366 if( ++ssl->in_ctr[i - 1] != 0 )
2367 break;
2368
2369 /* The loop goes to its end iff the counter is wrapping */
2370 if( i == ssl_ep_len( ssl ) )
2371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2373 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002374 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002375 }
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
2379 return( 0 );
2380}
2381
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002382#undef MAC_NONE
2383#undef MAC_PLAINTEXT
2384#undef MAC_CIPHERTEXT
2385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002387/*
2388 * Compression/decompression functions
2389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391{
2392 int ret;
2393 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002394 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002395 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002396 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002400 if( len_pre == 0 )
2401 return( 0 );
2402
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403 memcpy( msg_pre, ssl->out_msg, len_pre );
2404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002406 ssl->out_msglen ) );
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409 ssl->out_msg, ssl->out_msglen );
2410
Paul Bakker48916f92012-09-16 19:57:18 +00002411 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2412 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2413 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002414 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415
Paul Bakker48916f92012-09-16 19:57:18 +00002416 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417 if( ret != Z_OK )
2418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2420 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002421 }
2422
Angus Grattond8213d02016-05-25 20:56:48 +10002423 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002424 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 ssl->out_msglen ) );
2428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002430 ssl->out_msg, ssl->out_msglen );
2431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002433
2434 return( 0 );
2435}
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438{
2439 int ret;
2440 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002441 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002442 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002443 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002447 if( len_pre == 0 )
2448 return( 0 );
2449
Paul Bakker2770fbd2012-07-03 13:30:23 +00002450 memcpy( msg_pre, ssl->in_msg, len_pre );
2451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002453 ssl->in_msglen ) );
2454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002456 ssl->in_msg, ssl->in_msglen );
2457
Paul Bakker48916f92012-09-16 19:57:18 +00002458 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2459 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2460 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002461 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002462 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002463
Paul Bakker48916f92012-09-16 19:57:18 +00002464 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002465 if( ret != Z_OK )
2466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2468 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002469 }
2470
Angus Grattond8213d02016-05-25 20:56:48 +10002471 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002472 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002475 ssl->in_msglen ) );
2476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002478 ssl->in_msg, ssl->in_msglen );
2479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002481
2482 return( 0 );
2483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2487static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SSL_PROTO_DTLS)
2490static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002491{
2492 /* If renegotiation is not enforced, retransmit until we would reach max
2493 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002494 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002495 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002496 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002497 unsigned char doublings = 1;
2498
2499 while( ratio != 0 )
2500 {
2501 ++doublings;
2502 ratio >>= 1;
2503 }
2504
2505 if( ++ssl->renego_records_seen > doublings )
2506 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002508 return( 0 );
2509 }
2510 }
2511
2512 return( ssl_write_hello_request( ssl ) );
2513}
2514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002516
Paul Bakker5121ce52009-01-03 21:22:43 +00002517/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002518 * Fill the input message buffer by appending data to it.
2519 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002520 *
2521 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2522 * available (from this read and/or a previous one). Otherwise, an error code
2523 * is returned (possibly EOF or WANT_READ).
2524 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002525 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2526 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2527 * since we always read a whole datagram at once.
2528 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002529 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002530 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002533{
Paul Bakker23986e52011-04-24 08:57:21 +00002534 int ret;
2535 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002539 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002542 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002544 }
2545
Angus Grattond8213d02016-05-25 20:56:48 +10002546 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002550 }
2551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002553 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002555 uint32_t timeout;
2556
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002557 /* Just to be sure */
2558 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2559 {
2560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2561 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2563 }
2564
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002565 /*
2566 * The point is, we need to always read a full datagram at once, so we
2567 * sometimes read more then requested, and handle the additional data.
2568 * It could be the rest of the current record (while fetching the
2569 * header) and/or some other records in the same datagram.
2570 */
2571
2572 /*
2573 * Move to the next record in the already read datagram if applicable
2574 */
2575 if( ssl->next_record_offset != 0 )
2576 {
2577 if( ssl->in_left < ssl->next_record_offset )
2578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2580 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002581 }
2582
2583 ssl->in_left -= ssl->next_record_offset;
2584
2585 if( ssl->in_left != 0 )
2586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002588 ssl->next_record_offset ) );
2589 memmove( ssl->in_hdr,
2590 ssl->in_hdr + ssl->next_record_offset,
2591 ssl->in_left );
2592 }
2593
2594 ssl->next_record_offset = 0;
2595 }
2596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002599
2600 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002601 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002602 */
2603 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002606 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002607 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002608
2609 /*
2610 * A record can't be split accross datagrams. If we need to read but
2611 * are not at the beginning of a new record, the caller did something
2612 * wrong.
2613 */
2614 if( ssl->in_left != 0 )
2615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002618 }
2619
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002620 /*
2621 * Don't even try to read if time's out already.
2622 * This avoids by-passing the timer when repeatedly receiving messages
2623 * that will end up being dropped.
2624 */
2625 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002626 {
2627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002628 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002629 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002630 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002631 {
Angus Grattond8213d02016-05-25 20:56:48 +10002632 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002635 timeout = ssl->handshake->retransmit_timeout;
2636 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002637 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002640
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002641 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002642 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2643 timeout );
2644 else
2645 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002648
2649 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002651 }
2652
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002653 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002656 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002659 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002660 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002663 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002664 }
2665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002669 return( ret );
2670 }
2671
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002672 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002673 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002675 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002677 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002678 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002681 return( ret );
2682 }
2683
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002684 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002685 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002687 }
2688
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 if( ret < 0 )
2690 return( ret );
2691
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002692 ssl->in_left = ret;
2693 }
2694 else
2695#endif
2696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002698 ssl->in_left, nb_want ) );
2699
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002700 while( ssl->in_left < nb_want )
2701 {
2702 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002703
2704 if( ssl_check_timer( ssl ) != 0 )
2705 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2706 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002707 {
2708 if( ssl->f_recv_timeout != NULL )
2709 {
2710 ret = ssl->f_recv_timeout( ssl->p_bio,
2711 ssl->in_hdr + ssl->in_left, len,
2712 ssl->conf->read_timeout );
2713 }
2714 else
2715 {
2716 ret = ssl->f_recv( ssl->p_bio,
2717 ssl->in_hdr + ssl->in_left, len );
2718 }
2719 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002722 ssl->in_left, nb_want ) );
2723 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002724
2725 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002727
2728 if( ret < 0 )
2729 return( ret );
2730
mohammad160352aecb92018-03-28 23:41:40 -07002731 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002732 {
Darryl Green11999bb2018-03-13 15:22:58 +00002733 MBEDTLS_SSL_DEBUG_MSG( 1,
2734 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002735 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2737 }
2738
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002739 ssl->in_left += ret;
2740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
2742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
2745 return( 0 );
2746}
2747
2748/*
2749 * Flush any data not yet written
2750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002752{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002753 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002754 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002758 if( ssl->f_send == NULL )
2759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002761 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002763 }
2764
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002765 /* Avoid incrementing counter if data is flushed */
2766 if( ssl->out_left == 0 )
2767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002769 return( 0 );
2770 }
2771
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 while( ssl->out_left > 0 )
2773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2775 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Hanno Becker2b1e3542018-08-06 11:19:13 +01002777 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002778 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
2782 if( ret <= 0 )
2783 return( ret );
2784
mohammad160352aecb92018-03-28 23:41:40 -07002785 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002786 {
Darryl Green11999bb2018-03-13 15:22:58 +00002787 MBEDTLS_SSL_DEBUG_MSG( 1,
2788 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002789 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002790 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2791 }
2792
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 ssl->out_left -= ret;
2794 }
2795
Hanno Becker2b1e3542018-08-06 11:19:13 +01002796#if defined(MBEDTLS_SSL_PROTO_DTLS)
2797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002798 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002799 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002800 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002801 else
2802#endif
2803 {
2804 ssl->out_hdr = ssl->out_buf + 8;
2805 }
2806 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
2810 return( 0 );
2811}
2812
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002813/*
2814 * Functions to handle the DTLS retransmission state machine
2815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002817/*
2818 * Append current handshake message to current outgoing flight
2819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2824 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2825 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002826
2827 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002828 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002829 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002832 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002833 }
2834
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002835 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002836 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002839 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002840 }
2841
2842 /* Copy current handshake message with headers */
2843 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2844 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002845 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002846 msg->next = NULL;
2847
2848 /* Append to the current flight */
2849 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002850 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002851 else
2852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002854 while( cur->next != NULL )
2855 cur = cur->next;
2856 cur->next = msg;
2857 }
2858
Hanno Becker3b235902018-08-06 09:54:53 +01002859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002860 return( 0 );
2861}
2862
2863/*
2864 * Free the current flight of handshake messages
2865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 mbedtls_ssl_flight_item *cur = flight;
2869 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002870
2871 while( cur != NULL )
2872 {
2873 next = cur->next;
2874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 mbedtls_free( cur->p );
2876 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002877
2878 cur = next;
2879 }
2880}
2881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2883static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002884#endif
2885
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002886/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002887 * Swap transform_out and out_ctr with the alternative ones
2888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002892 unsigned char tmp_out_ctr[8];
2893
2894 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002897 return;
2898 }
2899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002901
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002902 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002903 tmp_transform = ssl->transform_out;
2904 ssl->transform_out = ssl->handshake->alt_transform_out;
2905 ssl->handshake->alt_transform_out = tmp_transform;
2906
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002907 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002908 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2909 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002910 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002911
2912 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002913 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2916 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2921 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002922 }
2923 }
2924#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002925}
2926
2927/*
2928 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002929 */
2930int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2931{
2932 int ret = 0;
2933
2934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2935
2936 ret = mbedtls_ssl_flight_transmit( ssl );
2937
2938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2939
2940 return( ret );
2941}
2942
2943/*
2944 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002945 *
2946 * Need to remember the current message in case flush_output returns
2947 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002948 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002949 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002950int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002951{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002952 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002956 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002958
2959 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002960 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002961 ssl_swap_epochs( ssl );
2962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002964 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002965
2966 while( ssl->handshake->cur_msg != NULL )
2967 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002968 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002969 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002970
Hanno Beckere1dcb032018-08-17 16:47:58 +01002971 int const is_finished =
2972 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2973 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2974
Hanno Becker04da1892018-08-14 13:22:10 +01002975 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2976 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2977
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002978 /* Swap epochs before sending Finished: we can't do it after
2979 * sending ChangeCipherSpec, in case write returns WANT_READ.
2980 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002981 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002982 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002984 ssl_swap_epochs( ssl );
2985 }
2986
Hanno Becker67bc7c32018-08-06 11:33:50 +01002987 ret = ssl_get_remaining_payload_in_datagram( ssl );
2988 if( ret < 0 )
2989 return( ret );
2990 max_frag_len = (size_t) ret;
2991
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002992 /* CCS is copied as is, while HS messages may need fragmentation */
2993 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2994 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002995 if( max_frag_len == 0 )
2996 {
2997 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2998 return( ret );
2999
3000 continue;
3001 }
3002
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003003 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003004 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003005 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003006
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003007 /* Update position inside current message */
3008 ssl->handshake->cur_msg_p += cur->len;
3009 }
3010 else
3011 {
3012 const unsigned char * const p = ssl->handshake->cur_msg_p;
3013 const size_t hs_len = cur->len - 12;
3014 const size_t frag_off = p - ( cur->p + 12 );
3015 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003016 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003017
Hanno Beckere1dcb032018-08-17 16:47:58 +01003018 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003019 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003020 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003021 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003022
Hanno Becker67bc7c32018-08-06 11:33:50 +01003023 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3024 return( ret );
3025
3026 continue;
3027 }
3028 max_hs_frag_len = max_frag_len - 12;
3029
3030 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3031 max_hs_frag_len : rem_len;
3032
3033 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003034 {
3035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003036 (unsigned) cur_hs_frag_len,
3037 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003038 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003039
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003040 /* Messages are stored with handshake headers as if not fragmented,
3041 * copy beginning of headers then fill fragmentation fields.
3042 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3043 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003044
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003045 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3046 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3047 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3048
Hanno Becker67bc7c32018-08-06 11:33:50 +01003049 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3050 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3051 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003052
3053 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3054
Hanno Becker3f7b9732018-08-28 09:53:25 +01003055 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003056 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3057 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003058 ssl->out_msgtype = cur->type;
3059
3060 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003061 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003062 }
3063
3064 /* If done with the current message move to the next one if any */
3065 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3066 {
3067 if( cur->next != NULL )
3068 {
3069 ssl->handshake->cur_msg = cur->next;
3070 ssl->handshake->cur_msg_p = cur->next->p + 12;
3071 }
3072 else
3073 {
3074 ssl->handshake->cur_msg = NULL;
3075 ssl->handshake->cur_msg_p = NULL;
3076 }
3077 }
3078
3079 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003080 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003083 return( ret );
3084 }
3085 }
3086
Hanno Becker67bc7c32018-08-06 11:33:50 +01003087 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3088 return( ret );
3089
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003090 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3092 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003093 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003096 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3097 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003098
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003100
3101 return( 0 );
3102}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003103
3104/*
3105 * To be called when the last message of an incoming flight is received.
3106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003108{
3109 /* We won't need to resend that one any more */
3110 ssl_flight_free( ssl->handshake->flight );
3111 ssl->handshake->flight = NULL;
3112 ssl->handshake->cur_msg = NULL;
3113
3114 /* The next incoming flight will start with this msg_seq */
3115 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3116
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003117 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003118 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003119
Hanno Becker0271f962018-08-16 13:23:47 +01003120 /* Clear future message buffering structure. */
3121 ssl_buffering_free( ssl );
3122
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003123 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003124 ssl_set_timer( ssl, 0 );
3125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3127 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003130 }
3131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003133}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003134
3135/*
3136 * To be called when the last message of an outgoing flight is send.
3137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003139{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003140 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003141 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3144 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003147 }
3148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003152
Paul Bakker5121ce52009-01-03 21:22:43 +00003153/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003154 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003156
3157/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003158 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003159 *
3160 * - fill in handshake headers
3161 * - update handshake checksum
3162 * - DTLS: save message for resending
3163 * - then pass to the record layer
3164 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003165 * DTLS: except for HelloRequest, messages are only queued, and will only be
3166 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003167 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003168 * Inputs:
3169 * - ssl->out_msglen: 4 + actual handshake message len
3170 * (4 is the size of handshake headers for TLS)
3171 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3172 * - ssl->out_msg + 4: the handshake message body
3173 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003174 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003175 * - ssl->out_msglen: the length of the record contents
3176 * (including handshake headers but excluding record headers)
3177 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003178 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003179int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003180{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003181 int ret;
3182 const size_t hs_len = ssl->out_msglen - 4;
3183 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3186
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003187 /*
3188 * Sanity checks
3189 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003190 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003191 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3192 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003193 /* In SSLv3, the client might send a NoCertificate alert. */
3194#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3195 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3196 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3197 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3198#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3199 {
3200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3202 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003205 /* Whenever we send anything different from a
3206 * HelloRequest we should be in a handshake - double check. */
3207 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3208 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003209 ssl->handshake == NULL )
3210 {
3211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003217 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003219 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003222 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003223#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003224
Hanno Beckerb50a2532018-08-06 11:52:54 +01003225 /* Double-check that we did not exceed the bounds
3226 * of the outgoing record buffer.
3227 * This should never fail as the various message
3228 * writing functions must obey the bounds of the
3229 * outgoing record buffer, but better be safe.
3230 *
3231 * Note: We deliberately do not check for the MTU or MFL here.
3232 */
3233 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3234 {
3235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3236 "size %u, maximum %u",
3237 (unsigned) ssl->out_msglen,
3238 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3240 }
3241
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003242 /*
3243 * Fill handshake headers
3244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003247 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3248 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3249 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003251 /*
3252 * DTLS has additional fields in the Handshake layer,
3253 * between the length field and the actual payload:
3254 * uint16 message_seq;
3255 * uint24 fragment_offset;
3256 * uint24 fragment_length;
3257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003259 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003260 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003261 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003262 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003263 {
3264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3265 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003266 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003267 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3269 }
3270
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003271 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003272 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003273
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003274 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003275 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003276 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003277 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3278 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3279 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003280 }
3281 else
3282 {
3283 ssl->out_msg[4] = 0;
3284 ssl->out_msg[5] = 0;
3285 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003286
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003287 /* Handshake hashes are computed without fragmentation,
3288 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003289 memset( ssl->out_msg + 6, 0x00, 3 );
3290 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003291 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003293
Hanno Becker0207e532018-08-28 10:28:28 +01003294 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003295 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3296 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 }
3298
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003299 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003301 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003302 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3303 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003304 {
3305 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308 return( ret );
3309 }
3310 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003311 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003312#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003313 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003314 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003315 {
3316 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3317 return( ret );
3318 }
3319 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003320
3321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3322
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003323 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003324}
3325
3326/*
3327 * Record layer functions
3328 */
3329
3330/*
3331 * Write current record.
3332 *
3333 * Uses:
3334 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3335 * - ssl->out_msglen: length of the record content (excl headers)
3336 * - ssl->out_msg: record content
3337 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003338int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003339{
3340 int ret, done = 0;
3341 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003342 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003343
3344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003347 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003349 {
3350 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003353 return( ret );
3354 }
3355
3356 len = ssl->out_msglen;
3357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3361 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 ret = mbedtls_ssl_hw_record_write( ssl );
3366 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3369 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003370 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003371
3372 if( ret == 0 )
3373 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003376 if( !done )
3377 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003378 unsigned i;
3379 size_t protected_record_size;
3380
Paul Bakker05ef8352012-05-08 09:17:57 +00003381 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003383 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003384
Hanno Becker19859472018-08-06 09:40:20 +01003385 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003386 ssl->out_len[0] = (unsigned char)( len >> 8 );
3387 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003388
Paul Bakker48916f92012-09-16 19:57:18 +00003389 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003390 {
3391 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003394 return( ret );
3395 }
3396
3397 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003398 ssl->out_len[0] = (unsigned char)( len >> 8 );
3399 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003400 }
3401
Hanno Becker2b1e3542018-08-06 11:19:13 +01003402 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3403
3404#if defined(MBEDTLS_SSL_PROTO_DTLS)
3405 /* In case of DTLS, double-check that we don't exceed
3406 * the remaining space in the datagram. */
3407 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3408 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003409 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003410 if( ret < 0 )
3411 return( ret );
3412
3413 if( protected_record_size > (size_t) ret )
3414 {
3415 /* Should never happen */
3416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3417 }
3418 }
3419#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003422 "version = [%d:%d], msglen = %d",
3423 ssl->out_hdr[0], ssl->out_hdr[1],
3424 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003427 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003428
3429 ssl->out_left += protected_record_size;
3430 ssl->out_hdr += protected_record_size;
3431 ssl_update_out_pointers( ssl, ssl->transform_out );
3432
Hanno Becker04484622018-08-06 09:49:38 +01003433 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3434 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3435 break;
3436
3437 /* The loop goes to its end iff the counter is wrapping */
3438 if( i == ssl_ep_len( ssl ) )
3439 {
3440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3441 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 }
3444
Hanno Becker67bc7c32018-08-06 11:33:50 +01003445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3447 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003448 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003449 size_t remaining;
3450 ret = ssl_get_remaining_payload_in_datagram( ssl );
3451 if( ret < 0 )
3452 {
3453 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3454 ret );
3455 return( ret );
3456 }
3457
3458 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003459 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003460 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003461 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003462 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003463 else
3464 {
Hanno Becker513815a2018-08-20 11:56:09 +01003465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003466 }
3467 }
3468#endif /* MBEDTLS_SSL_PROTO_DTLS */
3469
3470 if( ( flush == SSL_FORCE_FLUSH ) &&
3471 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 return( ret );
3475 }
3476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003478
3479 return( 0 );
3480}
3481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003483
3484static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3485{
3486 if( ssl->in_msglen < ssl->in_hslen ||
3487 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3488 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3489 {
3490 return( 1 );
3491 }
3492 return( 0 );
3493}
Hanno Becker44650b72018-08-16 12:51:11 +01003494
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003495static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003496{
3497 return( ( ssl->in_msg[9] << 16 ) |
3498 ( ssl->in_msg[10] << 8 ) |
3499 ssl->in_msg[11] );
3500}
3501
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003502static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003503{
3504 return( ( ssl->in_msg[6] << 16 ) |
3505 ( ssl->in_msg[7] << 8 ) |
3506 ssl->in_msg[8] );
3507}
3508
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003509static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003510{
3511 uint32_t msg_len, frag_off, frag_len;
3512
3513 msg_len = ssl_get_hs_total_len( ssl );
3514 frag_off = ssl_get_hs_frag_off( ssl );
3515 frag_len = ssl_get_hs_frag_len( ssl );
3516
3517 if( frag_off > msg_len )
3518 return( -1 );
3519
3520 if( frag_len > msg_len - frag_off )
3521 return( -1 );
3522
3523 if( frag_len + 12 > ssl->in_msglen )
3524 return( -1 );
3525
3526 return( 0 );
3527}
3528
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003529/*
3530 * Mark bits in bitmask (used for DTLS HS reassembly)
3531 */
3532static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3533{
3534 unsigned int start_bits, end_bits;
3535
3536 start_bits = 8 - ( offset % 8 );
3537 if( start_bits != 8 )
3538 {
3539 size_t first_byte_idx = offset / 8;
3540
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003541 /* Special case */
3542 if( len <= start_bits )
3543 {
3544 for( ; len != 0; len-- )
3545 mask[first_byte_idx] |= 1 << ( start_bits - len );
3546
3547 /* Avoid potential issues with offset or len becoming invalid */
3548 return;
3549 }
3550
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003551 offset += start_bits; /* Now offset % 8 == 0 */
3552 len -= start_bits;
3553
3554 for( ; start_bits != 0; start_bits-- )
3555 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3556 }
3557
3558 end_bits = len % 8;
3559 if( end_bits != 0 )
3560 {
3561 size_t last_byte_idx = ( offset + len ) / 8;
3562
3563 len -= end_bits; /* Now len % 8 == 0 */
3564
3565 for( ; end_bits != 0; end_bits-- )
3566 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3567 }
3568
3569 memset( mask + offset / 8, 0xFF, len / 8 );
3570}
3571
3572/*
3573 * Check that bitmask is full
3574 */
3575static int ssl_bitmask_check( unsigned char *mask, size_t len )
3576{
3577 size_t i;
3578
3579 for( i = 0; i < len / 8; i++ )
3580 if( mask[i] != 0xFF )
3581 return( -1 );
3582
3583 for( i = 0; i < len % 8; i++ )
3584 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3585 return( -1 );
3586
3587 return( 0 );
3588}
3589
Hanno Becker56e205e2018-08-16 09:06:12 +01003590/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003591static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003592 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003593{
Hanno Becker56e205e2018-08-16 09:06:12 +01003594 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003595
Hanno Becker56e205e2018-08-16 09:06:12 +01003596 alloc_len = 12; /* Handshake header */
3597 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003598
Hanno Beckerd07df862018-08-16 09:14:58 +01003599 if( add_bitmap )
3600 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003601
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003602 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003603}
Hanno Becker56e205e2018-08-16 09:06:12 +01003604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003606
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003607static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003608{
3609 return( ( ssl->in_msg[1] << 16 ) |
3610 ( ssl->in_msg[2] << 8 ) |
3611 ssl->in_msg[3] );
3612}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003613
Simon Butcher99000142016-10-13 17:21:01 +01003614int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003619 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003621 }
3622
Hanno Becker12555c62018-08-16 12:47:53 +01003623 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003626 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003627 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003630 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003631 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003632 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003633 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003634
Hanno Becker44650b72018-08-16 12:51:11 +01003635 if( ssl_check_hs_header( ssl ) != 0 )
3636 {
3637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3638 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3639 }
3640
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003641 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003642 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3643 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3644 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3645 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003646 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003647 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3648 {
3649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3650 recv_msg_seq,
3651 ssl->handshake->in_msg_seq ) );
3652 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3653 }
3654
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003655 /* Retransmit only on last message from previous flight, to avoid
3656 * too many retransmissions.
3657 * Besides, No sane server ever retransmits HelloVerifyRequest */
3658 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003662 "message_seq = %d, start_of_flight = %d",
3663 recv_msg_seq,
3664 ssl->handshake->in_flight_start_seq ) );
3665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003669 return( ret );
3670 }
3671 }
3672 else
3673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003675 "message_seq = %d, expected = %d",
3676 recv_msg_seq,
3677 ssl->handshake->in_msg_seq ) );
3678 }
3679
Hanno Becker90333da2017-10-10 11:27:13 +01003680 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003681 }
3682 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003683
Hanno Becker6d97ef52018-08-16 13:09:04 +01003684 /* Message reassembly is handled alongside buffering of future
3685 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003686 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003687 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003688 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003691 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003692 }
3693 }
3694 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003696 /* With TLS we don't handle fragmentation (for now) */
3697 if( ssl->in_msglen < ssl->in_hslen )
3698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3700 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003701 }
3702
Simon Butcher99000142016-10-13 17:21:01 +01003703 return( 0 );
3704}
3705
3706void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3707{
Hanno Becker0271f962018-08-16 13:23:47 +01003708 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003709
Hanno Becker0271f962018-08-16 13:23:47 +01003710 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003711 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003712 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003713 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003714
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003715 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003718 ssl->handshake != NULL )
3719 {
Hanno Becker0271f962018-08-16 13:23:47 +01003720 unsigned offset;
3721 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003722
Hanno Becker0271f962018-08-16 13:23:47 +01003723 /* Increment handshake sequence number */
3724 hs->in_msg_seq++;
3725
3726 /*
3727 * Clear up handshake buffering and reassembly structure.
3728 */
3729
3730 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003731 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003732
3733 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003734 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3735 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003736 offset++, hs_buf++ )
3737 {
3738 *hs_buf = *(hs_buf + 1);
3739 }
3740
3741 /* Create a fresh last entry */
3742 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003743 }
3744#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003745}
3746
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003747/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003748 * DTLS anti-replay: RFC 6347 4.1.2.6
3749 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003750 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3751 * Bit n is set iff record number in_window_top - n has been seen.
3752 *
3753 * Usually, in_window_top is the last record number seen and the lsb of
3754 * in_window is set. The only exception is the initial state (record number 0
3755 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3758static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003759{
3760 ssl->in_window_top = 0;
3761 ssl->in_window = 0;
3762}
3763
3764static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3765{
3766 return( ( (uint64_t) buf[0] << 40 ) |
3767 ( (uint64_t) buf[1] << 32 ) |
3768 ( (uint64_t) buf[2] << 24 ) |
3769 ( (uint64_t) buf[3] << 16 ) |
3770 ( (uint64_t) buf[4] << 8 ) |
3771 ( (uint64_t) buf[5] ) );
3772}
3773
3774/*
3775 * Return 0 if sequence number is acceptable, -1 otherwise
3776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003778{
3779 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3780 uint64_t bit;
3781
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003782 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003783 return( 0 );
3784
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003785 if( rec_seqnum > ssl->in_window_top )
3786 return( 0 );
3787
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003788 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003789
3790 if( bit >= 64 )
3791 return( -1 );
3792
3793 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3794 return( -1 );
3795
3796 return( 0 );
3797}
3798
3799/*
3800 * Update replay window on new validated record
3801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003803{
3804 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3805
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003806 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003807 return;
3808
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003809 if( rec_seqnum > ssl->in_window_top )
3810 {
3811 /* Update window_top and the contents of the window */
3812 uint64_t shift = rec_seqnum - ssl->in_window_top;
3813
3814 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003815 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003816 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003817 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003818 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003819 ssl->in_window |= 1;
3820 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003821
3822 ssl->in_window_top = rec_seqnum;
3823 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003824 else
3825 {
3826 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003827 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003828
3829 if( bit < 64 ) /* Always true, but be extra sure */
3830 ssl->in_window |= (uint64_t) 1 << bit;
3831 }
3832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003834
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003835#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003836/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003837static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3838
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003839/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003840 * Without any SSL context, check if a datagram looks like a ClientHello with
3841 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003842 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003843 *
3844 * - if cookie is valid, return 0
3845 * - if ClientHello looks superficially valid but cookie is not,
3846 * fill obuf and set olen, then
3847 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3848 * - otherwise return a specific error code
3849 */
3850static int ssl_check_dtls_clihlo_cookie(
3851 mbedtls_ssl_cookie_write_t *f_cookie_write,
3852 mbedtls_ssl_cookie_check_t *f_cookie_check,
3853 void *p_cookie,
3854 const unsigned char *cli_id, size_t cli_id_len,
3855 const unsigned char *in, size_t in_len,
3856 unsigned char *obuf, size_t buf_len, size_t *olen )
3857{
3858 size_t sid_len, cookie_len;
3859 unsigned char *p;
3860
3861 if( f_cookie_write == NULL || f_cookie_check == NULL )
3862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3863
3864 /*
3865 * Structure of ClientHello with record and handshake headers,
3866 * and expected values. We don't need to check a lot, more checks will be
3867 * done when actually parsing the ClientHello - skipping those checks
3868 * avoids code duplication and does not make cookie forging any easier.
3869 *
3870 * 0-0 ContentType type; copied, must be handshake
3871 * 1-2 ProtocolVersion version; copied
3872 * 3-4 uint16 epoch; copied, must be 0
3873 * 5-10 uint48 sequence_number; copied
3874 * 11-12 uint16 length; (ignored)
3875 *
3876 * 13-13 HandshakeType msg_type; (ignored)
3877 * 14-16 uint24 length; (ignored)
3878 * 17-18 uint16 message_seq; copied
3879 * 19-21 uint24 fragment_offset; copied, must be 0
3880 * 22-24 uint24 fragment_length; (ignored)
3881 *
3882 * 25-26 ProtocolVersion client_version; (ignored)
3883 * 27-58 Random random; (ignored)
3884 * 59-xx SessionID session_id; 1 byte len + sid_len content
3885 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3886 * ...
3887 *
3888 * Minimum length is 61 bytes.
3889 */
3890 if( in_len < 61 ||
3891 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3892 in[3] != 0 || in[4] != 0 ||
3893 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3894 {
3895 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3896 }
3897
3898 sid_len = in[59];
3899 if( sid_len > in_len - 61 )
3900 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3901
3902 cookie_len = in[60 + sid_len];
3903 if( cookie_len > in_len - 60 )
3904 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3905
3906 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3907 cli_id, cli_id_len ) == 0 )
3908 {
3909 /* Valid cookie */
3910 return( 0 );
3911 }
3912
3913 /*
3914 * If we get here, we've got an invalid cookie, let's prepare HVR.
3915 *
3916 * 0-0 ContentType type; copied
3917 * 1-2 ProtocolVersion version; copied
3918 * 3-4 uint16 epoch; copied
3919 * 5-10 uint48 sequence_number; copied
3920 * 11-12 uint16 length; olen - 13
3921 *
3922 * 13-13 HandshakeType msg_type; hello_verify_request
3923 * 14-16 uint24 length; olen - 25
3924 * 17-18 uint16 message_seq; copied
3925 * 19-21 uint24 fragment_offset; copied
3926 * 22-24 uint24 fragment_length; olen - 25
3927 *
3928 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3929 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3930 *
3931 * Minimum length is 28.
3932 */
3933 if( buf_len < 28 )
3934 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3935
3936 /* Copy most fields and adapt others */
3937 memcpy( obuf, in, 25 );
3938 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3939 obuf[25] = 0xfe;
3940 obuf[26] = 0xff;
3941
3942 /* Generate and write actual cookie */
3943 p = obuf + 28;
3944 if( f_cookie_write( p_cookie,
3945 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3946 {
3947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3948 }
3949
3950 *olen = p - obuf;
3951
3952 /* Go back and fill length fields */
3953 obuf[27] = (unsigned char)( *olen - 28 );
3954
3955 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3956 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3957 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3958
3959 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3960 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3961
3962 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3963}
3964
3965/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003966 * Handle possible client reconnect with the same UDP quadruplet
3967 * (RFC 6347 Section 4.2.8).
3968 *
3969 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3970 * that looks like a ClientHello.
3971 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003972 * - if the input looks like a ClientHello without cookies,
3973 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003974 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003975 * - if the input looks like a ClientHello with a valid cookie,
3976 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003977 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003978 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003979 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003980 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003981 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3982 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003983 */
3984static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3985{
3986 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003987 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003988
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003989 ret = ssl_check_dtls_clihlo_cookie(
3990 ssl->conf->f_cookie_write,
3991 ssl->conf->f_cookie_check,
3992 ssl->conf->p_cookie,
3993 ssl->cli_id, ssl->cli_id_len,
3994 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003995 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003996
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003997 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3998
3999 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004000 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004001 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004002 * If the error is permanent we'll catch it later,
4003 * if it's not, then hopefully it'll work next time. */
4004 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4005
4006 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004007 }
4008
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004009 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004010 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004011 /* Got a valid cookie, partially reset context */
4012 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4013 {
4014 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4015 return( ret );
4016 }
4017
4018 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004019 }
4020
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004021 return( ret );
4022}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004023#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004024
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004025/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004026 * ContentType type;
4027 * ProtocolVersion version;
4028 * uint16 epoch; // DTLS only
4029 * uint48 sequence_number; // DTLS only
4030 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004031 *
4032 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004033 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004034 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4035 *
4036 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004037 * 1. proceed with the record if this function returns 0
4038 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4039 * 3. return CLIENT_RECONNECT if this function return that value
4040 * 4. drop the whole datagram if this function returns anything else.
4041 * Point 2 is needed when the peer is resending, and we have already received
4042 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004045{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004046 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048 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 +02004049
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004051 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004052 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004056 ssl->in_msgtype,
4057 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004058
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004059 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4061 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4062 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4063 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004066
4067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004068 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4069 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004070 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4071#endif /* MBEDTLS_SSL_PROTO_DTLS */
4072 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4073 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004076 }
4077
4078 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004079 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4082 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004083 }
4084
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004085 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4088 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004089 }
4090
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004091 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004092 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004093 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4096 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004097 }
4098
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004099 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004100 * DTLS-related tests.
4101 * Check epoch before checking length constraint because
4102 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4103 * message gets duplicated before the corresponding Finished message,
4104 * the second ChangeCipherSpec should be discarded because it belongs
4105 * to an old epoch, but not because its length is shorter than
4106 * the minimum record length for packets using the new record transform.
4107 * Note that these two kinds of failures are handled differently,
4108 * as an unexpected record is silently skipped but an invalid
4109 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004110 */
4111#if defined(MBEDTLS_SSL_PROTO_DTLS)
4112 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4113 {
4114 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4115
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004116 /* Check epoch (and sequence number) with DTLS */
4117 if( rec_epoch != ssl->in_epoch )
4118 {
4119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4120 "expected %d, received %d",
4121 ssl->in_epoch, rec_epoch ) );
4122
4123#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4124 /*
4125 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4126 * access the first byte of record content (handshake type), as we
4127 * have an active transform (possibly iv_len != 0), so use the
4128 * fact that the record header len is 13 instead.
4129 */
4130 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4131 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4132 rec_epoch == 0 &&
4133 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4134 ssl->in_left > 13 &&
4135 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4136 {
4137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4138 "from the same port" ) );
4139 return( ssl_handle_possible_reconnect( ssl ) );
4140 }
4141 else
4142#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004143 {
4144 /* Consider buffering the record. */
4145 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4146 {
4147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4148 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4149 }
4150
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004151 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004152 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004153 }
4154
4155#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4156 /* Replay detection only works for the current epoch */
4157 if( rec_epoch == ssl->in_epoch &&
4158 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4159 {
4160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4161 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4162 }
4163#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004164
Hanno Becker52c6dc62017-05-26 16:07:36 +01004165 /* Drop unexpected ApplicationData records,
4166 * except at the beginning of renegotiations */
4167 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4168 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4169#if defined(MBEDTLS_SSL_RENEGOTIATION)
4170 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4171 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4172#endif
4173 )
4174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4176 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4177 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004178 }
4179#endif /* MBEDTLS_SSL_PROTO_DTLS */
4180
Hanno Becker52c6dc62017-05-26 16:07:36 +01004181
4182 /* Check length against bounds of the current transform and version */
4183 if( ssl->transform_in == NULL )
4184 {
4185 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004186 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004187 {
4188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4189 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4190 }
4191 }
4192 else
4193 {
4194 if( ssl->in_msglen < ssl->transform_in->minlen )
4195 {
4196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4197 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4198 }
4199
4200#if defined(MBEDTLS_SSL_PROTO_SSL3)
4201 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004202 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004203 {
4204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4205 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4206 }
4207#endif
4208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4209 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4210 /*
4211 * TLS encrypted messages can have up to 256 bytes of padding
4212 */
4213 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4214 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004215 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004216 {
4217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4218 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4219 }
4220#endif
4221 }
4222
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004223 return( 0 );
4224}
Paul Bakker5121ce52009-01-03 21:22:43 +00004225
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004226/*
4227 * If applicable, decrypt (and decompress) record content
4228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004230{
4231 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4234 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4237 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 ret = mbedtls_ssl_hw_record_read( ssl );
4242 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4245 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004246 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004247
4248 if( ret == 0 )
4249 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004252 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004253 {
4254 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 return( ret );
4258 }
4259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004261 ssl->in_msg, ssl->in_msglen );
4262
Angus Grattond8213d02016-05-25 20:56:48 +10004263 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4266 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 }
4268 }
4269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004271 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004273 {
4274 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004277 return( ret );
4278 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004283 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004286 }
4287#endif
4288
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004289 return( 0 );
4290}
4291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004293
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004294/*
4295 * Read a record.
4296 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004297 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4298 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4299 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004300 */
Hanno Becker1097b342018-08-15 14:09:41 +01004301
4302/* Helper functions for mbedtls_ssl_read_record(). */
4303static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004304static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4305static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004306
Hanno Becker327c93b2018-08-15 13:56:18 +01004307int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004308 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004309{
4310 int ret;
4311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004313
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004314 if( ssl->keep_current_message == 0 )
4315 {
4316 do {
Simon Butcher99000142016-10-13 17:21:01 +01004317
Hanno Becker26994592018-08-15 14:14:59 +01004318 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004319 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004320 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004321
Hanno Beckere74d5562018-08-15 14:26:08 +01004322 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004323 {
Hanno Becker40f50842018-08-15 14:48:01 +01004324#if defined(MBEDTLS_SSL_PROTO_DTLS)
4325 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004326
Hanno Becker40f50842018-08-15 14:48:01 +01004327 /* We only check for buffered messages if the
4328 * current datagram is fully consumed. */
4329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004330 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004331 {
Hanno Becker40f50842018-08-15 14:48:01 +01004332 if( ssl_load_buffered_message( ssl ) == 0 )
4333 have_buffered = 1;
4334 }
4335
4336 if( have_buffered == 0 )
4337#endif /* MBEDTLS_SSL_PROTO_DTLS */
4338 {
4339 ret = ssl_get_next_record( ssl );
4340 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4341 continue;
4342
4343 if( ret != 0 )
4344 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004345 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004346 return( ret );
4347 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004348 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004349 }
4350
4351 ret = mbedtls_ssl_handle_message_type( ssl );
4352
Hanno Becker40f50842018-08-15 14:48:01 +01004353#if defined(MBEDTLS_SSL_PROTO_DTLS)
4354 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4355 {
4356 /* Buffer future message */
4357 ret = ssl_buffer_message( ssl );
4358 if( ret != 0 )
4359 return( ret );
4360
4361 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4362 }
4363#endif /* MBEDTLS_SSL_PROTO_DTLS */
4364
Hanno Becker90333da2017-10-10 11:27:13 +01004365 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4366 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004367
4368 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004369 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004370 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004371 return( ret );
4372 }
4373
Hanno Becker327c93b2018-08-15 13:56:18 +01004374 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004375 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004376 {
4377 mbedtls_ssl_update_handshake_status( ssl );
4378 }
Simon Butcher99000142016-10-13 17:21:01 +01004379 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004380 else
Simon Butcher99000142016-10-13 17:21:01 +01004381 {
Hanno Becker02f59072018-08-15 14:00:24 +01004382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004383 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004384 }
4385
4386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4387
4388 return( 0 );
4389}
4390
Hanno Becker40f50842018-08-15 14:48:01 +01004391#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004392static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004393{
Hanno Becker40f50842018-08-15 14:48:01 +01004394 if( ssl->in_left > ssl->next_record_offset )
4395 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004396
Hanno Becker40f50842018-08-15 14:48:01 +01004397 return( 0 );
4398}
4399
4400static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4401{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004402 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004403 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004404 int ret = 0;
4405
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004406 if( hs == NULL )
4407 return( -1 );
4408
Hanno Beckere00ae372018-08-20 09:39:42 +01004409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4410
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004411 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4412 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4413 {
4414 /* Check if we have seen a ChangeCipherSpec before.
4415 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004416 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004417 {
4418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4419 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004420 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004421 }
4422
Hanno Becker39b8bc92018-08-28 17:17:13 +01004423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004424 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4425 ssl->in_msglen = 1;
4426 ssl->in_msg[0] = 1;
4427
4428 /* As long as they are equal, the exact value doesn't matter. */
4429 ssl->in_left = 0;
4430 ssl->next_record_offset = 0;
4431
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004432 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004433 goto exit;
4434 }
Hanno Becker37f95322018-08-16 13:55:32 +01004435
Hanno Beckerb8f50142018-08-28 10:01:34 +01004436#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004437 /* Debug only */
4438 {
4439 unsigned offset;
4440 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4441 {
4442 hs_buf = &hs->buffering.hs[offset];
4443 if( hs_buf->is_valid == 1 )
4444 {
4445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4446 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004447 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004448 }
4449 }
4450 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004451#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004452
4453 /* Check if we have buffered and/or fully reassembled the
4454 * next handshake message. */
4455 hs_buf = &hs->buffering.hs[0];
4456 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4457 {
4458 /* Synthesize a record containing the buffered HS message. */
4459 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4460 ( hs_buf->data[2] << 8 ) |
4461 hs_buf->data[3];
4462
4463 /* Double-check that we haven't accidentally buffered
4464 * a message that doesn't fit into the input buffer. */
4465 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4466 {
4467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4469 }
4470
4471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4472 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4473 hs_buf->data, msg_len + 12 );
4474
4475 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4476 ssl->in_hslen = msg_len + 12;
4477 ssl->in_msglen = msg_len + 12;
4478 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4479
4480 ret = 0;
4481 goto exit;
4482 }
4483 else
4484 {
4485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4486 hs->in_msg_seq ) );
4487 }
4488
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004489 ret = -1;
4490
4491exit:
4492
4493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4494 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004495}
4496
Hanno Beckera02b0b42018-08-21 17:20:27 +01004497static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4498 size_t desired )
4499{
4500 int offset;
4501 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4503 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004504
Hanno Becker01315ea2018-08-21 17:22:17 +01004505 /* Get rid of future records epoch first, if such exist. */
4506 ssl_free_buffered_record( ssl );
4507
4508 /* Check if we have enough space available now. */
4509 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4510 hs->buffering.total_bytes_buffered ) )
4511 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004513 return( 0 );
4514 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004515
Hanno Becker4f432ad2018-08-28 10:02:32 +01004516 /* We don't have enough space to buffer the next expected handshake
4517 * message. Remove buffers used for future messages to gain space,
4518 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004519 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4520 offset >= 0; offset-- )
4521 {
4522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4523 offset ) );
4524
Hanno Beckerb309b922018-08-23 13:18:05 +01004525 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004526
4527 /* Check if we have enough space available now. */
4528 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4529 hs->buffering.total_bytes_buffered ) )
4530 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004532 return( 0 );
4533 }
4534 }
4535
4536 return( -1 );
4537}
4538
Hanno Becker40f50842018-08-15 14:48:01 +01004539static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4540{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004541 int ret = 0;
4542 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4543
4544 if( hs == NULL )
4545 return( 0 );
4546
4547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4548
4549 switch( ssl->in_msgtype )
4550 {
4551 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004553
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004554 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004555 break;
4556
4557 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004558 {
4559 unsigned recv_msg_seq_offset;
4560 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4561 mbedtls_ssl_hs_buffer *hs_buf;
4562 size_t msg_len = ssl->in_hslen - 12;
4563
4564 /* We should never receive an old handshake
4565 * message - double-check nonetheless. */
4566 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4567 {
4568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4569 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4570 }
4571
4572 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4573 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4574 {
4575 /* Silently ignore -- message too far in the future */
4576 MBEDTLS_SSL_DEBUG_MSG( 2,
4577 ( "Ignore future HS message with sequence number %u, "
4578 "buffering window %u - %u",
4579 recv_msg_seq, ssl->handshake->in_msg_seq,
4580 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4581
4582 goto exit;
4583 }
4584
4585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4586 recv_msg_seq, recv_msg_seq_offset ) );
4587
4588 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4589
4590 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004591 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004592 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004593 size_t reassembly_buf_sz;
4594
Hanno Becker37f95322018-08-16 13:55:32 +01004595 hs_buf->is_fragmented =
4596 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4597
4598 /* We copy the message back into the input buffer
4599 * after reassembly, so check that it's not too large.
4600 * This is an implementation-specific limitation
4601 * and not one from the standard, hence it is not
4602 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004603 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004604 {
4605 /* Ignore message */
4606 goto exit;
4607 }
4608
Hanno Beckere0b150f2018-08-21 15:51:03 +01004609 /* Check if we have enough space to buffer the message. */
4610 if( hs->buffering.total_bytes_buffered >
4611 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4612 {
4613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4615 }
4616
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004617 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4618 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004619
4620 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4621 hs->buffering.total_bytes_buffered ) )
4622 {
4623 if( recv_msg_seq_offset > 0 )
4624 {
4625 /* If we can't buffer a future message because
4626 * of space limitations -- ignore. */
4627 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",
4628 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4629 (unsigned) hs->buffering.total_bytes_buffered ) );
4630 goto exit;
4631 }
Hanno Beckere1801392018-08-21 16:51:05 +01004632 else
4633 {
4634 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",
4635 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4636 (unsigned) hs->buffering.total_bytes_buffered ) );
4637 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004638
Hanno Beckera02b0b42018-08-21 17:20:27 +01004639 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004640 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004641 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",
4642 (unsigned) msg_len,
4643 (unsigned) reassembly_buf_sz,
4644 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004645 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004646 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4647 goto exit;
4648 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004649 }
4650
4651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4652 msg_len ) );
4653
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004654 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4655 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004656 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004657 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004658 goto exit;
4659 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004660 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004661
4662 /* Prepare final header: copy msg_type, length and message_seq,
4663 * then add standardised fragment_offset and fragment_length */
4664 memcpy( hs_buf->data, ssl->in_msg, 6 );
4665 memset( hs_buf->data + 6, 0, 3 );
4666 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4667
4668 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004669
4670 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004671 }
4672 else
4673 {
4674 /* Make sure msg_type and length are consistent */
4675 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4676 {
4677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4678 /* Ignore */
4679 goto exit;
4680 }
4681 }
4682
Hanno Becker4422bbb2018-08-20 09:40:19 +01004683 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004684 {
4685 size_t frag_len, frag_off;
4686 unsigned char * const msg = hs_buf->data + 12;
4687
4688 /*
4689 * Check and copy current fragment
4690 */
4691
4692 /* Validation of header fields already done in
4693 * mbedtls_ssl_prepare_handshake_record(). */
4694 frag_off = ssl_get_hs_frag_off( ssl );
4695 frag_len = ssl_get_hs_frag_len( ssl );
4696
4697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4698 frag_off, frag_len ) );
4699 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4700
4701 if( hs_buf->is_fragmented )
4702 {
4703 unsigned char * const bitmask = msg + msg_len;
4704 ssl_bitmask_set( bitmask, frag_off, frag_len );
4705 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4706 msg_len ) == 0 );
4707 }
4708 else
4709 {
4710 hs_buf->is_complete = 1;
4711 }
4712
4713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4714 hs_buf->is_complete ? "" : "not yet " ) );
4715 }
4716
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004717 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004718 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004719
4720 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004721 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004722 break;
4723 }
4724
4725exit:
4726
4727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4728 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004729}
4730#endif /* MBEDTLS_SSL_PROTO_DTLS */
4731
Hanno Becker1097b342018-08-15 14:09:41 +01004732static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004733{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004734 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004735 * Consume last content-layer message and potentially
4736 * update in_msglen which keeps track of the contents'
4737 * consumption state.
4738 *
4739 * (1) Handshake messages:
4740 * Remove last handshake message, move content
4741 * and adapt in_msglen.
4742 *
4743 * (2) Alert messages:
4744 * Consume whole record content, in_msglen = 0.
4745 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004746 * (3) Change cipher spec:
4747 * Consume whole record content, in_msglen = 0.
4748 *
4749 * (4) Application data:
4750 * Don't do anything - the record layer provides
4751 * the application data as a stream transport
4752 * and consumes through mbedtls_ssl_read only.
4753 *
4754 */
4755
4756 /* Case (1): Handshake messages */
4757 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004758 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004759 /* Hard assertion to be sure that no application data
4760 * is in flight, as corrupting ssl->in_msglen during
4761 * ssl->in_offt != NULL is fatal. */
4762 if( ssl->in_offt != NULL )
4763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4766 }
4767
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004768 /*
4769 * Get next Handshake message in the current record
4770 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004771
Hanno Becker4a810fb2017-05-24 16:27:30 +01004772 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004773 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004774 * current handshake content: If DTLS handshake
4775 * fragmentation is used, that's the fragment
4776 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004777 * size here is faulty and should be changed at
4778 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004779 * (2) While it doesn't seem to cause problems, one
4780 * has to be very careful not to assume that in_hslen
4781 * is always <= in_msglen in a sensible communication.
4782 * Again, it's wrong for DTLS handshake fragmentation.
4783 * The following check is therefore mandatory, and
4784 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004785 * Additionally, ssl->in_hslen might be arbitrarily out of
4786 * bounds after handling a DTLS message with an unexpected
4787 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004788 */
4789 if( ssl->in_hslen < ssl->in_msglen )
4790 {
4791 ssl->in_msglen -= ssl->in_hslen;
4792 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4793 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004794
Hanno Becker4a810fb2017-05-24 16:27:30 +01004795 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4796 ssl->in_msg, ssl->in_msglen );
4797 }
4798 else
4799 {
4800 ssl->in_msglen = 0;
4801 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004802
Hanno Becker4a810fb2017-05-24 16:27:30 +01004803 ssl->in_hslen = 0;
4804 }
4805 /* Case (4): Application data */
4806 else if( ssl->in_offt != NULL )
4807 {
4808 return( 0 );
4809 }
4810 /* Everything else (CCS & Alerts) */
4811 else
4812 {
4813 ssl->in_msglen = 0;
4814 }
4815
Hanno Becker1097b342018-08-15 14:09:41 +01004816 return( 0 );
4817}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004818
Hanno Beckere74d5562018-08-15 14:26:08 +01004819static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4820{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004821 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004822 return( 1 );
4823
4824 return( 0 );
4825}
4826
Hanno Becker5f066e72018-08-16 14:56:31 +01004827#if defined(MBEDTLS_SSL_PROTO_DTLS)
4828
4829static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4830{
4831 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4832 if( hs == NULL )
4833 return;
4834
Hanno Becker01315ea2018-08-21 17:22:17 +01004835 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004836 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004837 hs->buffering.total_bytes_buffered -=
4838 hs->buffering.future_record.len;
4839
4840 mbedtls_free( hs->buffering.future_record.data );
4841 hs->buffering.future_record.data = NULL;
4842 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004843}
4844
4845static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4846{
4847 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4848 unsigned char * rec;
4849 size_t rec_len;
4850 unsigned rec_epoch;
4851
4852 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4853 return( 0 );
4854
4855 if( hs == NULL )
4856 return( 0 );
4857
Hanno Becker5f066e72018-08-16 14:56:31 +01004858 rec = hs->buffering.future_record.data;
4859 rec_len = hs->buffering.future_record.len;
4860 rec_epoch = hs->buffering.future_record.epoch;
4861
4862 if( rec == NULL )
4863 return( 0 );
4864
Hanno Becker4cb782d2018-08-20 11:19:05 +01004865 /* Only consider loading future records if the
4866 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004867 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004868 return( 0 );
4869
Hanno Becker5f066e72018-08-16 14:56:31 +01004870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4871
4872 if( rec_epoch != ssl->in_epoch )
4873 {
4874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4875 goto exit;
4876 }
4877
4878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4879
4880 /* Double-check that the record is not too large */
4881 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4882 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4883 {
4884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4886 }
4887
4888 memcpy( ssl->in_hdr, rec, rec_len );
4889 ssl->in_left = rec_len;
4890 ssl->next_record_offset = 0;
4891
4892 ssl_free_buffered_record( ssl );
4893
4894exit:
4895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4896 return( 0 );
4897}
4898
4899static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4900{
4901 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4902 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004903 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004904
4905 /* Don't buffer future records outside handshakes. */
4906 if( hs == NULL )
4907 return( 0 );
4908
4909 /* Only buffer handshake records (we are only interested
4910 * in Finished messages). */
4911 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4912 return( 0 );
4913
4914 /* Don't buffer more than one future epoch record. */
4915 if( hs->buffering.future_record.data != NULL )
4916 return( 0 );
4917
Hanno Becker01315ea2018-08-21 17:22:17 +01004918 /* Don't buffer record if there's not enough buffering space remaining. */
4919 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4920 hs->buffering.total_bytes_buffered ) )
4921 {
4922 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",
4923 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4924 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004925 return( 0 );
4926 }
4927
Hanno Becker5f066e72018-08-16 14:56:31 +01004928 /* Buffer record */
4929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
4930 ssl->in_epoch + 1 ) );
4931 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
4932 rec_hdr_len + ssl->in_msglen );
4933
4934 /* ssl_parse_record_header() only considers records
4935 * of the next epoch as candidates for buffering. */
4936 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01004937 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004938
4939 hs->buffering.future_record.data =
4940 mbedtls_calloc( 1, hs->buffering.future_record.len );
4941 if( hs->buffering.future_record.data == NULL )
4942 {
4943 /* If we run out of RAM trying to buffer a
4944 * record from the next epoch, just ignore. */
4945 return( 0 );
4946 }
4947
Hanno Becker01315ea2018-08-21 17:22:17 +01004948 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01004949
Hanno Becker01315ea2018-08-21 17:22:17 +01004950 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01004951 return( 0 );
4952}
4953
4954#endif /* MBEDTLS_SSL_PROTO_DTLS */
4955
Hanno Beckere74d5562018-08-15 14:26:08 +01004956static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004957{
4958 int ret;
4959
Hanno Becker5f066e72018-08-16 14:56:31 +01004960#if defined(MBEDTLS_SSL_PROTO_DTLS)
4961 /* We might have buffered a future record; if so,
4962 * and if the epoch matches now, load it.
4963 * On success, this call will set ssl->in_left to
4964 * the length of the buffered record, so that
4965 * the calls to ssl_fetch_input() below will
4966 * essentially be no-ops. */
4967 ret = ssl_load_buffered_record( ssl );
4968 if( ret != 0 )
4969 return( ret );
4970#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004975 return( ret );
4976 }
4977
4978 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4982 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004983 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004984 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4985 {
4986 ret = ssl_buffer_future_record( ssl );
4987 if( ret != 0 )
4988 return( ret );
4989
4990 /* Fall through to handling of unexpected records */
4991 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4992 }
4993
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004994 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4995 {
4996 /* Skip unexpected record (but not whole datagram) */
4997 ssl->next_record_offset = ssl->in_msglen
4998 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004999
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5001 "(header)" ) );
5002 }
5003 else
5004 {
5005 /* Skip invalid record and the rest of the datagram */
5006 ssl->next_record_offset = 0;
5007 ssl->in_left = 0;
5008
5009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5010 "(header)" ) );
5011 }
5012
5013 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005014 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005015 }
5016#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005017 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005018 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005019
5020 /*
5021 * Read and optionally decrypt the message contents
5022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5024 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005027 return( ret );
5028 }
5029
5030 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005032 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005035 if( ssl->next_record_offset < ssl->in_left )
5036 {
5037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5038 }
5039 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005040 else
5041#endif
5042 ssl->in_left = 0;
5043
5044 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005047 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005048 {
5049 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5051 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005052 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005053 /* Except when waiting for Finished as a bad mac here
5054 * probably means something went wrong in the handshake
5055 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5056 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5057 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5058 {
5059#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5060 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5061 {
5062 mbedtls_ssl_send_alert_message( ssl,
5063 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5064 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5065 }
5066#endif
5067 return( ret );
5068 }
5069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005071 if( ssl->conf->badmac_limit != 0 &&
5072 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5075 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005076 }
5077#endif
5078
Hanno Becker4a810fb2017-05-24 16:27:30 +01005079 /* As above, invalid records cause
5080 * dismissal of the whole datagram. */
5081
5082 ssl->next_record_offset = 0;
5083 ssl->in_left = 0;
5084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005086 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005087 }
5088
5089 return( ret );
5090 }
5091 else
5092#endif
5093 {
5094 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5096 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 mbedtls_ssl_send_alert_message( ssl,
5099 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5100 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005101 }
5102#endif
5103 return( ret );
5104 }
5105 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005106
Simon Butcher99000142016-10-13 17:21:01 +01005107 return( 0 );
5108}
5109
5110int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5111{
5112 int ret;
5113
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005114 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005115 * Handle particular types of records
5116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 {
Simon Butcher99000142016-10-13 17:21:01 +01005119 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5120 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005121 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005123 }
5124
Hanno Beckere678eaa2018-08-21 14:57:46 +01005125 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005126 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005127 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005128 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5130 ssl->in_msglen ) );
5131 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005132 }
5133
Hanno Beckere678eaa2018-08-21 14:57:46 +01005134 if( ssl->in_msg[0] != 1 )
5135 {
5136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5137 ssl->in_msg[0] ) );
5138 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5139 }
5140
5141#if defined(MBEDTLS_SSL_PROTO_DTLS)
5142 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5143 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5144 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5145 {
5146 if( ssl->handshake == NULL )
5147 {
5148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5149 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5150 }
5151
5152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5153 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5154 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005155#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005156 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005159 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005160 if( ssl->in_msglen != 2 )
5161 {
5162 /* Note: Standard allows for more than one 2 byte alert
5163 to be packed in a single message, but Mbed TLS doesn't
5164 currently support this. */
5165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5166 ssl->in_msglen ) );
5167 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5168 }
5169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005171 ssl->in_msg[0], ssl->in_msg[1] ) );
5172
5173 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005174 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005179 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005181 }
5182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005183 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5184 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5187 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005188 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005189
5190#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5191 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5192 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5193 {
Hanno Becker90333da2017-10-10 11:27:13 +01005194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005195 /* Will be handled when trying to parse ServerHello */
5196 return( 0 );
5197 }
5198#endif
5199
5200#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5201 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5202 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5203 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5204 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5205 {
5206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5207 /* Will be handled in mbedtls_ssl_parse_certificate() */
5208 return( 0 );
5209 }
5210#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5211
5212 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005213 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 }
5215
Hanno Beckerc76c6192017-06-06 10:03:17 +01005216#if defined(MBEDTLS_SSL_PROTO_DTLS)
5217 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5218 ssl->handshake != NULL &&
5219 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5220 {
5221 ssl_handshake_wrapup_free_hs_transform( ssl );
5222 }
5223#endif
5224
Paul Bakker5121ce52009-01-03 21:22:43 +00005225 return( 0 );
5226}
5227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005229{
5230 int ret;
5231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5233 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5234 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005235 {
5236 return( ret );
5237 }
5238
5239 return( 0 );
5240}
5241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005243 unsigned char level,
5244 unsigned char message )
5245{
5246 int ret;
5247
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005248 if( ssl == NULL || ssl->conf == NULL )
5249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005255 ssl->out_msglen = 2;
5256 ssl->out_msg[0] = level;
5257 ssl->out_msg[1] = message;
5258
Hanno Becker67bc7c32018-08-06 11:33:50 +01005259 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005262 return( ret );
5263 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005265
5266 return( 0 );
5267}
5268
Paul Bakker5121ce52009-01-03 21:22:43 +00005269/*
5270 * Handshake functions
5271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5273 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5274 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5275 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5276 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5277 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5278 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005279/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005281{
Hanno Becker8759e162017-12-27 21:34:08 +00005282 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5289 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005292 ssl->state++;
5293 return( 0 );
5294 }
5295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005298}
5299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005301{
Hanno Becker8759e162017-12-27 21:34:08 +00005302 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5307 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005308 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5309 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005312 ssl->state++;
5313 return( 0 );
5314 }
5315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005318}
Gilles Peskinef9828522017-05-03 12:28:43 +02005319
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005320#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005321/* Some certificate support -> implement write and parse */
5322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005326 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005328 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5333 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005334 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5335 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005338 ssl->state++;
5339 return( 0 );
5340 }
5341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005343 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005344 {
5345 if( ssl->client_auth == 0 )
5346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005348 ssl->state++;
5349 return( 0 );
5350 }
5351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005353 /*
5354 * If using SSLv3 and got no cert, send an Alert message
5355 * (otherwise an empty Certificate message will be sent).
5356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5358 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005359 {
5360 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5362 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5363 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005366 goto write_msg;
5367 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370#endif /* MBEDTLS_SSL_CLI_C */
5371#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005372 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5377 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005378 }
5379 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005380#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005383
5384 /*
5385 * 0 . 0 handshake type
5386 * 1 . 3 handshake length
5387 * 4 . 6 length of all certs
5388 * 7 . 9 length of cert. 1
5389 * 10 . n-1 peer certificate
5390 * n . n+2 length of cert. 2
5391 * n+3 . ... upper level cert, etc.
5392 */
5393 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005395
Paul Bakker29087132010-03-21 21:03:34 +00005396 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005397 {
5398 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005399 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005402 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005404 }
5405
5406 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5407 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5408 ssl->out_msg[i + 2] = (unsigned char)( n );
5409
5410 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5411 i += n; crt = crt->next;
5412 }
5413
5414 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5415 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5416 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5417
5418 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5420 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005421
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005422#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005423write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005424#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005425
5426 ssl->state++;
5427
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005428 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005429 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005431 return( ret );
5432 }
5433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005435
Paul Bakkered27a042013-04-18 22:46:23 +02005436 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005437}
5438
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005439/*
5440 * Once the certificate message is read, parse it into a cert chain and
5441 * perform basic checks, but leave actual verification to the caller
5442 */
5443static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005444{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005445 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005446 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005447 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449#if defined(MBEDTLS_SSL_SRV_C)
5450#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 /*
5452 * Check if the client sent an empty certificate
5453 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005454 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005456 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005457 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5459 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5460 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005463
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005464 /* The client was asked for a certificate but didn't send
5465 one. The client should know what's going on, so we
5466 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005467 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005468 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005469 }
5470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5474 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005475 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5479 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5480 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5481 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005484
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005485 /* The client was asked for a certificate but didn't send
5486 one. The client should know what's going on, so we
5487 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005488 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005489 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005490 }
5491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5493 MBEDTLS_SSL_PROTO_TLS1_2 */
5494#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005499 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5500 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 }
5503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5505 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005508 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5509 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 }
5512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005514
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005518 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005519
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005520 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005524 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005527 }
5528
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005529 /* In case we tried to reuse a session but it failed */
5530 if( ssl->session_negotiate->peer_cert != NULL )
5531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5533 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005534 }
5535
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005536 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005538 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5542 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005543 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005544 }
5545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005547
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005548 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005549
5550 while( i < ssl->in_hslen )
5551 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005552 if ( i + 3 > ssl->in_hslen ) {
5553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5554 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5555 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5556 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005558 if( ssl->in_msg[i] != 0 )
5559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005561 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5562 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 }
5565
5566 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5567 | (unsigned int) ssl->in_msg[i + 2];
5568 i += 3;
5569
5570 if( n < 128 || i + n > ssl->in_hslen )
5571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005573 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5574 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005576 }
5577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005579 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005580 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005582 case 0: /*ok*/
5583 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5584 /* Ignore certificate with an unknown algorithm: maybe a
5585 prior certificate was already trusted. */
5586 break;
5587
5588 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5589 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5590 goto crt_parse_der_failed;
5591
5592 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5593 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5594 goto crt_parse_der_failed;
5595
5596 default:
5597 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5598 crt_parse_der_failed:
5599 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 return( ret );
5602 }
5603
5604 i += n;
5605 }
5606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005608
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005609 /*
5610 * On client, make sure the server cert doesn't change during renego to
5611 * avoid "triple handshake" attack: https://secure-resumption.com/
5612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005614 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005616 {
5617 if( ssl->session->peer_cert == NULL )
5618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005620 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5621 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005623 }
5624
5625 if( ssl->session->peer_cert->raw.len !=
5626 ssl->session_negotiate->peer_cert->raw.len ||
5627 memcmp( ssl->session->peer_cert->raw.p,
5628 ssl->session_negotiate->peer_cert->raw.p,
5629 ssl->session->peer_cert->raw.len ) != 0 )
5630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005632 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5633 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005635 }
5636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005638
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005639 return( 0 );
5640}
5641
5642int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5643{
5644 int ret;
5645 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00005646 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005647#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5648 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5649 ? ssl->handshake->sni_authmode
5650 : ssl->conf->authmode;
5651#else
5652 const int authmode = ssl->conf->authmode;
5653#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005654 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005655
5656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5657
5658 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5659 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5660 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5661 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5662 {
5663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5664 ssl->state++;
5665 return( 0 );
5666 }
5667
5668#if defined(MBEDTLS_SSL_SRV_C)
5669 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5670 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5671 {
5672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5673 ssl->state++;
5674 return( 0 );
5675 }
5676
5677 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5678 authmode == MBEDTLS_SSL_VERIFY_NONE )
5679 {
5680 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005682
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005683 ssl->state++;
5684 return( 0 );
5685 }
5686#endif
5687
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005688#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5689 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005690 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005691 {
5692 goto crt_verify;
5693 }
5694#endif
5695
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005696 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005697 {
5698 /* mbedtls_ssl_read_record may have sent an alert already. We
5699 let it decide whether to alert. */
5700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5701 return( ret );
5702 }
5703
5704 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5705 {
5706#if defined(MBEDTLS_SSL_SRV_C)
5707 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5708 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5709 {
5710 ret = 0;
5711 }
5712#endif
5713
5714 ssl->state++;
5715 return( ret );
5716 }
5717
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005718#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5719 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005720 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005721
5722crt_verify:
5723 if( ssl->handshake->ecrs_enabled)
5724 rs_ctx = &ssl->handshake->ecrs_ctx;
5725#endif
5726
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005727 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005728 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005729 mbedtls_x509_crt *ca_chain;
5730 mbedtls_x509_crl *ca_crl;
5731
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005732#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005733 if( ssl->handshake->sni_ca_chain != NULL )
5734 {
5735 ca_chain = ssl->handshake->sni_ca_chain;
5736 ca_crl = ssl->handshake->sni_ca_crl;
5737 }
5738 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005739#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005740 {
5741 ca_chain = ssl->conf->ca_chain;
5742 ca_crl = ssl->conf->ca_crl;
5743 }
5744
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005745 /*
5746 * Main check: verify certificate
5747 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005748 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005749 ssl->session_negotiate->peer_cert,
5750 ca_chain, ca_crl,
5751 ssl->conf->cert_profile,
5752 ssl->hostname,
5753 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005754 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005755
5756 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005759 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005760
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005761#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5762 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005763 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005764#endif
5765
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005766 /*
5767 * Secondary checks: always done, but change 'ret' only if it was 0
5768 */
5769
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005770#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005773
5774 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005776 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005777 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005778 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005781 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005783 }
5784 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005785#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005788 ciphersuite_info,
5789 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005790 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005793 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005795 }
5796
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005797 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5798 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5799 * with details encoded in the verification flags. All other kinds
5800 * of error codes, including those from the user provided f_vrfy
5801 * functions, are treated as fatal and lead to a failure of
5802 * ssl_parse_certificate even if verification was optional. */
5803 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5804 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5805 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5806 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005807 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005808 }
5809
5810 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5811 {
5812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5813 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5814 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005815
5816 if( ret != 0 )
5817 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005818 uint8_t alert;
5819
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005820 /* The certificate may have been rejected for several reasons.
5821 Pick one and send the corresponding alert. Which alert to send
5822 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005823 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5824 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5825 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5826 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5827 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5828 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5829 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5830 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5831 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5832 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5833 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5834 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5835 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5836 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5837 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5838 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5839 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5840 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5841 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5842 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005843 else
5844 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005845 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5846 alert );
5847 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005848
Hanno Beckere6706e62017-05-15 16:05:15 +01005849#if defined(MBEDTLS_DEBUG_C)
5850 if( ssl->session_negotiate->verify_result != 0 )
5851 {
5852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5853 ssl->session_negotiate->verify_result ) );
5854 }
5855 else
5856 {
5857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5858 }
5859#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 }
5861
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005862 ssl->state++;
5863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005865
5866 return( ret );
5867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5869 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5870 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5871 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5872 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5873 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5874 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005876int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005877{
5878 int ret;
5879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005883 ssl->out_msglen = 1;
5884 ssl->out_msg[0] = 1;
5885
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 ssl->state++;
5887
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005888 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 return( ret );
5892 }
5893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895
5896 return( 0 );
5897}
5898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005900{
5901 int ret;
5902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005904
Hanno Becker327c93b2018-08-15 13:56:18 +01005905 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005908 return( ret );
5909 }
5910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005914 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5915 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005917 }
5918
Hanno Beckere678eaa2018-08-21 14:57:46 +01005919 /* CCS records are only accepted if they have length 1 and content '1',
5920 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005921
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005922 /*
5923 * Switch to our negotiated transform and session parameters for inbound
5924 * data.
5925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005927 ssl->transform_in = ssl->transform_negotiate;
5928 ssl->session_in = ssl->session_negotiate;
5929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005931 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005934 ssl_dtls_replay_reset( ssl );
5935#endif
5936
5937 /* Increment epoch */
5938 if( ++ssl->in_epoch == 0 )
5939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005941 /* This is highly unlikely to happen for legitimate reasons, so
5942 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005944 }
5945 }
5946 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005948 memset( ssl->in_ctr, 0, 8 );
5949
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005950 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5953 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005958 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5959 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005961 }
5962 }
5963#endif
5964
Paul Bakker5121ce52009-01-03 21:22:43 +00005965 ssl->state++;
5966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005968
5969 return( 0 );
5970}
5971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5973 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005974{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005975 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5978 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5979 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005980 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005981 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5984#if defined(MBEDTLS_SHA512_C)
5985 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005986 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5987 else
5988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989#if defined(MBEDTLS_SHA256_C)
5990 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005991 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005992 else
5993#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005997 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005998 }
Paul Bakker380da532012-04-18 16:10:25 +00005999}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006002{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6004 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006005 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6006 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6009#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006010 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006013 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006016}
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006019 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6022 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006023 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6024 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006025#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6027#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006028 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006031 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006034}
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6037 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6038static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006039 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006040{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006041 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6042 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006043}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006044#endif
Paul Bakker380da532012-04-18 16:10:25 +00006045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6047#if defined(MBEDTLS_SHA256_C)
6048static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006049 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006050{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006051 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006052}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006053#endif
Paul Bakker380da532012-04-18 16:10:25 +00006054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#if defined(MBEDTLS_SHA512_C)
6056static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006057 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006058{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006059 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006060}
Paul Bakker769075d2012-11-24 11:26:46 +01006061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006065static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006067{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006068 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006069 mbedtls_md5_context md5;
6070 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006071
Paul Bakker5121ce52009-01-03 21:22:43 +00006072 unsigned char padbuf[48];
6073 unsigned char md5sum[16];
6074 unsigned char sha1sum[20];
6075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006077 if( !session )
6078 session = ssl->session;
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006081
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006082 mbedtls_md5_init( &md5 );
6083 mbedtls_sha1_init( &sha1 );
6084
6085 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6086 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006087
6088 /*
6089 * SSLv3:
6090 * hash =
6091 * MD5( master + pad2 +
6092 * MD5( handshake + sender + master + pad1 ) )
6093 * + SHA1( master + pad2 +
6094 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 */
6096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006098 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6099 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006103 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6104 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006108 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
Paul Bakker1ef83d62012-04-11 12:09:53 +00006110 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006112 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6113 mbedtls_md5_update_ret( &md5, session->master, 48 );
6114 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6115 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006116
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006117 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6118 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6119 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6120 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
Paul Bakker1ef83d62012-04-11 12:09:53 +00006122 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006124 mbedtls_md5_starts_ret( &md5 );
6125 mbedtls_md5_update_ret( &md5, session->master, 48 );
6126 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6127 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6128 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006129
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006130 mbedtls_sha1_starts_ret( &sha1 );
6131 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6132 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6133 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6134 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006138 mbedtls_md5_free( &md5 );
6139 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006140
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006141 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6142 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6143 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006150static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006152{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006153 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006154 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006155 mbedtls_md5_context md5;
6156 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006157 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006160 if( !session )
6161 session = ssl->session;
6162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006164
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006165 mbedtls_md5_init( &md5 );
6166 mbedtls_sha1_init( &sha1 );
6167
6168 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6169 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170
Paul Bakker1ef83d62012-04-11 12:09:53 +00006171 /*
6172 * TLSv1:
6173 * hash = PRF( master, finished_label,
6174 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6175 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006178 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6179 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006180#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006183 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6184 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006185#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006188 ? "client finished"
6189 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006190
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006191 mbedtls_md5_finish_ret( &md5, padbuf );
6192 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006193
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006194 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006195 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006198
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006199 mbedtls_md5_free( &md5 );
6200 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006201
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006202 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6209#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006210static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006212{
6213 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006214 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006215 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006216 unsigned char padbuf[32];
6217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006219 if( !session )
6220 session = ssl->session;
6221
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006222 mbedtls_sha256_init( &sha256 );
6223
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006225
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006226 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006227
6228 /*
6229 * TLSv1.2:
6230 * hash = PRF( master, finished_label,
6231 * Hash( handshake ) )[0.11]
6232 */
6233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234#if !defined(MBEDTLS_SHA256_ALT)
6235 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006236 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006237#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006240 ? "client finished"
6241 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006242
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006243 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006244
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006245 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006246 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006249
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006250 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006251
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006252 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006259static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006261{
6262 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006263 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006264 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006265 unsigned char padbuf[48];
6266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006268 if( !session )
6269 session = ssl->session;
6270
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006271 mbedtls_sha512_init( &sha512 );
6272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006274
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006275 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006276
6277 /*
6278 * TLSv1.2:
6279 * hash = PRF( master, finished_label,
6280 * Hash( handshake ) )[0.11]
6281 */
6282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006284 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6285 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006286#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006289 ? "client finished"
6290 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006291
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006292 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006293
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006294 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006295 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006298
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006299 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006300
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006301 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305#endif /* MBEDTLS_SHA512_C */
6306#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006311
6312 /*
6313 * Free our handshake params
6314 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006315 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006317 ssl->handshake = NULL;
6318
6319 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006320 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006321 */
6322 if( ssl->transform )
6323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324 mbedtls_ssl_transform_free( ssl->transform );
6325 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006326 }
6327 ssl->transform = ssl->transform_negotiate;
6328 ssl->transform_negotiate = NULL;
6329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006331}
6332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006334{
6335 int resume = ssl->handshake->resume;
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_RENEGOTIATION)
6340 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006343 ssl->renego_records_seen = 0;
6344 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006345#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006346
6347 /*
6348 * Free the previous session and switch in the current one
6349 */
Paul Bakker0a597072012-09-25 21:55:46 +00006350 if( ssl->session )
6351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006353 /* RFC 7366 3.1: keep the EtM state */
6354 ssl->session_negotiate->encrypt_then_mac =
6355 ssl->session->encrypt_then_mac;
6356#endif
6357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358 mbedtls_ssl_session_free( ssl->session );
6359 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006360 }
6361 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006362 ssl->session_negotiate = NULL;
6363
Paul Bakker0a597072012-09-25 21:55:46 +00006364 /*
6365 * Add cache entry
6366 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006367 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006368 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006369 resume == 0 )
6370 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006371 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006373 }
Paul Bakker0a597072012-09-25 21:55:46 +00006374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006376 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006377 ssl->handshake->flight != NULL )
6378 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006379 /* Cancel handshake timer */
6380 ssl_set_timer( ssl, 0 );
6381
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006382 /* Keep last flight around in case we need to resend it:
6383 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006385 }
6386 else
6387#endif
6388 ssl_handshake_wrapup_free_hs_transform( ssl );
6389
Paul Bakker48916f92012-09-16 19:57:18 +00006390 ssl->state++;
6391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006393}
6394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006396{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006397 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006400
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006401 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006402
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006403 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006404
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006405 /*
6406 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6407 * may define some other value. Currently (early 2016), no defined
6408 * ciphersuite does this (and this is unlikely to change as activity has
6409 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006414 ssl->verify_data_len = hash_len;
6415 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006416#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006417
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6420 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006421
6422 /*
6423 * In case of session resuming, invert the client and server
6424 * ChangeCipherSpec messages order.
6425 */
Paul Bakker0a597072012-09-25 21:55:46 +00006426 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006429 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006435#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006436 }
6437 else
6438 ssl->state++;
6439
Paul Bakker48916f92012-09-16 19:57:18 +00006440 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006441 * Switch to our negotiated transform and session parameters for outbound
6442 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006447 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006448 {
6449 unsigned char i;
6450
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006451 /* Remember current epoch settings for resending */
6452 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006453 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006454
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006455 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006456 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006457
6458 /* Increment epoch */
6459 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006460 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006461 break;
6462
6463 /* The loop goes to its end iff the counter is wrapping */
6464 if( i == 0 )
6465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6467 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006468 }
6469 }
6470 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006472 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006473
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006474 ssl->transform_out = ssl->transform_negotiate;
6475 ssl->session_out = ssl->session_negotiate;
6476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6478 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6483 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006484 }
6485 }
6486#endif
6487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006491#endif
6492
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006493 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 return( ret );
6497 }
6498
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006499#if defined(MBEDTLS_SSL_PROTO_DTLS)
6500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6501 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6502 {
6503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6504 return( ret );
6505 }
6506#endif
6507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006509
6510 return( 0 );
6511}
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006514#define SSL_MAX_HASH_LEN 36
6515#else
6516#define SSL_MAX_HASH_LEN 12
6517#endif
6518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006520{
Paul Bakker23986e52011-04-24 08:57:21 +00006521 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006522 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006523 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006526
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006527 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006528
Hanno Becker327c93b2018-08-15 13:56:18 +01006529 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 return( ret );
6533 }
6534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006538 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6539 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006541 }
6542
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006543 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544#if defined(MBEDTLS_SSL_PROTO_SSL3)
6545 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006546 hash_len = 36;
6547 else
6548#endif
6549 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6552 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006555 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6556 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006558 }
6559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006561 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006564 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6565 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 }
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006570 ssl->verify_data_len = hash_len;
6571 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006572#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006573
Paul Bakker0a597072012-09-25 21:55:46 +00006574 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006577 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006581 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 }
6585 else
6586 ssl->state++;
6587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006589 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006591#endif
6592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594
6595 return( 0 );
6596}
6597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6603 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6604 mbedtls_md5_init( &handshake->fin_md5 );
6605 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006606 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6607 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6610#if defined(MBEDTLS_SHA256_C)
6611 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006612 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614#if defined(MBEDTLS_SHA512_C)
6615 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006616 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006619
6620 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006621
6622#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6623 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6624 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6625#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627#if defined(MBEDTLS_DHM_C)
6628 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630#if defined(MBEDTLS_ECDH_C)
6631 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006632#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006633#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006634 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006635#if defined(MBEDTLS_SSL_CLI_C)
6636 handshake->ecjpake_cache = NULL;
6637 handshake->ecjpake_cache_len = 0;
6638#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006639#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006640
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006641#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006642 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006643#endif
6644
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006645#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6646 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6647#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006648}
6649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006651{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6655 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 mbedtls_md_init( &transform->md_ctx_enc );
6658 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006659}
6660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006662{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006664}
6665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006667{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006668 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006669 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006671 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006673 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006674 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006675
6676 /*
6677 * Either the pointers are now NULL or cleared properly and can be freed.
6678 * Now allocate missing structures.
6679 */
6680 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006681 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006682 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006683 }
Paul Bakker48916f92012-09-16 19:57:18 +00006684
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006685 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006686 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006687 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006688 }
Paul Bakker48916f92012-09-16 19:57:18 +00006689
Paul Bakker82788fb2014-10-20 13:59:19 +02006690 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006691 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006692 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006693 }
Paul Bakker48916f92012-09-16 19:57:18 +00006694
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006695 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006696 if( ssl->handshake == NULL ||
6697 ssl->transform_negotiate == NULL ||
6698 ssl->session_negotiate == NULL )
6699 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 mbedtls_free( ssl->handshake );
6703 mbedtls_free( ssl->transform_negotiate );
6704 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006705
6706 ssl->handshake = NULL;
6707 ssl->transform_negotiate = NULL;
6708 ssl->session_negotiate = NULL;
6709
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006710 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006711 }
6712
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006713 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006715 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006716 ssl_handshake_params_init( ssl->handshake );
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6720 {
6721 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006722
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006723 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6724 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6725 else
6726 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006727
6728 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006729 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006730#endif
6731
Paul Bakker48916f92012-09-16 19:57:18 +00006732 return( 0 );
6733}
6734
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006735#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006736/* Dummy cookie callbacks for defaults */
6737static int ssl_cookie_write_dummy( void *ctx,
6738 unsigned char **p, unsigned char *end,
6739 const unsigned char *cli_id, size_t cli_id_len )
6740{
6741 ((void) ctx);
6742 ((void) p);
6743 ((void) end);
6744 ((void) cli_id);
6745 ((void) cli_id_len);
6746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006748}
6749
6750static int ssl_cookie_check_dummy( void *ctx,
6751 const unsigned char *cookie, size_t cookie_len,
6752 const unsigned char *cli_id, size_t cli_id_len )
6753{
6754 ((void) ctx);
6755 ((void) cookie);
6756 ((void) cookie_len);
6757 ((void) cli_id);
6758 ((void) cli_id_len);
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006761}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006762#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006763
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006764/* Once ssl->out_hdr as the address of the beginning of the
6765 * next outgoing record is set, deduce the other pointers.
6766 *
6767 * Note: For TLS, we save the implicit record sequence number
6768 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6769 * and the caller has to make sure there's space for this.
6770 */
6771
6772static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6773 mbedtls_ssl_transform *transform )
6774{
6775#if defined(MBEDTLS_SSL_PROTO_DTLS)
6776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6777 {
6778 ssl->out_ctr = ssl->out_hdr + 3;
6779 ssl->out_len = ssl->out_hdr + 11;
6780 ssl->out_iv = ssl->out_hdr + 13;
6781 }
6782 else
6783#endif
6784 {
6785 ssl->out_ctr = ssl->out_hdr - 8;
6786 ssl->out_len = ssl->out_hdr + 3;
6787 ssl->out_iv = ssl->out_hdr + 5;
6788 }
6789
6790 /* Adjust out_msg to make space for explicit IV, if used. */
6791 if( transform != NULL &&
6792 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6793 {
6794 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6795 }
6796 else
6797 ssl->out_msg = ssl->out_iv;
6798}
6799
6800/* Once ssl->in_hdr as the address of the beginning of the
6801 * next incoming record is set, deduce the other pointers.
6802 *
6803 * Note: For TLS, we save the implicit record sequence number
6804 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6805 * and the caller has to make sure there's space for this.
6806 */
6807
6808static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6809 mbedtls_ssl_transform *transform )
6810{
6811#if defined(MBEDTLS_SSL_PROTO_DTLS)
6812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6813 {
6814 ssl->in_ctr = ssl->in_hdr + 3;
6815 ssl->in_len = ssl->in_hdr + 11;
6816 ssl->in_iv = ssl->in_hdr + 13;
6817 }
6818 else
6819#endif
6820 {
6821 ssl->in_ctr = ssl->in_hdr - 8;
6822 ssl->in_len = ssl->in_hdr + 3;
6823 ssl->in_iv = ssl->in_hdr + 5;
6824 }
6825
6826 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6827 if( transform != NULL &&
6828 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6829 {
6830 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6831 }
6832 else
6833 ssl->in_msg = ssl->in_iv;
6834}
6835
Paul Bakker5121ce52009-01-03 21:22:43 +00006836/*
6837 * Initialize an SSL context
6838 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006839void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6840{
6841 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6842}
6843
6844/*
6845 * Setup an SSL context
6846 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006847
6848static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6849{
6850 /* Set the incoming and outgoing record pointers. */
6851#if defined(MBEDTLS_SSL_PROTO_DTLS)
6852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6853 {
6854 ssl->out_hdr = ssl->out_buf;
6855 ssl->in_hdr = ssl->in_buf;
6856 }
6857 else
6858#endif /* MBEDTLS_SSL_PROTO_DTLS */
6859 {
6860 ssl->out_hdr = ssl->out_buf + 8;
6861 ssl->in_hdr = ssl->in_buf + 8;
6862 }
6863
6864 /* Derive other internal pointers. */
6865 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6866 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6867}
6868
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006869int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006870 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006871{
Paul Bakker48916f92012-09-16 19:57:18 +00006872 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006873
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006874 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006875
6876 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006877 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006878 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006879
6880 /* Set to NULL in case of an error condition */
6881 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006882
Angus Grattond8213d02016-05-25 20:56:48 +10006883 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6884 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006885 {
Angus Grattond8213d02016-05-25 20:56:48 +10006886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006887 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006888 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006889 }
6890
6891 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6892 if( ssl->out_buf == NULL )
6893 {
6894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006895 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006896 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006897 }
6898
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006899 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006900
Paul Bakker48916f92012-09-16 19:57:18 +00006901 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006902 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006903
6904 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006905
6906error:
6907 mbedtls_free( ssl->in_buf );
6908 mbedtls_free( ssl->out_buf );
6909
6910 ssl->conf = NULL;
6911
6912 ssl->in_buf = NULL;
6913 ssl->out_buf = NULL;
6914
6915 ssl->in_hdr = NULL;
6916 ssl->in_ctr = NULL;
6917 ssl->in_len = NULL;
6918 ssl->in_iv = NULL;
6919 ssl->in_msg = NULL;
6920
6921 ssl->out_hdr = NULL;
6922 ssl->out_ctr = NULL;
6923 ssl->out_len = NULL;
6924 ssl->out_iv = NULL;
6925 ssl->out_msg = NULL;
6926
k-stachowiak9f7798e2018-07-31 16:52:32 +02006927 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006928}
6929
6930/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006931 * Reset an initialized and used SSL context for re-use while retaining
6932 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006933 *
6934 * If partial is non-zero, keep data in the input buffer and client ID.
6935 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006936 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006937static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006938{
Paul Bakker48916f92012-09-16 19:57:18 +00006939 int ret;
6940
Hanno Becker7e772132018-08-10 12:38:21 +01006941#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
6942 !defined(MBEDTLS_SSL_SRV_C)
6943 ((void) partial);
6944#endif
6945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006947
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006948 /* Cancel any possibly running timer */
6949 ssl_set_timer( ssl, 0 );
6950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_RENEGOTIATION)
6952 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006953 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006954
6955 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6957 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006960
Paul Bakker7eb013f2011-10-06 12:37:39 +00006961 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01006962 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006963
6964 ssl->in_msgtype = 0;
6965 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006967 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006968 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006971 ssl_dtls_replay_reset( ssl );
6972#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006973
6974 ssl->in_hslen = 0;
6975 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006976
6977 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006978
6979 ssl->out_msgtype = 0;
6980 ssl->out_msglen = 0;
6981 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6983 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006984 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006985#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006986
Hanno Becker19859472018-08-06 09:40:20 +01006987 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6988
Paul Bakker48916f92012-09-16 19:57:18 +00006989 ssl->transform_in = NULL;
6990 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006991
Hanno Becker78640902018-08-13 16:35:15 +01006992 ssl->session_in = NULL;
6993 ssl->session_out = NULL;
6994
Angus Grattond8213d02016-05-25 20:56:48 +10006995 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01006996
6997#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006998 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01006999#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7000 {
7001 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007002 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007003 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7006 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7009 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7012 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007013 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007014 }
7015#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007016
Paul Bakker48916f92012-09-16 19:57:18 +00007017 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 mbedtls_ssl_transform_free( ssl->transform );
7020 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007021 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007022 }
Paul Bakker48916f92012-09-16 19:57:18 +00007023
Paul Bakkerc0463502013-02-14 11:19:38 +01007024 if( ssl->session )
7025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 mbedtls_ssl_session_free( ssl->session );
7027 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007028 ssl->session = NULL;
7029 }
7030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007032 ssl->alpn_chosen = NULL;
7033#endif
7034
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007035#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007036#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007037 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007038#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007039 {
7040 mbedtls_free( ssl->cli_id );
7041 ssl->cli_id = NULL;
7042 ssl->cli_id_len = 0;
7043 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007044#endif
7045
Paul Bakker48916f92012-09-16 19:57:18 +00007046 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7047 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007048
7049 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007050}
7051
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007052/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007053 * Reset an initialized and used SSL context for re-use while retaining
7054 * all application-set variables, function pointers and data.
7055 */
7056int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7057{
7058 return( ssl_session_reset_int( ssl, 0 ) );
7059}
7060
7061/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007062 * SSL set accessors
7063 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007064void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007065{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007066 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007067}
7068
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007069void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007071 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007072}
7073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007075void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007076{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007077 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007078}
7079#endif
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007082void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007083{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007084 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007085}
7086#endif
7087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007089
Hanno Becker1841b0a2018-08-24 11:13:57 +01007090void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7091 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007092{
7093 ssl->disable_datagram_packing = !allow_packing;
7094}
7095
7096void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7097 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007098{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007099 conf->hs_timeout_min = min;
7100 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007101}
7102#endif
7103
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007104void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007106 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007107}
7108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007110void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007111 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007112 void *p_vrfy )
7113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007114 conf->f_vrfy = f_vrfy;
7115 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007118
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007119void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007120 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 void *p_rng )
7122{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007123 conf->f_rng = f_rng;
7124 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007125}
7126
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007127void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007128 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007129 void *p_dbg )
7130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007131 conf->f_dbg = f_dbg;
7132 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007133}
7134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007136 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007137 mbedtls_ssl_send_t *f_send,
7138 mbedtls_ssl_recv_t *f_recv,
7139 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007140{
7141 ssl->p_bio = p_bio;
7142 ssl->f_send = f_send;
7143 ssl->f_recv = f_recv;
7144 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007145}
7146
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007147#if defined(MBEDTLS_SSL_PROTO_DTLS)
7148void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7149{
7150 ssl->mtu = mtu;
7151}
7152#endif
7153
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007154void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007155{
7156 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007157}
7158
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007159void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7160 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007161 mbedtls_ssl_set_timer_t *f_set_timer,
7162 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007163{
7164 ssl->p_timer = p_timer;
7165 ssl->f_set_timer = f_set_timer;
7166 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007167
7168 /* Make sure we start with no timer running */
7169 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007170}
7171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007173void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007174 void *p_cache,
7175 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7176 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007177{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007178 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007179 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007180 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_SSL_CLI_C)
7185int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007186{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007187 int ret;
7188
7189 if( ssl == NULL ||
7190 session == NULL ||
7191 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007192 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007195 }
7196
7197 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7198 return( ret );
7199
Paul Bakker0a597072012-09-25 21:55:46 +00007200 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007201
7202 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007205
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007206void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007207 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007208{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007209 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7210 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7211 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7212 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007213}
7214
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007215void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007216 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007217 int major, int minor )
7218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007220 return;
7221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007223 return;
7224
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007225 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007226}
7227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007229void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007230 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007231{
7232 conf->cert_profile = profile;
7233}
7234
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007235/* Append a new keycert entry to a (possibly empty) list */
7236static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7237 mbedtls_x509_crt *cert,
7238 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007239{
niisato8ee24222018-06-25 19:05:48 +09007240 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007241
niisato8ee24222018-06-25 19:05:48 +09007242 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7243 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007244 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007245
niisato8ee24222018-06-25 19:05:48 +09007246 new_cert->cert = cert;
7247 new_cert->key = key;
7248 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007249
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007250 /* Update head is the list was null, else add to the end */
7251 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007252 {
niisato8ee24222018-06-25 19:05:48 +09007253 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007254 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007255 else
7256 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007257 mbedtls_ssl_key_cert *cur = *head;
7258 while( cur->next != NULL )
7259 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007260 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007261 }
7262
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007263 return( 0 );
7264}
7265
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007266int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007267 mbedtls_x509_crt *own_cert,
7268 mbedtls_pk_context *pk_key )
7269{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007270 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007271}
7272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007273void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007274 mbedtls_x509_crt *ca_chain,
7275 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007276{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007277 conf->ca_chain = ca_chain;
7278 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007281
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007282#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7283int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7284 mbedtls_x509_crt *own_cert,
7285 mbedtls_pk_context *pk_key )
7286{
7287 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7288 own_cert, pk_key ) );
7289}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007290
7291void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7292 mbedtls_x509_crt *ca_chain,
7293 mbedtls_x509_crl *ca_crl )
7294{
7295 ssl->handshake->sni_ca_chain = ca_chain;
7296 ssl->handshake->sni_ca_crl = ca_crl;
7297}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007298
7299void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7300 int authmode )
7301{
7302 ssl->handshake->sni_authmode = authmode;
7303}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007304#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7305
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007306#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007307/*
7308 * Set EC J-PAKE password for current handshake
7309 */
7310int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7311 const unsigned char *pw,
7312 size_t pw_len )
7313{
7314 mbedtls_ecjpake_role role;
7315
Janos Follath8eb64132016-06-03 15:40:57 +01007316 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7318
7319 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7320 role = MBEDTLS_ECJPAKE_SERVER;
7321 else
7322 role = MBEDTLS_ECJPAKE_CLIENT;
7323
7324 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7325 role,
7326 MBEDTLS_MD_SHA256,
7327 MBEDTLS_ECP_DP_SECP256R1,
7328 pw, pw_len ) );
7329}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007330#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007333int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007334 const unsigned char *psk, size_t psk_len,
7335 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007336{
Paul Bakker6db455e2013-09-18 17:29:31 +02007337 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007342
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007343 /* Identity len will be encoded on two bytes */
7344 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007345 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007346 {
7347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7348 }
7349
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007350 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007351 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007352 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007353
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007354 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007355 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007356 conf->psk_len = 0;
7357 }
7358 if( conf->psk_identity != NULL )
7359 {
7360 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007361 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007362 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007363 }
7364
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007365 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7366 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007367 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007368 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007369 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007370 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007371 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007372 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007373 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007374
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007375 conf->psk_len = psk_len;
7376 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007377
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007378 memcpy( conf->psk, psk, conf->psk_len );
7379 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007380
7381 return( 0 );
7382}
7383
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007384int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7385 const unsigned char *psk, size_t psk_len )
7386{
7387 if( psk == NULL || ssl->handshake == NULL )
7388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7389
7390 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7391 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7392
7393 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007394 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007395 mbedtls_platform_zeroize( ssl->handshake->psk,
7396 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007397 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007398 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007399 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007400
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007401 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007402 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007403
7404 ssl->handshake->psk_len = psk_len;
7405 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7406
7407 return( 0 );
7408}
7409
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007410void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007412 size_t),
7413 void *p_psk )
7414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007415 conf->f_psk = f_psk;
7416 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007419
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007420#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007421
7422#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007423int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007424{
7425 int ret;
7426
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007427 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7428 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7429 {
7430 mbedtls_mpi_free( &conf->dhm_P );
7431 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007432 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007434
7435 return( 0 );
7436}
Hanno Becker470a8c42017-10-04 15:28:46 +01007437#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007438
Hanno Beckera90658f2017-10-04 15:29:08 +01007439int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7440 const unsigned char *dhm_P, size_t P_len,
7441 const unsigned char *dhm_G, size_t G_len )
7442{
7443 int ret;
7444
7445 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7446 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7447 {
7448 mbedtls_mpi_free( &conf->dhm_P );
7449 mbedtls_mpi_free( &conf->dhm_G );
7450 return( ret );
7451 }
7452
7453 return( 0 );
7454}
Paul Bakker5121ce52009-01-03 21:22:43 +00007455
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007456int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007457{
7458 int ret;
7459
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007460 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7461 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7462 {
7463 mbedtls_mpi_free( &conf->dhm_P );
7464 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007465 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007466 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007467
7468 return( 0 );
7469}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007470#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007471
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007472#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7473/*
7474 * Set the minimum length for Diffie-Hellman parameters
7475 */
7476void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7477 unsigned int bitlen )
7478{
7479 conf->dhm_min_bitlen = bitlen;
7480}
7481#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7482
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007483#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007484/*
7485 * Set allowed/preferred hashes for handshake signatures
7486 */
7487void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7488 const int *hashes )
7489{
7490 conf->sig_hashes = hashes;
7491}
Hanno Becker947194e2017-04-07 13:25:49 +01007492#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007493
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007494#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007495/*
7496 * Set the allowed elliptic curves
7497 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007498void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007499 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007500{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007501 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007502}
Hanno Becker947194e2017-04-07 13:25:49 +01007503#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007504
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007505#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007507{
Hanno Becker947194e2017-04-07 13:25:49 +01007508 /* Initialize to suppress unnecessary compiler warning */
7509 size_t hostname_len = 0;
7510
7511 /* Check if new hostname is valid before
7512 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007513 if( hostname != NULL )
7514 {
7515 hostname_len = strlen( hostname );
7516
7517 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7519 }
7520
7521 /* Now it's clear that we will overwrite the old hostname,
7522 * so we can free it safely */
7523
7524 if( ssl->hostname != NULL )
7525 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007526 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007527 mbedtls_free( ssl->hostname );
7528 }
7529
7530 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007531
Paul Bakker5121ce52009-01-03 21:22:43 +00007532 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007533 {
7534 ssl->hostname = NULL;
7535 }
7536 else
7537 {
7538 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007539 if( ssl->hostname == NULL )
7540 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007541
Hanno Becker947194e2017-04-07 13:25:49 +01007542 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007543
Hanno Becker947194e2017-04-07 13:25:49 +01007544 ssl->hostname[hostname_len] = '\0';
7545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007546
7547 return( 0 );
7548}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007549#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007551#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007552void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007554 const unsigned char *, size_t),
7555 void *p_sni )
7556{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007557 conf->f_sni = f_sni;
7558 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007563int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007564{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007565 size_t cur_len, tot_len;
7566 const char **p;
7567
7568 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007569 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7570 * MUST NOT be truncated."
7571 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007572 */
7573 tot_len = 0;
7574 for( p = protos; *p != NULL; p++ )
7575 {
7576 cur_len = strlen( *p );
7577 tot_len += cur_len;
7578
7579 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007581 }
7582
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007583 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007584
7585 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007586}
7587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007589{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007590 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007593
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007594void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007595{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007596 conf->max_major_ver = major;
7597 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007598}
7599
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007600void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007602 conf->min_major_ver = major;
7603 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007604}
7605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007607void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007608{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007609 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007610}
7611#endif
7612
Janos Follath088ce432017-04-10 12:42:31 +01007613#if defined(MBEDTLS_SSL_SRV_C)
7614void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7615 char cert_req_ca_list )
7616{
7617 conf->cert_req_ca_list = cert_req_ca_list;
7618}
7619#endif
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007622void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007624 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007625}
7626#endif
7627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007629void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007630{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007631 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007632}
7633#endif
7634
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007635#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007636void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007638 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007639}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007640#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007643int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007646 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007649 }
7650
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007651 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007652
7653 return( 0 );
7654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007658void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007659{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007660 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007665void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007666{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007667 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007668}
7669#endif
7670
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007671void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007672{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007673 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007674}
7675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007677void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007678{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007679 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007680}
7681
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007682void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007683{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007684 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007685}
7686
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007687void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007688 const unsigned char period[8] )
7689{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007690 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007695#if defined(MBEDTLS_SSL_CLI_C)
7696void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007697{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007698 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007699}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007700#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007701
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007702#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007703void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7704 mbedtls_ssl_ticket_write_t *f_ticket_write,
7705 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7706 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007707{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007708 conf->f_ticket_write = f_ticket_write;
7709 conf->f_ticket_parse = f_ticket_parse;
7710 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007711}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007714
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007715#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7716void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7717 mbedtls_ssl_export_keys_t *f_export_keys,
7718 void *p_export_keys )
7719{
7720 conf->f_export_keys = f_export_keys;
7721 conf->p_export_keys = p_export_keys;
7722}
7723#endif
7724
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007725#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007726void mbedtls_ssl_conf_async_private_cb(
7727 mbedtls_ssl_config *conf,
7728 mbedtls_ssl_async_sign_t *f_async_sign,
7729 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7730 mbedtls_ssl_async_resume_t *f_async_resume,
7731 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007732 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007733{
7734 conf->f_async_sign_start = f_async_sign;
7735 conf->f_async_decrypt_start = f_async_decrypt;
7736 conf->f_async_resume = f_async_resume;
7737 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007738 conf->p_async_config_data = async_config_data;
7739}
7740
Gilles Peskine8f97af72018-04-26 11:46:10 +02007741void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7742{
7743 return( conf->p_async_config_data );
7744}
7745
Gilles Peskine1febfef2018-04-30 11:54:39 +02007746void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007747{
7748 if( ssl->handshake == NULL )
7749 return( NULL );
7750 else
7751 return( ssl->handshake->user_async_ctx );
7752}
7753
Gilles Peskine1febfef2018-04-30 11:54:39 +02007754void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007755 void *ctx )
7756{
7757 if( ssl->handshake != NULL )
7758 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007759}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007760#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007761
Paul Bakker5121ce52009-01-03 21:22:43 +00007762/*
7763 * SSL get accessors
7764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766{
7767 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7768}
7769
Hanno Becker8b170a02017-10-10 11:51:19 +01007770int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7771{
7772 /*
7773 * Case A: We're currently holding back
7774 * a message for further processing.
7775 */
7776
7777 if( ssl->keep_current_message == 1 )
7778 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007780 return( 1 );
7781 }
7782
7783 /*
7784 * Case B: Further records are pending in the current datagram.
7785 */
7786
7787#if defined(MBEDTLS_SSL_PROTO_DTLS)
7788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7789 ssl->in_left > ssl->next_record_offset )
7790 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007791 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007792 return( 1 );
7793 }
7794#endif /* MBEDTLS_SSL_PROTO_DTLS */
7795
7796 /*
7797 * Case C: A handshake message is being processed.
7798 */
7799
Hanno Becker8b170a02017-10-10 11:51:19 +01007800 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7801 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007802 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007803 return( 1 );
7804 }
7805
7806 /*
7807 * Case D: An application data message is being processed
7808 */
7809 if( ssl->in_offt != NULL )
7810 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007812 return( 1 );
7813 }
7814
7815 /*
7816 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007817 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007818 * we implement support for multiple alerts in single records.
7819 */
7820
7821 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7822 return( 0 );
7823}
7824
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007825uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007826{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007827 if( ssl->session != NULL )
7828 return( ssl->session->verify_result );
7829
7830 if( ssl->session_negotiate != NULL )
7831 return( ssl->session_negotiate->verify_result );
7832
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007833 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007834}
7835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007837{
Paul Bakker926c8e42013-03-06 10:23:34 +01007838 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007839 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007842}
7843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007847 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007848 {
7849 switch( ssl->minor_ver )
7850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007852 return( "DTLSv1.0" );
7853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007855 return( "DTLSv1.2" );
7856
7857 default:
7858 return( "unknown (DTLS)" );
7859 }
7860 }
7861#endif
7862
Paul Bakker43ca69c2011-01-15 17:35:19 +00007863 switch( ssl->minor_ver )
7864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007866 return( "SSLv3.0" );
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007869 return( "TLSv1.0" );
7870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007872 return( "TLSv1.1" );
7873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007875 return( "TLSv1.2" );
7876
Paul Bakker43ca69c2011-01-15 17:35:19 +00007877 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007878 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007879 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007880}
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007883{
Hanno Becker3136ede2018-08-17 15:28:19 +01007884 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007886 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007887
Hanno Becker78640902018-08-13 16:35:15 +01007888 if( transform == NULL )
7889 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891#if defined(MBEDTLS_ZLIB_SUPPORT)
7892 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7893 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007894#endif
7895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 case MBEDTLS_MODE_GCM:
7899 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007900 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007902 transform_expansion = transform->minlen;
7903 break;
7904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007906
7907 block_size = mbedtls_cipher_get_block_size(
7908 &transform->cipher_ctx_enc );
7909
Hanno Becker3136ede2018-08-17 15:28:19 +01007910 /* Expansion due to the addition of the MAC. */
7911 transform_expansion += transform->maclen;
7912
7913 /* Expansion due to the addition of CBC padding;
7914 * Theoretically up to 256 bytes, but we never use
7915 * more than the block size of the underlying cipher. */
7916 transform_expansion += block_size;
7917
7918 /* For TLS 1.1 or higher, an explicit IV is added
7919 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007920#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7921 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007922 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007923#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007924
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007925 break;
7926
7927 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007930 }
7931
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007932 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007933}
7934
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007935#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7936size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7937{
7938 size_t max_len;
7939
7940 /*
7941 * Assume mfl_code is correct since it was checked when set
7942 */
Angus Grattond8213d02016-05-25 20:56:48 +10007943 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007944
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007945 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007946 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007947 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007948 {
Angus Grattond8213d02016-05-25 20:56:48 +10007949 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007950 }
7951
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007952 /* During a handshake, use the value being negotiated */
7953 if( ssl->session_negotiate != NULL &&
7954 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7955 {
7956 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7957 }
7958
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007959 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007960}
7961#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7962
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007963#if defined(MBEDTLS_SSL_PROTO_DTLS)
7964static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
7965{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04007966 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
7967 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
7968 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
7969 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
7970 return ( 0 );
7971
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02007972 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
7973 return( ssl->mtu );
7974
7975 if( ssl->mtu == 0 )
7976 return( ssl->handshake->mtu );
7977
7978 return( ssl->mtu < ssl->handshake->mtu ?
7979 ssl->mtu : ssl->handshake->mtu );
7980}
7981#endif /* MBEDTLS_SSL_PROTO_DTLS */
7982
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007983int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7984{
7985 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7986
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02007987#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
7988 !defined(MBEDTLS_SSL_PROTO_DTLS)
7989 (void) ssl;
7990#endif
7991
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007992#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7993 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7994
7995 if( max_len > mfl )
7996 max_len = mfl;
7997#endif
7998
7999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008000 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008001 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008002 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008003 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8004 const size_t overhead = (size_t) ret;
8005
8006 if( ret < 0 )
8007 return( ret );
8008
8009 if( mtu <= overhead )
8010 {
8011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8012 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8013 }
8014
8015 if( max_len > mtu - overhead )
8016 max_len = mtu - overhead;
8017 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008018#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008019
Hanno Becker0defedb2018-08-10 12:35:02 +01008020#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8021 !defined(MBEDTLS_SSL_PROTO_DTLS)
8022 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008023#endif
8024
8025 return( (int) max_len );
8026}
8027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028#if defined(MBEDTLS_X509_CRT_PARSE_C)
8029const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008030{
8031 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008032 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008033
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008034 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#if defined(MBEDTLS_SSL_CLI_C)
8039int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008040{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008041 if( ssl == NULL ||
8042 dst == NULL ||
8043 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008044 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008047 }
8048
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008049 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008050}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008052
Paul Bakker5121ce52009-01-03 21:22:43 +00008053/*
Paul Bakker1961b702013-01-25 14:49:24 +01008054 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008059
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008060 if( ssl == NULL || ssl->conf == NULL )
8061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008064 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008068 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008070#endif
8071
Paul Bakker1961b702013-01-25 14:49:24 +01008072 return( ret );
8073}
8074
8075/*
8076 * Perform the SSL handshake
8077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008079{
8080 int ret = 0;
8081
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008082 if( ssl == NULL || ssl->conf == NULL )
8083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008090
8091 if( ret != 0 )
8092 break;
8093 }
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008096
8097 return( ret );
8098}
8099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_SSL_RENEGOTIATION)
8101#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008102/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008103 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008106{
8107 int ret;
8108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008110
8111 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8113 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008114
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008115 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008116 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008118 return( ret );
8119 }
8120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008122
8123 return( 0 );
8124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008126
8127/*
8128 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 * - any side: calling mbedtls_ssl_renegotiate(),
8130 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8131 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008132 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008133 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008137{
8138 int ret;
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008141
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008142 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8143 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008144
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008145 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8146 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008148 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008150 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008151 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008152 ssl->handshake->out_msg_seq = 1;
8153 else
8154 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008155 }
8156#endif
8157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8159 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008164 return( ret );
8165 }
8166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008168
8169 return( 0 );
8170}
8171
8172/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008173 * Renegotiate current connection on client,
8174 * or request renegotiation on server
8175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008179
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008180 if( ssl == NULL || ssl->conf == NULL )
8181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008184 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008185 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008191
8192 /* Did we already try/start sending HelloRequest? */
8193 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008195
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008196 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008201 /*
8202 * On client, either start the renegotiation process or,
8203 * if already in progress, continue the handshake
8204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008209
8210 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008213 return( ret );
8214 }
8215 }
8216 else
8217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008221 return( ret );
8222 }
8223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008225
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008226 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008227}
8228
8229/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008230 * Check record counters and renegotiate if they're above the limit.
8231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008233{
Andres AG2196c7f2016-12-15 17:01:16 +00008234 size_t ep_len = ssl_ep_len( ssl );
8235 int in_ctr_cmp;
8236 int out_ctr_cmp;
8237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8239 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008240 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008241 {
8242 return( 0 );
8243 }
8244
Andres AG2196c7f2016-12-15 17:01:16 +00008245 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8246 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008247 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008248 ssl->conf->renego_period + ep_len, 8 - ep_len );
8249
8250 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008251 {
8252 return( 0 );
8253 }
8254
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008259
8260/*
8261 * Receive application data decrypted from the SSL layer
8262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008264{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008265 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008266 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008267
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008268 if( ssl == NULL || ssl->conf == NULL )
8269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008277 return( ret );
8278
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008279 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008281 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008282 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008283 return( ret );
8284 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008285 }
8286#endif
8287
Hanno Becker4a810fb2017-05-24 16:27:30 +01008288 /*
8289 * Check if renegotiation is necessary and/or handshake is
8290 * in process. If yes, perform/continue, and fall through
8291 * if an unexpected packet is received while the client
8292 * is waiting for the ServerHello.
8293 *
8294 * (There is no equivalent to the last condition on
8295 * the server-side as it is not treated as within
8296 * a handshake while waiting for the ClientHello
8297 * after a renegotiation request.)
8298 */
8299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008301 ret = ssl_check_ctr_renegotiate( ssl );
8302 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8303 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008306 return( ret );
8307 }
8308#endif
8309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008313 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8314 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008317 return( ret );
8318 }
8319 }
8320
Hanno Beckere41158b2017-10-23 13:30:32 +01008321 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008322 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008323 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008324 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008325 if( ssl->f_get_timer != NULL &&
8326 ssl->f_get_timer( ssl->p_timer ) == -1 )
8327 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008328 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008329 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008330
Hanno Becker327c93b2018-08-15 13:56:18 +01008331 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008332 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008333 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8334 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008335
Hanno Becker4a810fb2017-05-24 16:27:30 +01008336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8337 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008338 }
8339
8340 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008342 {
8343 /*
8344 * OpenSSL sends empty messages to randomize the IV
8345 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008346 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008349 return( 0 );
8350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008352 return( ret );
8353 }
8354 }
8355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008359
Hanno Becker4a810fb2017-05-24 16:27:30 +01008360 /*
8361 * - For client-side, expect SERVER_HELLO_REQUEST.
8362 * - For server-side, expect CLIENT_HELLO.
8363 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8364 */
8365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008369 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008372
8373 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008375 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008376 {
8377 continue;
8378 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008379#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008381 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008382#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008383
Hanno Becker4a810fb2017-05-24 16:27:30 +01008384#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008385 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008389
8390 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008392 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008393 {
8394 continue;
8395 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008398 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008399#endif /* MBEDTLS_SSL_SRV_C */
8400
Hanno Becker21df7f92017-10-17 11:03:26 +01008401#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008402 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008403 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8404 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8405 ssl->conf->allow_legacy_renegotiation ==
8406 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8407 {
8408 /*
8409 * Accept renegotiation request
8410 */
Paul Bakker48916f92012-09-16 19:57:18 +00008411
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008412 /* DTLS clients need to know renego is server-initiated */
8413#if defined(MBEDTLS_SSL_PROTO_DTLS)
8414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8415 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8416 {
8417 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8418 }
8419#endif
8420 ret = ssl_start_renegotiation( ssl );
8421 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8422 ret != 0 )
8423 {
8424 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8425 return( ret );
8426 }
8427 }
8428 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008429#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008430 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008431 /*
8432 * Refuse renegotiation
8433 */
8434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#if defined(MBEDTLS_SSL_PROTO_SSL3)
8438 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008439 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008440 /* SSLv3 does not have a "no_renegotiation" warning, so
8441 we send a fatal alert and abort the connection. */
8442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8443 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8444 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008445 }
8446 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8448#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8449 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8450 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8453 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8454 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008455 {
8456 return( ret );
8457 }
Paul Bakker48916f92012-09-16 19:57:18 +00008458 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008459 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8461 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008465 }
Paul Bakker48916f92012-09-16 19:57:18 +00008466 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008467
Hanno Becker90333da2017-10-10 11:27:13 +01008468 /* At this point, we don't know whether the renegotiation has been
8469 * completed or not. The cases to consider are the following:
8470 * 1) The renegotiation is complete. In this case, no new record
8471 * has been read yet.
8472 * 2) The renegotiation is incomplete because the client received
8473 * an application data record while awaiting the ServerHello.
8474 * 3) The renegotiation is incomplete because the client received
8475 * a non-handshake, non-application data message while awaiting
8476 * the ServerHello.
8477 * In each of these case, looping will be the proper action:
8478 * - For 1), the next iteration will read a new record and check
8479 * if it's application data.
8480 * - For 2), the loop condition isn't satisfied as application data
8481 * is present, hence continue is the same as break
8482 * - For 3), the loop condition is satisfied and read_record
8483 * will re-deliver the message that was held back by the client
8484 * when expecting the ServerHello.
8485 */
8486 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008487 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008488#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008490 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008491 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008492 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008493 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008496 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008497 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008498 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008499 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8504 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008507 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008508 }
8509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8513 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008514 }
8515
8516 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008517
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008518 /* We're going to return something now, cancel timer,
8519 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008521 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008522
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008524 /* If we requested renego but received AppData, resend HelloRequest.
8525 * Do it now, after setting in_offt, to avoid taking this branch
8526 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008528 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008530 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008531 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008534 return( ret );
8535 }
8536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008538#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008539 }
8540
8541 n = ( len < ssl->in_msglen )
8542 ? len : ssl->in_msglen;
8543
8544 memcpy( buf, ssl->in_offt, n );
8545 ssl->in_msglen -= n;
8546
8547 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008548 {
8549 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008550 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008551 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008553 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008554 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008555 /* more data available */
8556 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008560
Paul Bakker23986e52011-04-24 08:57:21 +00008561 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008562}
8563
8564/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008565 * Send application data to be encrypted by the SSL layer, taking care of max
8566 * fragment length and buffer size.
8567 *
8568 * According to RFC 5246 Section 6.2.1:
8569 *
8570 * Zero-length fragments of Application data MAY be sent as they are
8571 * potentially useful as a traffic analysis countermeasure.
8572 *
8573 * Therefore, it is possible that the input message length is 0 and the
8574 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008575 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008576static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008577 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008578{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008579 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8580 const size_t max_len = (size_t) ret;
8581
8582 if( ret < 0 )
8583 {
8584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8585 return( ret );
8586 }
8587
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008588 if( len > max_len )
8589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008591 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008594 "maximum fragment length: %d > %d",
8595 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008597 }
8598 else
8599#endif
8600 len = max_len;
8601 }
Paul Bakker887bd502011-06-08 13:10:54 +00008602
Paul Bakker5121ce52009-01-03 21:22:43 +00008603 if( ssl->out_left != 0 )
8604 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008605 /*
8606 * The user has previously tried to send the data and
8607 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8608 * written. In this case, we expect the high-level write function
8609 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008614 return( ret );
8615 }
8616 }
Paul Bakker887bd502011-06-08 13:10:54 +00008617 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008618 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008619 /*
8620 * The user is trying to send a message the first time, so we need to
8621 * copy the data into the internal buffers and setup the data structure
8622 * to keep track of partial writes
8623 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008624 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008626 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008627
Hanno Becker67bc7c32018-08-06 11:33:50 +01008628 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008631 return( ret );
8632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008633 }
8634
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008635 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008636}
8637
8638/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008639 * Write application data, doing 1/n-1 splitting if necessary.
8640 *
8641 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008642 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008643 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008646static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008647 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008648{
8649 int ret;
8650
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008651 if( ssl->conf->cbc_record_splitting ==
8652 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008653 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8655 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8656 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008657 {
8658 return( ssl_write_real( ssl, buf, len ) );
8659 }
8660
8661 if( ssl->split_done == 0 )
8662 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008663 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008664 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008665 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008666 }
8667
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008668 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8669 return( ret );
8670 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008671
8672 return( ret + 1 );
8673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008675
8676/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008677 * Write application data (public-facing wrapper)
8678 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008679int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008680{
8681 int ret;
8682
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008684
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008685 if( ssl == NULL || ssl->conf == NULL )
8686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8687
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008688#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008689 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8690 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008691 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008692 return( ret );
8693 }
8694#endif
8695
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008696 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008697 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008698 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008699 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008701 return( ret );
8702 }
8703 }
8704
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008705#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008706 ret = ssl_write_split( ssl, buf, len );
8707#else
8708 ret = ssl_write_real( ssl, buf, len );
8709#endif
8710
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008712
8713 return( ret );
8714}
8715
8716/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008717 * Notify the peer that the connection is being closed
8718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008720{
8721 int ret;
8722
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008723 if( ssl == NULL || ssl->conf == NULL )
8724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008727
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008728 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8734 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8735 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008738 return( ret );
8739 }
8740 }
8741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008743
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008744 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008745}
8746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008748{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008749 if( transform == NULL )
8750 return;
8751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008753 deflateEnd( &transform->ctx_deflate );
8754 inflateEnd( &transform->ctx_inflate );
8755#endif
8756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8758 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 mbedtls_md_free( &transform->md_ctx_enc );
8761 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008762
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008763 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008764}
8765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766#if defined(MBEDTLS_X509_CRT_PARSE_C)
8767static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008770
8771 while( cur != NULL )
8772 {
8773 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008775 cur = next;
8776 }
8777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008779
Hanno Becker0271f962018-08-16 13:23:47 +01008780#if defined(MBEDTLS_SSL_PROTO_DTLS)
8781
8782static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8783{
8784 unsigned offset;
8785 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8786
8787 if( hs == NULL )
8788 return;
8789
Hanno Becker283f5ef2018-08-24 09:34:47 +01008790 ssl_free_buffered_record( ssl );
8791
Hanno Becker0271f962018-08-16 13:23:47 +01008792 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008793 ssl_buffering_free_slot( ssl, offset );
8794}
8795
8796static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8797 uint8_t slot )
8798{
8799 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8800 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008801
8802 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8803 return;
8804
Hanno Beckere605b192018-08-21 15:59:07 +01008805 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008806 {
Hanno Beckere605b192018-08-21 15:59:07 +01008807 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008808 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008809 mbedtls_free( hs_buf->data );
8810 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008811 }
8812}
8813
8814#endif /* MBEDTLS_SSL_PROTO_DTLS */
8815
Gilles Peskine9b562d52018-04-25 20:32:43 +02008816void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008817{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008818 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8819
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008820 if( handshake == NULL )
8821 return;
8822
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008823#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8824 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8825 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008826 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008827 handshake->async_in_progress = 0;
8828 }
8829#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8830
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008831#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8832 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8833 mbedtls_md5_free( &handshake->fin_md5 );
8834 mbedtls_sha1_free( &handshake->fin_sha1 );
8835#endif
8836#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8837#if defined(MBEDTLS_SHA256_C)
8838 mbedtls_sha256_free( &handshake->fin_sha256 );
8839#endif
8840#if defined(MBEDTLS_SHA512_C)
8841 mbedtls_sha512_free( &handshake->fin_sha512 );
8842#endif
8843#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845#if defined(MBEDTLS_DHM_C)
8846 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848#if defined(MBEDTLS_ECDH_C)
8849 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008850#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008851#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008852 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008853#if defined(MBEDTLS_SSL_CLI_C)
8854 mbedtls_free( handshake->ecjpake_cache );
8855 handshake->ecjpake_cache = NULL;
8856 handshake->ecjpake_cache_len = 0;
8857#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008858#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008859
Janos Follath4ae5c292016-02-10 11:27:43 +00008860#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8861 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008862 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008864#endif
8865
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008866#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8867 if( handshake->psk != NULL )
8868 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008869 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008870 mbedtls_free( handshake->psk );
8871 }
8872#endif
8873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8875 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008876 /*
8877 * Free only the linked list wrapper, not the keys themselves
8878 * since the belong to the SNI callback
8879 */
8880 if( handshake->sni_key_cert != NULL )
8881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008883
8884 while( cur != NULL )
8885 {
8886 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008888 cur = next;
8889 }
8890 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008892
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008893#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008894 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008895#endif
8896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897#if defined(MBEDTLS_SSL_PROTO_DTLS)
8898 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008899 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008900 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008901#endif
8902
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008903 mbedtls_platform_zeroize( handshake,
8904 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008905}
8906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008907void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008908{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008909 if( session == NULL )
8910 return;
8911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008913 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 mbedtls_x509_crt_free( session->peer_cert );
8916 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008917 }
Paul Bakkered27a042013-04-18 22:46:23 +02008918#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008919
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008920#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008922#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008923
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008924 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008925}
8926
Paul Bakker5121ce52009-01-03 21:22:43 +00008927/*
8928 * Free an SSL context
8929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008931{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008932 if( ssl == NULL )
8933 return;
8934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008936
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008937 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008938 {
Angus Grattond8213d02016-05-25 20:56:48 +10008939 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008941 }
8942
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008943 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008944 {
Angus Grattond8213d02016-05-25 20:56:48 +10008945 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008947 }
8948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008950 if( ssl->compress_buf != NULL )
8951 {
Angus Grattond8213d02016-05-25 20:56:48 +10008952 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008954 }
8955#endif
8956
Paul Bakker48916f92012-09-16 19:57:18 +00008957 if( ssl->transform )
8958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959 mbedtls_ssl_transform_free( ssl->transform );
8960 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008961 }
8962
8963 if( ssl->handshake )
8964 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008965 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8967 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969 mbedtls_free( ssl->handshake );
8970 mbedtls_free( ssl->transform_negotiate );
8971 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008972 }
8973
Paul Bakkerc0463502013-02-14 11:19:38 +01008974 if( ssl->session )
8975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976 mbedtls_ssl_session_free( ssl->session );
8977 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008978 }
8979
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008980#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008981 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008982 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008983 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008985 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8989 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8992 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008993 }
8994#endif
8995
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008996#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008998#endif
8999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009001
Paul Bakker86f04f42013-02-14 11:20:09 +01009002 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009003 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009004}
9005
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009006/*
9007 * Initialze mbedtls_ssl_config
9008 */
9009void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9010{
9011 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9012}
9013
Simon Butcherc97b6972015-12-27 23:48:17 +00009014#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009015static int ssl_preset_default_hashes[] = {
9016#if defined(MBEDTLS_SHA512_C)
9017 MBEDTLS_MD_SHA512,
9018 MBEDTLS_MD_SHA384,
9019#endif
9020#if defined(MBEDTLS_SHA256_C)
9021 MBEDTLS_MD_SHA256,
9022 MBEDTLS_MD_SHA224,
9023#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009024#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009025 MBEDTLS_MD_SHA1,
9026#endif
9027 MBEDTLS_MD_NONE
9028};
Simon Butcherc97b6972015-12-27 23:48:17 +00009029#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009030
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009031static int ssl_preset_suiteb_ciphersuites[] = {
9032 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9033 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9034 0
9035};
9036
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009037#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009038static int ssl_preset_suiteb_hashes[] = {
9039 MBEDTLS_MD_SHA256,
9040 MBEDTLS_MD_SHA384,
9041 MBEDTLS_MD_NONE
9042};
9043#endif
9044
9045#if defined(MBEDTLS_ECP_C)
9046static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9047 MBEDTLS_ECP_DP_SECP256R1,
9048 MBEDTLS_ECP_DP_SECP384R1,
9049 MBEDTLS_ECP_DP_NONE
9050};
9051#endif
9052
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009053/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009054 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009055 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009056int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009057 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009058{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009059#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009060 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009061#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009062
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009063 /* Use the functions here so that they are covered in tests,
9064 * but otherwise access member directly for efficiency */
9065 mbedtls_ssl_conf_endpoint( conf, endpoint );
9066 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009067
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009068 /*
9069 * Things that are common to all presets
9070 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009071#if defined(MBEDTLS_SSL_CLI_C)
9072 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9073 {
9074 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9075#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9076 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9077#endif
9078 }
9079#endif
9080
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009081#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009082 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009083#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009084
9085#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9086 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9087#endif
9088
9089#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9090 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9091#endif
9092
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009093#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9094 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9095#endif
9096
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009097#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009098 conf->f_cookie_write = ssl_cookie_write_dummy;
9099 conf->f_cookie_check = ssl_cookie_check_dummy;
9100#endif
9101
9102#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9103 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9104#endif
9105
Janos Follath088ce432017-04-10 12:42:31 +01009106#if defined(MBEDTLS_SSL_SRV_C)
9107 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9108#endif
9109
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009110#if defined(MBEDTLS_SSL_PROTO_DTLS)
9111 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9112 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9113#endif
9114
9115#if defined(MBEDTLS_SSL_RENEGOTIATION)
9116 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009117 memset( conf->renego_period, 0x00, 2 );
9118 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009119#endif
9120
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009121#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9122 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9123 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009124 const unsigned char dhm_p[] =
9125 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9126 const unsigned char dhm_g[] =
9127 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9128
Hanno Beckera90658f2017-10-04 15:29:08 +01009129 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9130 dhm_p, sizeof( dhm_p ),
9131 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009132 {
9133 return( ret );
9134 }
9135 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009136#endif
9137
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009138 /*
9139 * Preset-specific defaults
9140 */
9141 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009142 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009143 /*
9144 * NSA Suite B
9145 */
9146 case MBEDTLS_SSL_PRESET_SUITEB:
9147 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9148 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9149 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9150 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9151
9152 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9153 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9154 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9155 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9156 ssl_preset_suiteb_ciphersuites;
9157
9158#if defined(MBEDTLS_X509_CRT_PARSE_C)
9159 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009160#endif
9161
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009162#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009163 conf->sig_hashes = ssl_preset_suiteb_hashes;
9164#endif
9165
9166#if defined(MBEDTLS_ECP_C)
9167 conf->curve_list = ssl_preset_suiteb_curves;
9168#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009169 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009170
9171 /*
9172 * Default
9173 */
9174 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009175 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9176 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9177 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9178 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9179 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9180 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9181 MBEDTLS_SSL_MIN_MINOR_VERSION :
9182 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009183 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9184 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9185
9186#if defined(MBEDTLS_SSL_PROTO_DTLS)
9187 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9188 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9189#endif
9190
9191 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9192 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9193 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9194 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9195 mbedtls_ssl_list_ciphersuites();
9196
9197#if defined(MBEDTLS_X509_CRT_PARSE_C)
9198 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9199#endif
9200
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009201#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009202 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009203#endif
9204
9205#if defined(MBEDTLS_ECP_C)
9206 conf->curve_list = mbedtls_ecp_grp_id_list();
9207#endif
9208
9209#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9210 conf->dhm_min_bitlen = 1024;
9211#endif
9212 }
9213
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009214 return( 0 );
9215}
9216
9217/*
9218 * Free mbedtls_ssl_config
9219 */
9220void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9221{
9222#if defined(MBEDTLS_DHM_C)
9223 mbedtls_mpi_free( &conf->dhm_P );
9224 mbedtls_mpi_free( &conf->dhm_G );
9225#endif
9226
9227#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9228 if( conf->psk != NULL )
9229 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009230 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009231 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009232 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009233 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009234 }
9235
9236 if( conf->psk_identity != NULL )
9237 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009238 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009239 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009240 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009241 conf->psk_identity_len = 0;
9242 }
9243#endif
9244
9245#if defined(MBEDTLS_X509_CRT_PARSE_C)
9246 ssl_key_cert_free( conf->key_cert );
9247#endif
9248
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009249 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009250}
9251
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009252#if defined(MBEDTLS_PK_C) && \
9253 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009254/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259#if defined(MBEDTLS_RSA_C)
9260 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9261 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009263#if defined(MBEDTLS_ECDSA_C)
9264 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9265 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009268}
9269
Hanno Becker7e5437a2017-04-28 17:15:26 +01009270unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9271{
9272 switch( type ) {
9273 case MBEDTLS_PK_RSA:
9274 return( MBEDTLS_SSL_SIG_RSA );
9275 case MBEDTLS_PK_ECDSA:
9276 case MBEDTLS_PK_ECKEY:
9277 return( MBEDTLS_SSL_SIG_ECDSA );
9278 default:
9279 return( MBEDTLS_SSL_SIG_ANON );
9280 }
9281}
9282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009284{
9285 switch( sig )
9286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287#if defined(MBEDTLS_RSA_C)
9288 case MBEDTLS_SSL_SIG_RSA:
9289 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291#if defined(MBEDTLS_ECDSA_C)
9292 case MBEDTLS_SSL_SIG_ECDSA:
9293 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009294#endif
9295 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009297 }
9298}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009299#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009300
Hanno Becker7e5437a2017-04-28 17:15:26 +01009301#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9302 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9303
9304/* Find an entry in a signature-hash set matching a given hash algorithm. */
9305mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9306 mbedtls_pk_type_t sig_alg )
9307{
9308 switch( sig_alg )
9309 {
9310 case MBEDTLS_PK_RSA:
9311 return( set->rsa );
9312 case MBEDTLS_PK_ECDSA:
9313 return( set->ecdsa );
9314 default:
9315 return( MBEDTLS_MD_NONE );
9316 }
9317}
9318
9319/* Add a signature-hash-pair to a signature-hash set */
9320void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9321 mbedtls_pk_type_t sig_alg,
9322 mbedtls_md_type_t md_alg )
9323{
9324 switch( sig_alg )
9325 {
9326 case MBEDTLS_PK_RSA:
9327 if( set->rsa == MBEDTLS_MD_NONE )
9328 set->rsa = md_alg;
9329 break;
9330
9331 case MBEDTLS_PK_ECDSA:
9332 if( set->ecdsa == MBEDTLS_MD_NONE )
9333 set->ecdsa = md_alg;
9334 break;
9335
9336 default:
9337 break;
9338 }
9339}
9340
9341/* Allow exactly one hash algorithm for each signature. */
9342void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9343 mbedtls_md_type_t md_alg )
9344{
9345 set->rsa = md_alg;
9346 set->ecdsa = md_alg;
9347}
9348
9349#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9350 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9351
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009352/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009353 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009356{
9357 switch( hash )
9358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359#if defined(MBEDTLS_MD5_C)
9360 case MBEDTLS_SSL_HASH_MD5:
9361 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363#if defined(MBEDTLS_SHA1_C)
9364 case MBEDTLS_SSL_HASH_SHA1:
9365 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367#if defined(MBEDTLS_SHA256_C)
9368 case MBEDTLS_SSL_HASH_SHA224:
9369 return( MBEDTLS_MD_SHA224 );
9370 case MBEDTLS_SSL_HASH_SHA256:
9371 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373#if defined(MBEDTLS_SHA512_C)
9374 case MBEDTLS_SSL_HASH_SHA384:
9375 return( MBEDTLS_MD_SHA384 );
9376 case MBEDTLS_SSL_HASH_SHA512:
9377 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009378#endif
9379 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009381 }
9382}
9383
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009384/*
9385 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9386 */
9387unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9388{
9389 switch( md )
9390 {
9391#if defined(MBEDTLS_MD5_C)
9392 case MBEDTLS_MD_MD5:
9393 return( MBEDTLS_SSL_HASH_MD5 );
9394#endif
9395#if defined(MBEDTLS_SHA1_C)
9396 case MBEDTLS_MD_SHA1:
9397 return( MBEDTLS_SSL_HASH_SHA1 );
9398#endif
9399#if defined(MBEDTLS_SHA256_C)
9400 case MBEDTLS_MD_SHA224:
9401 return( MBEDTLS_SSL_HASH_SHA224 );
9402 case MBEDTLS_MD_SHA256:
9403 return( MBEDTLS_SSL_HASH_SHA256 );
9404#endif
9405#if defined(MBEDTLS_SHA512_C)
9406 case MBEDTLS_MD_SHA384:
9407 return( MBEDTLS_SSL_HASH_SHA384 );
9408 case MBEDTLS_MD_SHA512:
9409 return( MBEDTLS_SSL_HASH_SHA512 );
9410#endif
9411 default:
9412 return( MBEDTLS_SSL_HASH_NONE );
9413 }
9414}
9415
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009416#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009417/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009418 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009419 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009420 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009421int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009424
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009425 if( ssl->conf->curve_list == NULL )
9426 return( -1 );
9427
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009428 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009429 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009430 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009431
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009432 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009433}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009434#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009435
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009436#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009437/*
9438 * Check if a hash proposed by the peer is in our list.
9439 * Return 0 if we're willing to use it, -1 otherwise.
9440 */
9441int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9442 mbedtls_md_type_t md )
9443{
9444 const int *cur;
9445
9446 if( ssl->conf->sig_hashes == NULL )
9447 return( -1 );
9448
9449 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9450 if( *cur == (int) md )
9451 return( 0 );
9452
9453 return( -1 );
9454}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009455#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#if defined(MBEDTLS_X509_CRT_PARSE_C)
9458int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9459 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009460 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009461 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009462{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009463 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009465 int usage = 0;
9466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009468 const char *ext_oid;
9469 size_t ext_len;
9470#endif
9471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9473 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009474 ((void) cert);
9475 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009476 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009477#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9480 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009481 {
9482 /* Server part of the key exchange */
9483 switch( ciphersuite->key_exchange )
9484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 case MBEDTLS_KEY_EXCHANGE_RSA:
9486 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009487 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009488 break;
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9491 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9492 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9493 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009494 break;
9495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9497 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009498 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009499 break;
9500
9501 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 case MBEDTLS_KEY_EXCHANGE_NONE:
9503 case MBEDTLS_KEY_EXCHANGE_PSK:
9504 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9505 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009506 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009507 usage = 0;
9508 }
9509 }
9510 else
9511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9513 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009514 }
9515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009517 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009518 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009519 ret = -1;
9520 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009521#else
9522 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9526 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9529 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009530 }
9531 else
9532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9534 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009535 }
9536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009538 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009539 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009540 ret = -1;
9541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009543
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009544 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009547
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009548/*
9549 * Convert version numbers to/from wire format
9550 * and, for DTLS, to/from TLS equivalent.
9551 *
9552 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009553 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009554 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9555 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009558 unsigned char ver[2] )
9559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560#if defined(MBEDTLS_SSL_PROTO_DTLS)
9561 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009564 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9565
9566 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9567 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9568 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009569 else
9570#else
9571 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009572#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009573 {
9574 ver[0] = (unsigned char) major;
9575 ver[1] = (unsigned char) minor;
9576 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009577}
9578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009580 const unsigned char ver[2] )
9581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#if defined(MBEDTLS_SSL_PROTO_DTLS)
9583 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009584 {
9585 *major = 255 - ver[0] + 2;
9586 *minor = 255 - ver[1] + 1;
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009589 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9590 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009591 else
9592#else
9593 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009594#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009595 {
9596 *major = ver[0];
9597 *minor = ver[1];
9598 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009599}
9600
Simon Butcher99000142016-10-13 17:21:01 +01009601int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9602{
9603#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9604 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9605 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9606
9607 switch( md )
9608 {
9609#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9610#if defined(MBEDTLS_MD5_C)
9611 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009612 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009613#endif
9614#if defined(MBEDTLS_SHA1_C)
9615 case MBEDTLS_SSL_HASH_SHA1:
9616 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9617 break;
9618#endif
9619#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9620#if defined(MBEDTLS_SHA512_C)
9621 case MBEDTLS_SSL_HASH_SHA384:
9622 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9623 break;
9624#endif
9625#if defined(MBEDTLS_SHA256_C)
9626 case MBEDTLS_SSL_HASH_SHA256:
9627 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9628 break;
9629#endif
9630 default:
9631 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9632 }
9633
9634 return 0;
9635#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9636 (void) ssl;
9637 (void) md;
9638
9639 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9640#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9641}
9642
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009643#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9644 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9645int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9646 unsigned char *output,
9647 unsigned char *data, size_t data_len )
9648{
9649 int ret = 0;
9650 mbedtls_md5_context mbedtls_md5;
9651 mbedtls_sha1_context mbedtls_sha1;
9652
9653 mbedtls_md5_init( &mbedtls_md5 );
9654 mbedtls_sha1_init( &mbedtls_sha1 );
9655
9656 /*
9657 * digitally-signed struct {
9658 * opaque md5_hash[16];
9659 * opaque sha_hash[20];
9660 * };
9661 *
9662 * md5_hash
9663 * MD5(ClientHello.random + ServerHello.random
9664 * + ServerParams);
9665 * sha_hash
9666 * SHA(ClientHello.random + ServerHello.random
9667 * + ServerParams);
9668 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009669 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009670 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009672 goto exit;
9673 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009674 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009675 ssl->handshake->randbytes, 64 ) ) != 0 )
9676 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009678 goto exit;
9679 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009680 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009681 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009683 goto exit;
9684 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009685 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009686 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009688 goto exit;
9689 }
9690
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009691 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009692 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009694 goto exit;
9695 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009696 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009697 ssl->handshake->randbytes, 64 ) ) != 0 )
9698 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009699 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009700 goto exit;
9701 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009702 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009703 data_len ) ) != 0 )
9704 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009706 goto exit;
9707 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009708 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009709 output + 16 ) ) != 0 )
9710 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009712 goto exit;
9713 }
9714
9715exit:
9716 mbedtls_md5_free( &mbedtls_md5 );
9717 mbedtls_sha1_free( &mbedtls_sha1 );
9718
9719 if( ret != 0 )
9720 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9721 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9722
9723 return( ret );
9724
9725}
9726#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9727 MBEDTLS_SSL_PROTO_TLS1_1 */
9728
9729#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9730 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9731int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009732 unsigned char *hash, size_t *hashlen,
9733 unsigned char *data, size_t data_len,
9734 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009735{
9736 int ret = 0;
9737 mbedtls_md_context_t ctx;
9738 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009739 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009740
9741 mbedtls_md_init( &ctx );
9742
9743 /*
9744 * digitally-signed struct {
9745 * opaque client_random[32];
9746 * opaque server_random[32];
9747 * ServerDHParams params;
9748 * };
9749 */
9750 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9751 {
9752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9753 goto exit;
9754 }
9755 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9756 {
9757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9758 goto exit;
9759 }
9760 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9761 {
9762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9763 goto exit;
9764 }
9765 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9766 {
9767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9768 goto exit;
9769 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009770 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009771 {
9772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9773 goto exit;
9774 }
9775
9776exit:
9777 mbedtls_md_free( &ctx );
9778
9779 if( ret != 0 )
9780 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9781 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9782
9783 return( ret );
9784}
9785#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9786 MBEDTLS_SSL_PROTO_TLS1_2 */
9787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788#endif /* MBEDTLS_SSL_TLS_C */