blob: f7570d85c249eba081a9d440947deb595453b05a [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 Becker3307b532017-12-27 21:37:21 +0000632#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
633 transform->encrypt_then_mac = session->encrypt_then_mac;
634#endif
635 transform->minor_ver = ssl->minor_ver;
636
Hanno Becker8759e162017-12-27 21:34:08 +0000637 ciphersuite_info = handshake->ciphersuite_info;
638 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100639 if( cipher_info == NULL )
640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000642 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100644 }
645
Hanno Becker8759e162017-12-27 21:34:08 +0000646 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100647 if( md_info == NULL )
648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000650 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100652 }
653
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000655 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_SSL_PROTO_SSL3)
658 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000659 {
Paul Bakker48916f92012-09-16 19:57:18 +0000660 handshake->tls_prf = ssl3_prf;
661 handshake->calc_verify = ssl_calc_verify_ssl;
662 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000663 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200664 else
665#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
667 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 handshake->tls_prf = tls1_prf;
670 handshake->calc_verify = ssl_calc_verify_tls;
671 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200673 else
674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
676#if defined(MBEDTLS_SHA512_C)
677 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Becker8759e162017-12-27 21:34:08 +0000678 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000679 {
Paul Bakker48916f92012-09-16 19:57:18 +0000680 handshake->tls_prf = tls_prf_sha384;
681 handshake->calc_verify = ssl_calc_verify_tls_sha384;
682 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000683 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000684 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_SHA256_C)
687 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000688 {
Paul Bakker48916f92012-09-16 19:57:18 +0000689 handshake->tls_prf = tls_prf_sha256;
690 handshake->calc_verify = ssl_calc_verify_tls_sha256;
691 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000692 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693 else
694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200699 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000700
701 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 * SSLv3:
703 * master =
704 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
705 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
706 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200707 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 * master = PRF( premaster, "master secret", randbytes )[0..47]
710 */
Paul Bakker0a597072012-09-25 21:55:46 +0000711 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000714 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
717 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200718 {
719 unsigned char session_hash[48];
720 size_t hash_len;
721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200723
724 ssl->handshake->calc_verify( ssl, session_hash );
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
727 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_SHA512_C)
Hanno Becker8759e162017-12-27 21:34:08 +0000730 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200731 {
732 hash_len = 48;
733 }
734 else
735#endif
736 hash_len = 32;
737 }
738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200740 hash_len = 36;
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200743
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100744 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
745 "extended master secret",
746 session_hash, hash_len,
747 session->master, 48 );
748 if( ret != 0 )
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100751 return( ret );
752 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200753
754 }
755 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200756#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100757 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
758 "master secret",
759 handshake->randbytes, 64,
760 session->master, 48 );
761 if( ret != 0 )
762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100764 return( ret );
765 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200766
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500767 mbedtls_platform_zeroize( handshake->premaster,
768 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 }
770 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773 /*
774 * Swap the client and server random values.
775 */
Paul Bakker48916f92012-09-16 19:57:18 +0000776 memcpy( tmp, handshake->randbytes, 64 );
777 memcpy( handshake->randbytes, tmp + 32, 32 );
778 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500779 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
781 /*
782 * SSLv3:
783 * key block =
784 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
785 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
786 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
787 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
788 * ...
789 *
790 * TLSv1:
791 * key block = PRF( master, "key expansion", randbytes )
792 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100793 ret = handshake->tls_prf( session->master, 48, "key expansion",
794 handshake->randbytes, 64, keyblk, 256 );
795 if( ret != 0 )
796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100798 return( ret );
799 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
802 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
803 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
804 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
805 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500807 mbedtls_platform_zeroize( handshake->randbytes,
808 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
810 /*
811 * Determine the appropriate key, IV and MAC length.
812 */
Paul Bakker68884e32013-01-07 18:20:04 +0100813
Hanno Beckere7f2df02017-12-27 08:17:40 +0000814 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200817 cipher_info->mode == MBEDTLS_MODE_CCM ||
818 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 {
Hanno Becker8759e162017-12-27 21:34:08 +0000820 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200821
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200822 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000823 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000824 transform->taglen =
825 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200826
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200827 /* All modes haves 96-bit IVs;
828 * GCM and CCM has 4 implicit and 8 explicit bytes
829 * ChachaPoly has all 12 bytes implicit
830 */
Paul Bakker68884e32013-01-07 18:20:04 +0100831 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200832 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
833 transform->fixed_ivlen = 12;
834 else
835 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200836
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200837 /* Minimum length of encrypted record */
838 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000839 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100840 }
841 else
842 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200843 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
845 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200848 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100849 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200851 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000852 mac_key_len = mbedtls_md_get_size( md_info );
853 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200856 /*
857 * If HMAC is to be truncated, we shall keep the leftmost bytes,
858 * (rfc 6066 page 13 or rfc 2104 section 4),
859 * so we only need to adjust the length here.
860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000864
865#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
866 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000867 * HMAC implementation which also truncates the key
868 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000869 mac_key_len = transform->maclen;
870#endif
871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200873
874 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100875 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200877 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200879 transform->minlen = transform->maclen;
880 else
Paul Bakker68884e32013-01-07 18:20:04 +0100881 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200882 /*
883 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100884 * 1. if EtM is in use: one block plus MAC
885 * otherwise: * first multiple of blocklen greater than maclen
886 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
889 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100890 {
891 transform->minlen = transform->maclen
892 + cipher_info->block_size;
893 }
894 else
895#endif
896 {
897 transform->minlen = transform->maclen
898 + cipher_info->block_size
899 - transform->maclen % cipher_info->block_size;
900 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
903 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
904 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200905 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100906 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
909 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
910 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200911 {
912 transform->minlen += transform->ivlen;
913 }
914 else
915#endif
916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200919 }
Paul Bakker68884e32013-01-07 18:20:04 +0100920 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 }
922
Hanno Beckere7f2df02017-12-27 08:17:40 +0000923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
924 (unsigned) keylen,
925 (unsigned) transform->minlen,
926 (unsigned) transform->ivlen,
927 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
929 /*
930 * Finally setup the cipher contexts, IVs and MAC secrets.
931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200933 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000934 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000935 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000936 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Paul Bakker68884e32013-01-07 18:20:04 +0100938 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000939 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000941 /*
942 * This is not used in TLS v1.1.
943 */
Paul Bakker48916f92012-09-16 19:57:18 +0000944 iv_copy_len = ( transform->fixed_ivlen ) ?
945 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000946 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
947 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000948 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 }
950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#endif /* MBEDTLS_SSL_CLI_C */
952#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200953 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000955 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +0000956 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Hanno Becker81c7b182017-11-09 18:39:33 +0000958 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100959 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000961 /*
962 * This is not used in TLS v1.1.
963 */
Paul Bakker48916f92012-09-16 19:57:18 +0000964 iv_copy_len = ( transform->fixed_ivlen ) ?
965 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000966 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
967 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000968 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100970 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100975 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_SSL_PROTO_SSL3)
978 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100979 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000980 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100984 }
985
Hanno Becker81c7b182017-11-09 18:39:33 +0000986 memcpy( transform->mac_enc, mac_enc, mac_key_len );
987 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100988 }
989 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_SSL_PROTO_SSL3 */
991#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
992 defined(MBEDTLS_SSL_PROTO_TLS1_2)
993 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100994 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100995 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
996 For AEAD-based ciphersuites, there is nothing to do here. */
997 if( mac_key_len != 0 )
998 {
999 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1000 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1001 }
Paul Bakker68884e32013-01-07 18:20:04 +01001002 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001003 else
1004#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001008 }
Paul Bakker68884e32013-01-07 18:20:04 +01001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1011 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001012 {
1013 int ret = 0;
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001016
Hanno Beckere7f2df02017-12-27 08:17:40 +00001017 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001018 transform->iv_enc, transform->iv_dec,
1019 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001020 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001021 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1024 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001025 }
1026 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001028
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001029#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1030 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001031 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001032 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1033 session->master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001034 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001035 iv_copy_len );
1036 }
1037#endif
1038
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001039 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001040 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001043 return( ret );
1044 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001045
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001046 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001047 cipher_info ) ) != 0 )
1048 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001050 return( ret );
1051 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001054 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001058 return( ret );
1059 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001062 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001066 return( ret );
1067 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_CIPHER_MODE_CBC)
1070 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1073 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001076 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001077 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1080 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001083 return( ret );
1084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001088 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001091 // Initialize compression
1092 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001094 {
Paul Bakker16770332013-10-11 09:59:44 +02001095 if( ssl->compress_buf == NULL )
1096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001098 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001099 if( ssl->compress_buf == NULL )
1100 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001102 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001103 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001104 }
1105 }
1106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001108
Paul Bakker48916f92012-09-16 19:57:18 +00001109 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1110 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001111
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001112 if( deflateInit( &transform->ctx_deflate,
1113 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001114 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1117 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001118 }
1119 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
1124 return( 0 );
1125}
1126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_SSL_PROTO_SSL3)
1128void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001129{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001130 mbedtls_md5_context md5;
1131 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 unsigned char pad_1[48];
1133 unsigned char pad_2[48];
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001137 mbedtls_md5_init( &md5 );
1138 mbedtls_sha1_init( &sha1 );
1139
1140 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1141 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Paul Bakker380da532012-04-18 16:10:25 +00001143 memset( pad_1, 0x36, 48 );
1144 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001146 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1147 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1148 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001150 mbedtls_md5_starts_ret( &md5 );
1151 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1152 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1153 mbedtls_md5_update_ret( &md5, hash, 16 );
1154 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001156 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1157 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1158 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001159
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001160 mbedtls_sha1_starts_ret( &sha1 );
1161 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1162 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1163 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1164 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001168
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001169 mbedtls_md5_free( &md5 );
1170 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001171
Paul Bakker380da532012-04-18 16:10:25 +00001172 return;
1173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1177void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001178{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001179 mbedtls_md5_context md5;
1180 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001183
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001184 mbedtls_md5_init( &md5 );
1185 mbedtls_sha1_init( &sha1 );
1186
1187 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1188 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001189
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001190 mbedtls_md5_finish_ret( &md5, hash );
1191 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001195
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001196 mbedtls_md5_free( &md5 );
1197 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001198
Paul Bakker380da532012-04-18 16:10:25 +00001199 return;
1200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1204#if defined(MBEDTLS_SHA256_C)
1205void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001206{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001207 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001208
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001209 mbedtls_sha256_init( &sha256 );
1210
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001212
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001213 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001214 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001218
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001219 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001220
Paul Bakker380da532012-04-18 16:10:25 +00001221 return;
1222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_SHA512_C)
1226void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001227{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001228 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001229
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001230 mbedtls_sha512_init( &sha512 );
1231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001233
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001234 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001235 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001240 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001241
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 return;
1243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#endif /* MBEDTLS_SHA512_C */
1245#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1248int 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 +02001249{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001250 unsigned char *p = ssl->handshake->premaster;
1251 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001252 const unsigned char *psk = ssl->conf->psk;
1253 size_t psk_len = ssl->conf->psk_len;
1254
1255 /* If the psk callback was called, use its result */
1256 if( ssl->handshake->psk != NULL )
1257 {
1258 psk = ssl->handshake->psk;
1259 psk_len = ssl->handshake->psk_len;
1260 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001261
1262 /*
1263 * PMS = struct {
1264 * opaque other_secret<0..2^16-1>;
1265 * opaque psk<0..2^16-1>;
1266 * };
1267 * with "other_secret" depending on the particular key exchange
1268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1270 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001271 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001272 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001274
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001275 *(p++) = (unsigned char)( psk_len >> 8 );
1276 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001277
1278 if( end < p || (size_t)( end - p ) < psk_len )
1279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1280
1281 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001282 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001283 }
1284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1286#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1287 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001288 {
1289 /*
1290 * other_secret already set by the ClientKeyExchange message,
1291 * and is 48 bytes long
1292 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001293 if( end - p < 2 )
1294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1295
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001296 *p++ = 0;
1297 *p++ = 48;
1298 p += 48;
1299 }
1300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1302#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1303 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001304 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001305 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001306 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001307
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001308 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001310 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001311 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001314 return( ret );
1315 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001316 *(p++) = (unsigned char)( len >> 8 );
1317 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001318 p += len;
1319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001321 }
1322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1324#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1325 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001326 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001327 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001328 size_t zlen;
1329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001331 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001332 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001335 return( ret );
1336 }
1337
1338 *(p++) = (unsigned char)( zlen >> 8 );
1339 *(p++) = (unsigned char)( zlen );
1340 p += zlen;
1341
Janos Follath3fbdada2018-08-15 10:26:53 +01001342 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1343 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001344 }
1345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001350 }
1351
1352 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001353 if( end - p < 2 )
1354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001355
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001356 *(p++) = (unsigned char)( psk_len >> 8 );
1357 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001358
1359 if( end < p || (size_t)( end - p ) < psk_len )
1360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1361
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001362 memcpy( p, psk, psk_len );
1363 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001364
1365 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1366
1367 return( 0 );
1368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001372/*
1373 * SSLv3.0 MAC functions
1374 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001375#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001376static void ssl_mac( mbedtls_md_context_t *md_ctx,
1377 const unsigned char *secret,
1378 const unsigned char *buf, size_t len,
1379 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001380 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381{
1382 unsigned char header[11];
1383 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001384 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1386 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001387
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001388 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001390 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001391 else
Paul Bakker68884e32013-01-07 18:20:04 +01001392 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
1394 memcpy( header, ctr, 8 );
1395 header[ 8] = (unsigned char) type;
1396 header[ 9] = (unsigned char)( len >> 8 );
1397 header[10] = (unsigned char)( len );
1398
Paul Bakker68884e32013-01-07 18:20:04 +01001399 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_md_starts( md_ctx );
1401 mbedtls_md_update( md_ctx, secret, md_size );
1402 mbedtls_md_update( md_ctx, padding, padlen );
1403 mbedtls_md_update( md_ctx, header, 11 );
1404 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001405 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Paul Bakker68884e32013-01-07 18:20:04 +01001407 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_md_starts( md_ctx );
1409 mbedtls_md_update( md_ctx, secret, md_size );
1410 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001411 mbedtls_md_update( md_ctx, out, md_size );
1412 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001415
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001416/* The function below is only used in the Lucky 13 counter-measure in
1417 * ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001418#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001419 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1420 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1421 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1422/* This function makes sure every byte in the memory region is accessed
1423 * (in ascending addresses order) */
1424static void ssl_read_memory( unsigned char *p, size_t len )
1425{
1426 unsigned char acc = 0;
1427 volatile unsigned char force;
1428
1429 for( ; len != 0; p++, len-- )
1430 acc ^= *p;
1431
1432 force = acc;
1433 (void) force;
1434}
1435#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1436
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001437/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001439 */
Hanno Becker3307b532017-12-27 21:37:21 +00001440
1441static void ssl_extract_add_data_from_record( unsigned char* add_data,
1442 mbedtls_record *rec )
1443{
1444 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1445 add_data[8] = rec->type;
1446 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1447 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1448 add_data[12] = rec->data_len & 0xFF;
1449}
1450
1451static int ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1452 mbedtls_ssl_transform *transform,
1453 mbedtls_record *rec,
1454 int (*f_rng)(void *, unsigned char *, size_t),
1455 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001458 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001459 unsigned char * data;
1460 unsigned char add_data[13];
1461 size_t post_avail;
1462
1463 /* The SSL context is only used for debugging purposes! */
1464#if !defined(MBEDTLS_SSL_DEBUG_C)
1465 ((void) ssl);
1466#endif
1467
1468 /* The PRNG is used for dynamic IV generation that's used
1469 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1470#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1471 ( defined(MBEDTLS_AES_C) || \
1472 defined(MBEDTLS_ARIA_C) || \
1473 defined(MBEDTLS_CAMELLIA_C) ) && \
1474 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1475 ((void) f_rng);
1476 ((void) p_rng);
1477#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001480
Hanno Becker3307b532017-12-27 21:37:21 +00001481 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001482 {
Hanno Becker3307b532017-12-27 21:37:21 +00001483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1485 }
1486 if( rec == NULL ||
1487 rec->buf == NULL ||
1488 rec->buf_len < rec->data_offset ||
1489 rec->buf_len - rec->data_offset < rec->data_len )
1490 {
1491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001493 }
1494
Hanno Becker3307b532017-12-27 21:37:21 +00001495 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1496 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001498 data, rec->data_len );
1499
1500 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1501
1502 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1503 {
1504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1505 (unsigned) rec->data_len,
1506 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1508 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001509
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001511 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001513#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 if( mode == MBEDTLS_MODE_STREAM ||
1515 ( mode == MBEDTLS_MODE_CBC
1516#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001517 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001518#endif
1519 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 {
Hanno Becker3307b532017-12-27 21:37:21 +00001521 if( post_avail < transform->maclen )
1522 {
1523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1524 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1525 }
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001528 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001529 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001530 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001531 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1532 data, rec->data_len, rec->ctr, rec->type, mac );
1533 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001534 }
1535 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1538 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001539 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001540 {
Hanno Becker992b6872017-11-09 18:57:39 +00001541 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1542
Hanno Becker3307b532017-12-27 21:37:21 +00001543 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001544
Hanno Becker3307b532017-12-27 21:37:21 +00001545 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1546 sizeof( add_data ) );
1547 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1548 data, rec->data_len );
1549 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1550 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1551
1552 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001553 }
1554 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001555#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001559 }
1560
Hanno Becker3307b532017-12-27 21:37:21 +00001561 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1562 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001563
Hanno Becker3307b532017-12-27 21:37:21 +00001564 rec->data_len += transform->maclen;
1565 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001566 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001567 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001568#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001569
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001570 /*
1571 * Encrypt
1572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1574 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001576 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001577 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001579 "including %d bytes of padding",
1580 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Hanno Becker3307b532017-12-27 21:37:21 +00001582 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1583 transform->iv_enc, transform->ivlen,
1584 data, rec->data_len,
1585 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001588 return( ret );
1589 }
1590
Hanno Becker3307b532017-12-27 21:37:21 +00001591 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 }
Paul Bakker68884e32013-01-07 18:20:04 +01001597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001599#if defined(MBEDTLS_GCM_C) || \
1600 defined(MBEDTLS_CCM_C) || \
1601 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001603 mode == MBEDTLS_MODE_CCM ||
1604 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001605 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001606 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001607 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00001608 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001609
Hanno Becker3307b532017-12-27 21:37:21 +00001610 /* Check that there's space for both the authentication tag
1611 * and the explicit IV before and after the record content. */
1612 if( post_avail < transform->taglen ||
1613 rec->data_offset < explicit_iv_len )
1614 {
1615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1616 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1617 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001618
Paul Bakker68884e32013-01-07 18:20:04 +01001619 /*
1620 * Generate IV
1621 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001622 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1623 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001624 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001625 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00001626 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1627 explicit_iv_len );
1628 /* Prefix record content with explicit IV. */
1629 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001630 }
1631 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1632 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001633 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001634 unsigned char i;
1635
1636 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1637
1638 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001639 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001640 }
1641 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001642 {
1643 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1645 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001646 }
1647
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001648 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1649 iv, transform->ivlen );
1650 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00001651 data - explicit_iv_len, explicit_iv_len );
1652 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1653 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001655 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00001656 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001657
Paul Bakker68884e32013-01-07 18:20:04 +01001658 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001659 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001660 */
Hanno Becker3307b532017-12-27 21:37:21 +00001661
1662 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001663 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00001664 iv, transform->ivlen,
1665 add_data, 13, /* add data */
1666 data, rec->data_len, /* source */
1667 data, &rec->data_len, /* destination */
1668 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001671 return( ret );
1672 }
1673
Hanno Becker3307b532017-12-27 21:37:21 +00001674 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1675 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001676
Hanno Becker3307b532017-12-27 21:37:21 +00001677 rec->data_len += transform->taglen + explicit_iv_len;
1678 rec->data_offset -= explicit_iv_len;
1679 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001680 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1684#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001685 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001688 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001689 size_t padlen, i;
1690 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001691
Hanno Becker3307b532017-12-27 21:37:21 +00001692 /* Currently we're always using minimal padding
1693 * (up to 255 bytes would be allowed). */
1694 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1695 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 padlen = 0;
1697
Hanno Becker3307b532017-12-27 21:37:21 +00001698 /* Check there's enough space in the buffer for the padding. */
1699 if( post_avail < padlen + 1 )
1700 {
1701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1702 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1703 }
1704
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00001706 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Hanno Becker3307b532017-12-27 21:37:21 +00001708 rec->data_len += padlen + 1;
1709 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001712 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001713 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1714 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715 */
Hanno Becker3307b532017-12-27 21:37:21 +00001716 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717 {
Hanno Becker3307b532017-12-27 21:37:21 +00001718 if( f_rng == NULL )
1719 {
1720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
1721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1722 }
1723
1724 if( rec->data_offset < transform->ivlen )
1725 {
1726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1727 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1728 }
1729
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001730 /*
1731 * Generate IV
1732 */
Hanno Becker3307b532017-12-27 21:37:21 +00001733 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001734 if( ret != 0 )
1735 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001736
Hanno Becker3307b532017-12-27 21:37:21 +00001737 memcpy( data - transform->ivlen, transform->iv_enc,
1738 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001739
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001744 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00001745 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001746 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Hanno Becker3307b532017-12-27 21:37:21 +00001748 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1749 transform->iv_enc,
1750 transform->ivlen,
1751 data, rec->data_len,
1752 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001755 return( ret );
1756 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001757
Hanno Becker3307b532017-12-27 21:37:21 +00001758 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001762 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00001765 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001766 {
1767 /*
1768 * Save IV in SSL3 and TLS1
1769 */
Hanno Becker3307b532017-12-27 21:37:21 +00001770 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
1771 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 }
Hanno Becker3307b532017-12-27 21:37:21 +00001773 else
Paul Bakkercca5b812013-08-31 17:40:26 +02001774#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001775 {
1776 data -= transform->ivlen;
1777 rec->data_offset -= transform->ivlen;
1778 rec->data_len += transform->ivlen;
1779 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001782 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001783 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001784 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1785
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001786 /*
1787 * MAC(MAC_write_key, seq_num +
1788 * TLSCipherText.type +
1789 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001790 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001791 * IV + // except for TLS 1.0
1792 * ENC(content + padding + padding_length));
1793 */
Hanno Becker3307b532017-12-27 21:37:21 +00001794
1795 if( post_avail < transform->maclen)
1796 {
1797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1798 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1799 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00001802 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1803 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001804
Hanno Becker3307b532017-12-27 21:37:21 +00001805 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001806
Hanno Becker3307b532017-12-27 21:37:21 +00001807 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1808 sizeof( add_data ) );
1809 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1810 data, rec->data_len );
1811 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1812 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001813
Hanno Becker3307b532017-12-27 21:37:21 +00001814 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001815
Hanno Becker3307b532017-12-27 21:37:21 +00001816 rec->data_len += transform->maclen;
1817 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001818 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001824 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1827 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001830 /* Make extra sure authentication was performed, exactly once */
1831 if( auth_done != 1 )
1832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001835 }
1836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
1839 return( 0 );
1840}
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001845 int auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00001846#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001847 size_t padlen = 0, correct = 1;
1848#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001852 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001856 }
1857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001859
Paul Bakker48916f92012-09-16 19:57:18 +00001860 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001863 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 }
1866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1868 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001869 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001870 int ret;
1871 size_t olen = 0;
1872
Paul Bakker68884e32013-01-07 18:20:04 +01001873 padlen = 0;
1874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001876 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001877 ssl->transform_in->ivlen,
1878 ssl->in_msg, ssl->in_msglen,
1879 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001882 return( ret );
1883 }
1884
1885 if( ssl->in_msglen != olen )
1886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001889 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 }
Paul Bakker68884e32013-01-07 18:20:04 +01001891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001893#if defined(MBEDTLS_GCM_C) || \
1894 defined(MBEDTLS_CCM_C) || \
1895 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001897 mode == MBEDTLS_MODE_CCM ||
1898 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001899 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001900 int ret;
1901 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001902 unsigned char *dec_msg;
1903 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001904 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001905 unsigned char iv[12];
1906 mbedtls_ssl_transform *transform = ssl->transform_in;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001907 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001908
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001909 /*
1910 * Compute and update sizes
1911 */
Hanno Becker8759e162017-12-27 21:34:08 +00001912 if( ssl->in_msglen < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001915 "+ taglen (%d)", ssl->in_msglen,
Hanno Becker8759e162017-12-27 21:34:08 +00001916 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001918 }
Hanno Becker8759e162017-12-27 21:34:08 +00001919 dec_msglen = ssl->in_msglen - explicit_iv_len - transform->taglen;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001920
Paul Bakker68884e32013-01-07 18:20:04 +01001921 dec_msg = ssl->in_msg;
1922 dec_msg_result = ssl->in_msg;
1923 ssl->in_msglen = dec_msglen;
1924
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001925 /*
1926 * Prepare additional authenticated data
1927 */
Paul Bakker68884e32013-01-07 18:20:04 +01001928 memcpy( add_data, ssl->in_ctr, 8 );
1929 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001931 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001932 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1933 add_data[12] = ssl->in_msglen & 0xFF;
1934
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001935 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001936
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001937 /*
1938 * Prepare IV
1939 */
1940 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1941 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001942 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001943 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1944 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001945
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001946 }
1947 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1948 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001949 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001950 unsigned char i;
1951
1952 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1953
1954 for( i = 0; i < 8; i++ )
1955 iv[i+4] ^= ssl->in_ctr[i];
1956 }
1957 else
1958 {
1959 /* Reminder if we ever add an AEAD mode with a different size */
1960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1961 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1962 }
1963
1964 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker8759e162017-12-27 21:34:08 +00001965 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen,
1966 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001967
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001968 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001969 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001972 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001973 add_data, 13,
1974 dec_msg, dec_msglen,
1975 dec_msg_result, &olen,
Hanno Becker8759e162017-12-27 21:34:08 +00001976 dec_msg + dec_msglen,
1977 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1982 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001983
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001984 return( ret );
1985 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001986 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001987
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001988 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001992 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001993 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1996#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001997 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 {
Paul Bakker45829992013-01-03 14:52:21 +01002000 /*
2001 * Decrypt and check the padding
2002 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002003 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002004 unsigned char *dec_msg;
2005 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002006 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002007 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002008 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002009
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 /*
Paul Bakker45829992013-01-03 14:52:21 +01002011 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2014 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002015 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002016#endif
Paul Bakker45829992013-01-03 14:52:21 +01002017
2018 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2019 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002022 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2023 ssl->transform_in->ivlen,
2024 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002026 }
2027
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002028 dec_msglen = ssl->in_msglen;
2029 dec_msg = ssl->in_msg;
2030 dec_msg_result = ssl->in_msg;
2031
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002032 /*
2033 * Authenticate before decrypt if enabled
2034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2036 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002037 {
Hanno Becker992b6872017-11-09 18:57:39 +00002038 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002039 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002042
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002043 dec_msglen -= ssl->transform_in->maclen;
2044 ssl->in_msglen -= ssl->transform_in->maclen;
2045
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002046 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2047 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2048 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2049 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2054 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002055 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002056 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002060 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002061 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002062 ssl->transform_in->maclen );
2063
Hanno Becker992b6872017-11-09 18:57:39 +00002064 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2065 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002070 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002071 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002072 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002074
2075 /*
2076 * Check length sanity
2077 */
2078 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002081 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002083 }
2084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002086 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002087 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002090 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002091 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002092 dec_msglen -= ssl->transform_in->ivlen;
2093 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002094
Paul Bakker48916f92012-09-16 19:57:18 +00002095 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002096 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002097 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002101 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002102 ssl->transform_in->ivlen,
2103 dec_msg, dec_msglen,
2104 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002107 return( ret );
2108 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002109
Paul Bakkercca5b812013-08-31 17:40:26 +02002110 if( dec_msglen != olen )
2111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2113 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002114 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2117 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002118 {
2119 /*
2120 * Save IV in SSL3 and TLS1
2121 */
2122 memcpy( ssl->transform_in->iv_dec,
2123 ssl->transform_in->cipher_ctx_dec.iv,
2124 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002127
2128 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002129
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002130 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002131 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_SSL_DEBUG_ALL)
2134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002135 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002136#endif
Paul Bakker45829992013-01-03 14:52:21 +01002137 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002138 correct = 0;
2139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141#if defined(MBEDTLS_SSL_PROTO_SSL3)
2142 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 {
Paul Bakker48916f92012-09-16 19:57:18 +00002144 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#if defined(MBEDTLS_SSL_DEBUG_ALL)
2147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002149 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002150#endif
Paul Bakker45829992013-01-03 14:52:21 +01002151 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153 }
2154 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2156#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2157 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2158 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 {
2160 /*
Paul Bakker45829992013-01-03 14:52:21 +01002161 * TLSv1+: always check the padding up to the first failure
2162 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002164 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002165 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002166 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002167
Paul Bakker956c9e02013-12-19 14:42:28 +01002168 /*
2169 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002170 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002171 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002172 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002173 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002174 *
2175 * In both cases we reset padding_idx to a safe value (0) to
2176 * prevent out-of-buffer reads.
2177 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002178 correct &= ( padlen <= ssl->in_msglen );
2179 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002180 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002181
2182 padding_idx *= correct;
2183
Angus Grattonb512bc12018-06-19 15:57:50 +10002184 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002185 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002186 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002187 pad_count += real_count *
2188 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2189 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002190
2191 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002194 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002196#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002197 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002199 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2201 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002205 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002206
2207 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002209 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002211 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002217#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002218 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
2222 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223 * Authenticate if not done yet.
2224 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002226#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002227 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002228 {
Hanno Becker992b6872017-11-09 18:57:39 +00002229 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002230
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002231 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002232
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002233 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2234 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#if defined(MBEDTLS_SSL_PROTO_SSL3)
2237 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002238 {
2239 ssl_mac( &ssl->transform_in->md_ctx_dec,
2240 ssl->transform_in->mac_dec,
2241 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002242 ssl->in_ctr, ssl->in_msgtype,
2243 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002244 }
2245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2247#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2248 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2249 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002250 {
2251 /*
2252 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002253 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002254 *
2255 * Known timing attacks:
2256 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2257 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002258 * To compensate for different timings for the MAC calculation
2259 * depending on how much padding was removed (which is determined
2260 * by padlen), process extra_run more blocks through the hash
2261 * function.
2262 *
2263 * The formula in the paper is
2264 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2265 * where L1 is the size of the header plus the decrypted message
2266 * plus CBC padding and L2 is the size of the header plus the
2267 * decrypted message. This is for an underlying hash function
2268 * with 64-byte blocks.
2269 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2270 * correctly. We round down instead of up, so -56 is the correct
2271 * value for our calculations instead of -55.
2272 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002273 * Repeat the formula rather than defining a block_size variable.
2274 * This avoids requiring division by a variable at runtime
2275 * (which would be marginally less efficient and would require
2276 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002277 */
2278 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002279
2280 /*
2281 * The next two sizes are the minimum and maximum values of
2282 * in_msglen over all padlen values.
2283 *
2284 * They're independent of padlen, since we previously did
2285 * in_msglen -= padlen.
2286 *
2287 * Note that max_len + maclen is never more than the buffer
2288 * length, as we previously did in_msglen -= maclen too.
2289 */
2290 const size_t max_len = ssl->in_msglen + padlen;
2291 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2292
Hanno Becker8759e162017-12-27 21:34:08 +00002293 switch( ssl->handshake->ciphersuite_info->mac )
Gilles Peskine20b44082018-05-29 14:06:49 +02002294 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002295#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2296 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002297 case MBEDTLS_MD_MD5:
2298 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002299 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002300 /* 8 bytes of message size, 64-byte compression blocks */
2301 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2302 ( 13 + ssl->in_msglen + 8 ) / 64;
2303 break;
2304#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002305#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002306 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002307 /* 16 bytes of message size, 128-byte compression blocks */
2308 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2309 ( 13 + ssl->in_msglen + 16 ) / 128;
2310 break;
2311#endif
2312 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2315 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002316
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002317 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2320 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2321 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2322 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002323 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002324 /* Make sure we access everything even when padlen > 0. This
2325 * makes the synchronisation requirements for just-in-time
2326 * Prime+Probe attacks much tighter and hopefully impractical. */
2327 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002328 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002329
2330 /* Call mbedtls_md_process at least once due to cache attacks
2331 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002332 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002336
2337 /* Make sure we access all the memory that could contain the MAC,
2338 * before we check it in the next code block. This makes the
2339 * synchronisation requirements for just-in-time Prime+Probe
2340 * attacks much tighter and hopefully impractical. */
2341 ssl_read_memory( ssl->in_msg + min_len,
2342 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002343 }
2344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2346 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002350 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002351
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002352#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002353 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2354 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2355 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
Hanno Becker992b6872017-11-09 18:57:39 +00002358 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2359 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#if defined(MBEDTLS_SSL_DEBUG_ALL)
2362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002363#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002364 correct = 0;
2365 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002366 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002367 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002368
2369 /*
2370 * Finally check the correct flag
2371 */
2372 if( correct == 0 )
2373 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002374#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002375
2376 /* Make extra sure authentication was performed, exactly once */
2377 if( auth_done != 1 )
2378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
2383 if( ssl->in_msglen == 0 )
2384 {
Angus Gratton34817922018-06-19 15:58:22 +10002385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2386 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2387 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2388 {
2389 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2391 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2392 }
2393#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2394
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 ssl->nb_zero++;
2396
2397 /*
2398 * Three or more empty messages may be a DoS attack
2399 * (excessive CPU consumption).
2400 */
2401 if( ssl->nb_zero > 3 )
2402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 }
2407 }
2408 else
2409 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002412 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002413 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002414 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002415 }
2416 else
2417#endif
2418 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002419 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002420 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2421 if( ++ssl->in_ctr[i - 1] != 0 )
2422 break;
2423
2424 /* The loop goes to its end iff the counter is wrapping */
2425 if( i == ssl_ep_len( ssl ) )
2426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2428 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002429 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002430 }
2431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
2434 return( 0 );
2435}
2436
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002437#undef MAC_NONE
2438#undef MAC_PLAINTEXT
2439#undef MAC_CIPHERTEXT
2440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002442/*
2443 * Compression/decompression functions
2444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446{
2447 int ret;
2448 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002449 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002450 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002451 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002454
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002455 if( len_pre == 0 )
2456 return( 0 );
2457
Paul Bakker2770fbd2012-07-03 13:30:23 +00002458 memcpy( msg_pre, ssl->out_msg, len_pre );
2459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002461 ssl->out_msglen ) );
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002464 ssl->out_msg, ssl->out_msglen );
2465
Paul Bakker48916f92012-09-16 19:57:18 +00002466 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2467 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2468 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002469 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002470
Paul Bakker48916f92012-09-16 19:57:18 +00002471 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002472 if( ret != Z_OK )
2473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2475 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002476 }
2477
Angus Grattond8213d02016-05-25 20:56:48 +10002478 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002479 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002482 ssl->out_msglen ) );
2483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002485 ssl->out_msg, ssl->out_msglen );
2486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002488
2489 return( 0 );
2490}
2491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002493{
2494 int ret;
2495 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002496 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002497 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002498 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002501
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002502 if( len_pre == 0 )
2503 return( 0 );
2504
Paul Bakker2770fbd2012-07-03 13:30:23 +00002505 memcpy( msg_pre, ssl->in_msg, len_pre );
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002508 ssl->in_msglen ) );
2509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002511 ssl->in_msg, ssl->in_msglen );
2512
Paul Bakker48916f92012-09-16 19:57:18 +00002513 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2514 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2515 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002516 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002517 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002518
Paul Bakker48916f92012-09-16 19:57:18 +00002519 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002520 if( ret != Z_OK )
2521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2523 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002524 }
2525
Angus Grattond8213d02016-05-25 20:56:48 +10002526 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002527 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002530 ssl->in_msglen ) );
2531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002533 ssl->in_msg, ssl->in_msglen );
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002536
2537 return( 0 );
2538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2542static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#if defined(MBEDTLS_SSL_PROTO_DTLS)
2545static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002546{
2547 /* If renegotiation is not enforced, retransmit until we would reach max
2548 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002549 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002550 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002551 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002552 unsigned char doublings = 1;
2553
2554 while( ratio != 0 )
2555 {
2556 ++doublings;
2557 ratio >>= 1;
2558 }
2559
2560 if( ++ssl->renego_records_seen > doublings )
2561 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002563 return( 0 );
2564 }
2565 }
2566
2567 return( ssl_write_hello_request( ssl ) );
2568}
2569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002571
Paul Bakker5121ce52009-01-03 21:22:43 +00002572/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002573 * Fill the input message buffer by appending data to it.
2574 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002575 *
2576 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2577 * available (from this read and/or a previous one). Otherwise, an error code
2578 * is returned (possibly EOF or WANT_READ).
2579 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002580 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2581 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2582 * since we always read a whole datagram at once.
2583 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002584 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002585 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588{
Paul Bakker23986e52011-04-24 08:57:21 +00002589 int ret;
2590 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002594 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002597 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002599 }
2600
Angus Grattond8213d02016-05-25 20:56:48 +10002601 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002605 }
2606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002610 uint32_t timeout;
2611
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002612 /* Just to be sure */
2613 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2614 {
2615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2616 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2618 }
2619
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002620 /*
2621 * The point is, we need to always read a full datagram at once, so we
2622 * sometimes read more then requested, and handle the additional data.
2623 * It could be the rest of the current record (while fetching the
2624 * header) and/or some other records in the same datagram.
2625 */
2626
2627 /*
2628 * Move to the next record in the already read datagram if applicable
2629 */
2630 if( ssl->next_record_offset != 0 )
2631 {
2632 if( ssl->in_left < ssl->next_record_offset )
2633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002636 }
2637
2638 ssl->in_left -= ssl->next_record_offset;
2639
2640 if( ssl->in_left != 0 )
2641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002643 ssl->next_record_offset ) );
2644 memmove( ssl->in_hdr,
2645 ssl->in_hdr + ssl->next_record_offset,
2646 ssl->in_left );
2647 }
2648
2649 ssl->next_record_offset = 0;
2650 }
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002654
2655 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002656 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002657 */
2658 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002661 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002662 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002663
2664 /*
2665 * A record can't be split accross datagrams. If we need to read but
2666 * are not at the beginning of a new record, the caller did something
2667 * wrong.
2668 */
2669 if( ssl->in_left != 0 )
2670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2672 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002673 }
2674
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002675 /*
2676 * Don't even try to read if time's out already.
2677 * This avoids by-passing the timer when repeatedly receiving messages
2678 * that will end up being dropped.
2679 */
2680 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002681 {
2682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002683 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002684 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002685 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002686 {
Angus Grattond8213d02016-05-25 20:56:48 +10002687 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002690 timeout = ssl->handshake->retransmit_timeout;
2691 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002692 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002695
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002696 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002697 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2698 timeout );
2699 else
2700 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002703
2704 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002706 }
2707
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002708 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002711 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002714 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002715 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002718 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002719 }
2720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002724 return( ret );
2725 }
2726
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002727 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002730 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002732 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002733 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002736 return( ret );
2737 }
2738
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002739 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002742 }
2743
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 if( ret < 0 )
2745 return( ret );
2746
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002747 ssl->in_left = ret;
2748 }
2749 else
2750#endif
2751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002753 ssl->in_left, nb_want ) );
2754
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002755 while( ssl->in_left < nb_want )
2756 {
2757 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002758
2759 if( ssl_check_timer( ssl ) != 0 )
2760 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2761 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002762 {
2763 if( ssl->f_recv_timeout != NULL )
2764 {
2765 ret = ssl->f_recv_timeout( ssl->p_bio,
2766 ssl->in_hdr + ssl->in_left, len,
2767 ssl->conf->read_timeout );
2768 }
2769 else
2770 {
2771 ret = ssl->f_recv( ssl->p_bio,
2772 ssl->in_hdr + ssl->in_left, len );
2773 }
2774 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002777 ssl->in_left, nb_want ) );
2778 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002779
2780 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002782
2783 if( ret < 0 )
2784 return( ret );
2785
mohammad160352aecb92018-03-28 23:41:40 -07002786 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002787 {
Darryl Green11999bb2018-03-13 15:22:58 +00002788 MBEDTLS_SSL_DEBUG_MSG( 1,
2789 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002790 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2792 }
2793
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002794 ssl->in_left += ret;
2795 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 }
2797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
2800 return( 0 );
2801}
2802
2803/*
2804 * Flush any data not yet written
2805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002807{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002808 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002809 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002813 if( ssl->f_send == NULL )
2814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002816 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002818 }
2819
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002820 /* Avoid incrementing counter if data is flushed */
2821 if( ssl->out_left == 0 )
2822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002824 return( 0 );
2825 }
2826
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 while( ssl->out_left > 0 )
2828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2830 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Hanno Becker2b1e3542018-08-06 11:19:13 +01002832 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002833 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
2837 if( ret <= 0 )
2838 return( ret );
2839
mohammad160352aecb92018-03-28 23:41:40 -07002840 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002841 {
Darryl Green11999bb2018-03-13 15:22:58 +00002842 MBEDTLS_SSL_DEBUG_MSG( 1,
2843 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002844 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002845 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2846 }
2847
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 ssl->out_left -= ret;
2849 }
2850
Hanno Becker2b1e3542018-08-06 11:19:13 +01002851#if defined(MBEDTLS_SSL_PROTO_DTLS)
2852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002853 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002854 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002855 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002856 else
2857#endif
2858 {
2859 ssl->out_hdr = ssl->out_buf + 8;
2860 }
2861 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
2865 return( 0 );
2866}
2867
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002868/*
2869 * Functions to handle the DTLS retransmission state machine
2870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002872/*
2873 * Append current handshake message to current outgoing flight
2874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2879 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2880 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002881
2882 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002883 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002884 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002887 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002888 }
2889
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002890 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002891 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002894 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002895 }
2896
2897 /* Copy current handshake message with headers */
2898 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2899 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002900 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002901 msg->next = NULL;
2902
2903 /* Append to the current flight */
2904 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002905 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002906 else
2907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002909 while( cur->next != NULL )
2910 cur = cur->next;
2911 cur->next = msg;
2912 }
2913
Hanno Becker3b235902018-08-06 09:54:53 +01002914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002915 return( 0 );
2916}
2917
2918/*
2919 * Free the current flight of handshake messages
2920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 mbedtls_ssl_flight_item *cur = flight;
2924 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002925
2926 while( cur != NULL )
2927 {
2928 next = cur->next;
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 mbedtls_free( cur->p );
2931 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002932
2933 cur = next;
2934 }
2935}
2936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2938static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002939#endif
2940
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002941/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002942 * Swap transform_out and out_ctr with the alternative ones
2943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002947 unsigned char tmp_out_ctr[8];
2948
2949 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002952 return;
2953 }
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002956
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002957 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002958 tmp_transform = ssl->transform_out;
2959 ssl->transform_out = ssl->handshake->alt_transform_out;
2960 ssl->handshake->alt_transform_out = tmp_transform;
2961
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002962 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002963 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2964 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002965 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002966
2967 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002968 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2971 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2976 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002977 }
2978 }
2979#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002980}
2981
2982/*
2983 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002984 */
2985int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2986{
2987 int ret = 0;
2988
2989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2990
2991 ret = mbedtls_ssl_flight_transmit( ssl );
2992
2993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2994
2995 return( ret );
2996}
2997
2998/*
2999 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003000 *
3001 * Need to remember the current message in case flush_output returns
3002 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003003 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003004 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003005int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003006{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003007 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003011 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003013
3014 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003015 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003016 ssl_swap_epochs( ssl );
3017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003019 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003020
3021 while( ssl->handshake->cur_msg != NULL )
3022 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003023 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003024 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003025
Hanno Beckere1dcb032018-08-17 16:47:58 +01003026 int const is_finished =
3027 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3028 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3029
Hanno Becker04da1892018-08-14 13:22:10 +01003030 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3031 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3032
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003033 /* Swap epochs before sending Finished: we can't do it after
3034 * sending ChangeCipherSpec, in case write returns WANT_READ.
3035 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003036 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003037 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003039 ssl_swap_epochs( ssl );
3040 }
3041
Hanno Becker67bc7c32018-08-06 11:33:50 +01003042 ret = ssl_get_remaining_payload_in_datagram( ssl );
3043 if( ret < 0 )
3044 return( ret );
3045 max_frag_len = (size_t) ret;
3046
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003047 /* CCS is copied as is, while HS messages may need fragmentation */
3048 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3049 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003050 if( max_frag_len == 0 )
3051 {
3052 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3053 return( ret );
3054
3055 continue;
3056 }
3057
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003058 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003059 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003060 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003061
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003062 /* Update position inside current message */
3063 ssl->handshake->cur_msg_p += cur->len;
3064 }
3065 else
3066 {
3067 const unsigned char * const p = ssl->handshake->cur_msg_p;
3068 const size_t hs_len = cur->len - 12;
3069 const size_t frag_off = p - ( cur->p + 12 );
3070 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003071 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003072
Hanno Beckere1dcb032018-08-17 16:47:58 +01003073 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003074 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003075 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003076 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003077
Hanno Becker67bc7c32018-08-06 11:33:50 +01003078 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3079 return( ret );
3080
3081 continue;
3082 }
3083 max_hs_frag_len = max_frag_len - 12;
3084
3085 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3086 max_hs_frag_len : rem_len;
3087
3088 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003089 {
3090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003091 (unsigned) cur_hs_frag_len,
3092 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003093 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003094
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003095 /* Messages are stored with handshake headers as if not fragmented,
3096 * copy beginning of headers then fill fragmentation fields.
3097 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3098 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003099
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003100 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3101 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3102 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3103
Hanno Becker67bc7c32018-08-06 11:33:50 +01003104 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3105 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3106 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003107
3108 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3109
Hanno Becker3f7b9732018-08-28 09:53:25 +01003110 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003111 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3112 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003113 ssl->out_msgtype = cur->type;
3114
3115 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003116 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003117 }
3118
3119 /* If done with the current message move to the next one if any */
3120 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3121 {
3122 if( cur->next != NULL )
3123 {
3124 ssl->handshake->cur_msg = cur->next;
3125 ssl->handshake->cur_msg_p = cur->next->p + 12;
3126 }
3127 else
3128 {
3129 ssl->handshake->cur_msg = NULL;
3130 ssl->handshake->cur_msg_p = NULL;
3131 }
3132 }
3133
3134 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003135 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003138 return( ret );
3139 }
3140 }
3141
Hanno Becker67bc7c32018-08-06 11:33:50 +01003142 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3143 return( ret );
3144
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003145 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3147 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003148 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003151 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3152 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003153
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155
3156 return( 0 );
3157}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003158
3159/*
3160 * To be called when the last message of an incoming flight is received.
3161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003163{
3164 /* We won't need to resend that one any more */
3165 ssl_flight_free( ssl->handshake->flight );
3166 ssl->handshake->flight = NULL;
3167 ssl->handshake->cur_msg = NULL;
3168
3169 /* The next incoming flight will start with this msg_seq */
3170 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3171
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003172 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003173 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003174
Hanno Becker0271f962018-08-16 13:23:47 +01003175 /* Clear future message buffering structure. */
3176 ssl_buffering_free( ssl );
3177
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003178 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003179 ssl_set_timer( ssl, 0 );
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3182 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003185 }
3186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003188}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003189
3190/*
3191 * To be called when the last message of an outgoing flight is send.
3192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003194{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003195 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003196 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3199 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003202 }
3203 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003207
Paul Bakker5121ce52009-01-03 21:22:43 +00003208/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003209 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003211
3212/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003213 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003214 *
3215 * - fill in handshake headers
3216 * - update handshake checksum
3217 * - DTLS: save message for resending
3218 * - then pass to the record layer
3219 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003220 * DTLS: except for HelloRequest, messages are only queued, and will only be
3221 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003222 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003223 * Inputs:
3224 * - ssl->out_msglen: 4 + actual handshake message len
3225 * (4 is the size of handshake headers for TLS)
3226 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3227 * - ssl->out_msg + 4: the handshake message body
3228 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003229 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003230 * - ssl->out_msglen: the length of the record contents
3231 * (including handshake headers but excluding record headers)
3232 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003233 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003234int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003235{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003236 int ret;
3237 const size_t hs_len = ssl->out_msglen - 4;
3238 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3241
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003242 /*
3243 * Sanity checks
3244 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003245 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003246 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3247 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003248 /* In SSLv3, the client might send a NoCertificate alert. */
3249#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3250 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3251 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3252 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3253#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3254 {
3255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3257 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003260 /* Whenever we send anything different from a
3261 * HelloRequest we should be in a handshake - double check. */
3262 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3263 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003264 ssl->handshake == NULL )
3265 {
3266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003271 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003272 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003274 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003277 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003278#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003279
Hanno Beckerb50a2532018-08-06 11:52:54 +01003280 /* Double-check that we did not exceed the bounds
3281 * of the outgoing record buffer.
3282 * This should never fail as the various message
3283 * writing functions must obey the bounds of the
3284 * outgoing record buffer, but better be safe.
3285 *
3286 * Note: We deliberately do not check for the MTU or MFL here.
3287 */
3288 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3289 {
3290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3291 "size %u, maximum %u",
3292 (unsigned) ssl->out_msglen,
3293 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3295 }
3296
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003297 /*
3298 * Fill handshake headers
3299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003302 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3303 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3304 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003305
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003306 /*
3307 * DTLS has additional fields in the Handshake layer,
3308 * between the length field and the actual payload:
3309 * uint16 message_seq;
3310 * uint24 fragment_offset;
3311 * uint24 fragment_length;
3312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003314 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003315 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003316 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003317 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003318 {
3319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3320 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003321 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003322 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3324 }
3325
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003326 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003327 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003328
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003329 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003330 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003331 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003332 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3333 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3334 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003335 }
3336 else
3337 {
3338 ssl->out_msg[4] = 0;
3339 ssl->out_msg[5] = 0;
3340 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003341
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003342 /* Handshake hashes are computed without fragmentation,
3343 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003344 memset( ssl->out_msg + 6, 0x00, 3 );
3345 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003348
Hanno Becker0207e532018-08-28 10:28:28 +01003349 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003350 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3351 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
3353
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003354 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003357 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3358 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359 {
3360 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003363 return( ret );
3364 }
3365 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003366 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003367#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003368 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003369 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003370 {
3371 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3372 return( ret );
3373 }
3374 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003375
3376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3377
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003378 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003379}
3380
3381/*
3382 * Record layer functions
3383 */
3384
3385/*
3386 * Write current record.
3387 *
3388 * Uses:
3389 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3390 * - ssl->out_msglen: length of the record content (excl headers)
3391 * - ssl->out_msg: record content
3392 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003393int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003394{
3395 int ret, done = 0;
3396 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003397 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003398
3399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003402 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003404 {
3405 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003408 return( ret );
3409 }
3410
3411 len = ssl->out_msglen;
3412 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3416 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 ret = mbedtls_ssl_hw_record_write( ssl );
3421 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3424 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003425 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003426
3427 if( ret == 0 )
3428 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003431 if( !done )
3432 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003433 unsigned i;
3434 size_t protected_record_size;
3435
Paul Bakker05ef8352012-05-08 09:17:57 +00003436 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003438 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003439
Hanno Becker19859472018-08-06 09:40:20 +01003440 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003441 ssl->out_len[0] = (unsigned char)( len >> 8 );
3442 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003443
Paul Bakker48916f92012-09-16 19:57:18 +00003444 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003445 {
Hanno Becker3307b532017-12-27 21:37:21 +00003446 mbedtls_record rec;
3447
3448 rec.buf = ssl->out_iv;
3449 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3450 ( ssl->out_iv - ssl->out_buf );
3451 rec.data_len = ssl->out_msglen;
3452 rec.data_offset = ssl->out_msg - rec.buf;
3453
3454 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3455 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3456 ssl->conf->transport, rec.ver );
3457 rec.type = ssl->out_msgtype;
3458
3459 if( ( ret = ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
3460 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003463 return( ret );
3464 }
3465
Hanno Becker3307b532017-12-27 21:37:21 +00003466 if( rec.data_offset != 0 )
3467 {
3468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3470 }
3471
3472 ssl->out_msglen = rec.data_len;
3473 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3474 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003475 }
3476
Hanno Becker2b1e3542018-08-06 11:19:13 +01003477 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3478
3479#if defined(MBEDTLS_SSL_PROTO_DTLS)
3480 /* In case of DTLS, double-check that we don't exceed
3481 * the remaining space in the datagram. */
3482 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3483 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003484 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003485 if( ret < 0 )
3486 return( ret );
3487
3488 if( protected_record_size > (size_t) ret )
3489 {
3490 /* Should never happen */
3491 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3492 }
3493 }
3494#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003497 "version = [%d:%d], msglen = %d",
3498 ssl->out_hdr[0], ssl->out_hdr[1],
3499 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003502 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003503
3504 ssl->out_left += protected_record_size;
3505 ssl->out_hdr += protected_record_size;
3506 ssl_update_out_pointers( ssl, ssl->transform_out );
3507
Hanno Becker04484622018-08-06 09:49:38 +01003508 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3509 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3510 break;
3511
3512 /* The loop goes to its end iff the counter is wrapping */
3513 if( i == ssl_ep_len( ssl ) )
3514 {
3515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3516 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 }
3519
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003521 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3522 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003523 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003524 size_t remaining;
3525 ret = ssl_get_remaining_payload_in_datagram( ssl );
3526 if( ret < 0 )
3527 {
3528 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3529 ret );
3530 return( ret );
3531 }
3532
3533 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003534 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003535 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003536 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003537 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003538 else
3539 {
Hanno Becker513815a2018-08-20 11:56:09 +01003540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003541 }
3542 }
3543#endif /* MBEDTLS_SSL_PROTO_DTLS */
3544
3545 if( ( flush == SSL_FORCE_FLUSH ) &&
3546 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 return( ret );
3550 }
3551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
3554 return( 0 );
3555}
3556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003558
3559static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3560{
3561 if( ssl->in_msglen < ssl->in_hslen ||
3562 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3563 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3564 {
3565 return( 1 );
3566 }
3567 return( 0 );
3568}
Hanno Becker44650b72018-08-16 12:51:11 +01003569
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003570static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003571{
3572 return( ( ssl->in_msg[9] << 16 ) |
3573 ( ssl->in_msg[10] << 8 ) |
3574 ssl->in_msg[11] );
3575}
3576
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003577static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003578{
3579 return( ( ssl->in_msg[6] << 16 ) |
3580 ( ssl->in_msg[7] << 8 ) |
3581 ssl->in_msg[8] );
3582}
3583
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003584static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003585{
3586 uint32_t msg_len, frag_off, frag_len;
3587
3588 msg_len = ssl_get_hs_total_len( ssl );
3589 frag_off = ssl_get_hs_frag_off( ssl );
3590 frag_len = ssl_get_hs_frag_len( ssl );
3591
3592 if( frag_off > msg_len )
3593 return( -1 );
3594
3595 if( frag_len > msg_len - frag_off )
3596 return( -1 );
3597
3598 if( frag_len + 12 > ssl->in_msglen )
3599 return( -1 );
3600
3601 return( 0 );
3602}
3603
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003604/*
3605 * Mark bits in bitmask (used for DTLS HS reassembly)
3606 */
3607static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3608{
3609 unsigned int start_bits, end_bits;
3610
3611 start_bits = 8 - ( offset % 8 );
3612 if( start_bits != 8 )
3613 {
3614 size_t first_byte_idx = offset / 8;
3615
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003616 /* Special case */
3617 if( len <= start_bits )
3618 {
3619 for( ; len != 0; len-- )
3620 mask[first_byte_idx] |= 1 << ( start_bits - len );
3621
3622 /* Avoid potential issues with offset or len becoming invalid */
3623 return;
3624 }
3625
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003626 offset += start_bits; /* Now offset % 8 == 0 */
3627 len -= start_bits;
3628
3629 for( ; start_bits != 0; start_bits-- )
3630 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3631 }
3632
3633 end_bits = len % 8;
3634 if( end_bits != 0 )
3635 {
3636 size_t last_byte_idx = ( offset + len ) / 8;
3637
3638 len -= end_bits; /* Now len % 8 == 0 */
3639
3640 for( ; end_bits != 0; end_bits-- )
3641 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3642 }
3643
3644 memset( mask + offset / 8, 0xFF, len / 8 );
3645}
3646
3647/*
3648 * Check that bitmask is full
3649 */
3650static int ssl_bitmask_check( unsigned char *mask, size_t len )
3651{
3652 size_t i;
3653
3654 for( i = 0; i < len / 8; i++ )
3655 if( mask[i] != 0xFF )
3656 return( -1 );
3657
3658 for( i = 0; i < len % 8; i++ )
3659 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3660 return( -1 );
3661
3662 return( 0 );
3663}
3664
Hanno Becker56e205e2018-08-16 09:06:12 +01003665/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003666static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003667 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003668{
Hanno Becker56e205e2018-08-16 09:06:12 +01003669 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003670
Hanno Becker56e205e2018-08-16 09:06:12 +01003671 alloc_len = 12; /* Handshake header */
3672 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003673
Hanno Beckerd07df862018-08-16 09:14:58 +01003674 if( add_bitmap )
3675 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003676
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003677 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003678}
Hanno Becker56e205e2018-08-16 09:06:12 +01003679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003681
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003682static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003683{
3684 return( ( ssl->in_msg[1] << 16 ) |
3685 ( ssl->in_msg[2] << 8 ) |
3686 ssl->in_msg[3] );
3687}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003688
Simon Butcher99000142016-10-13 17:21:01 +01003689int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003694 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003696 }
3697
Hanno Becker12555c62018-08-16 12:47:53 +01003698 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003701 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003702 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003706 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003707 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003708 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003709
Hanno Becker44650b72018-08-16 12:51:11 +01003710 if( ssl_check_hs_header( ssl ) != 0 )
3711 {
3712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3713 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3714 }
3715
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003716 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003717 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3718 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3719 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3720 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003721 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003722 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3723 {
3724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3725 recv_msg_seq,
3726 ssl->handshake->in_msg_seq ) );
3727 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3728 }
3729
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003730 /* Retransmit only on last message from previous flight, to avoid
3731 * too many retransmissions.
3732 * Besides, No sane server ever retransmits HelloVerifyRequest */
3733 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003737 "message_seq = %d, start_of_flight = %d",
3738 recv_msg_seq,
3739 ssl->handshake->in_flight_start_seq ) );
3740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003744 return( ret );
3745 }
3746 }
3747 else
3748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003750 "message_seq = %d, expected = %d",
3751 recv_msg_seq,
3752 ssl->handshake->in_msg_seq ) );
3753 }
3754
Hanno Becker90333da2017-10-10 11:27:13 +01003755 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003756 }
3757 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003758
Hanno Becker6d97ef52018-08-16 13:09:04 +01003759 /* Message reassembly is handled alongside buffering of future
3760 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003761 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003762 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003763 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003766 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003767 }
3768 }
3769 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003771 /* With TLS we don't handle fragmentation (for now) */
3772 if( ssl->in_msglen < ssl->in_hslen )
3773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3775 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003776 }
3777
Simon Butcher99000142016-10-13 17:21:01 +01003778 return( 0 );
3779}
3780
3781void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3782{
Hanno Becker0271f962018-08-16 13:23:47 +01003783 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003784
Hanno Becker0271f962018-08-16 13:23:47 +01003785 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003786 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003787 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003788 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003789
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003790 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003793 ssl->handshake != NULL )
3794 {
Hanno Becker0271f962018-08-16 13:23:47 +01003795 unsigned offset;
3796 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003797
Hanno Becker0271f962018-08-16 13:23:47 +01003798 /* Increment handshake sequence number */
3799 hs->in_msg_seq++;
3800
3801 /*
3802 * Clear up handshake buffering and reassembly structure.
3803 */
3804
3805 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003806 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003807
3808 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003809 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3810 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003811 offset++, hs_buf++ )
3812 {
3813 *hs_buf = *(hs_buf + 1);
3814 }
3815
3816 /* Create a fresh last entry */
3817 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003818 }
3819#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003820}
3821
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003822/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003823 * DTLS anti-replay: RFC 6347 4.1.2.6
3824 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003825 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3826 * Bit n is set iff record number in_window_top - n has been seen.
3827 *
3828 * Usually, in_window_top is the last record number seen and the lsb of
3829 * in_window is set. The only exception is the initial state (record number 0
3830 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3833static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003834{
3835 ssl->in_window_top = 0;
3836 ssl->in_window = 0;
3837}
3838
3839static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3840{
3841 return( ( (uint64_t) buf[0] << 40 ) |
3842 ( (uint64_t) buf[1] << 32 ) |
3843 ( (uint64_t) buf[2] << 24 ) |
3844 ( (uint64_t) buf[3] << 16 ) |
3845 ( (uint64_t) buf[4] << 8 ) |
3846 ( (uint64_t) buf[5] ) );
3847}
3848
3849/*
3850 * Return 0 if sequence number is acceptable, -1 otherwise
3851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003853{
3854 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3855 uint64_t bit;
3856
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003857 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003858 return( 0 );
3859
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003860 if( rec_seqnum > ssl->in_window_top )
3861 return( 0 );
3862
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003863 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003864
3865 if( bit >= 64 )
3866 return( -1 );
3867
3868 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3869 return( -1 );
3870
3871 return( 0 );
3872}
3873
3874/*
3875 * Update replay window on new validated record
3876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003878{
3879 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3880
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003881 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003882 return;
3883
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003884 if( rec_seqnum > ssl->in_window_top )
3885 {
3886 /* Update window_top and the contents of the window */
3887 uint64_t shift = rec_seqnum - ssl->in_window_top;
3888
3889 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003890 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003891 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003892 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003893 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003894 ssl->in_window |= 1;
3895 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003896
3897 ssl->in_window_top = rec_seqnum;
3898 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003899 else
3900 {
3901 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003902 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003903
3904 if( bit < 64 ) /* Always true, but be extra sure */
3905 ssl->in_window |= (uint64_t) 1 << bit;
3906 }
3907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003909
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003910#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003911/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003912static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3913
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003914/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003915 * Without any SSL context, check if a datagram looks like a ClientHello with
3916 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003917 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003918 *
3919 * - if cookie is valid, return 0
3920 * - if ClientHello looks superficially valid but cookie is not,
3921 * fill obuf and set olen, then
3922 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3923 * - otherwise return a specific error code
3924 */
3925static int ssl_check_dtls_clihlo_cookie(
3926 mbedtls_ssl_cookie_write_t *f_cookie_write,
3927 mbedtls_ssl_cookie_check_t *f_cookie_check,
3928 void *p_cookie,
3929 const unsigned char *cli_id, size_t cli_id_len,
3930 const unsigned char *in, size_t in_len,
3931 unsigned char *obuf, size_t buf_len, size_t *olen )
3932{
3933 size_t sid_len, cookie_len;
3934 unsigned char *p;
3935
3936 if( f_cookie_write == NULL || f_cookie_check == NULL )
3937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3938
3939 /*
3940 * Structure of ClientHello with record and handshake headers,
3941 * and expected values. We don't need to check a lot, more checks will be
3942 * done when actually parsing the ClientHello - skipping those checks
3943 * avoids code duplication and does not make cookie forging any easier.
3944 *
3945 * 0-0 ContentType type; copied, must be handshake
3946 * 1-2 ProtocolVersion version; copied
3947 * 3-4 uint16 epoch; copied, must be 0
3948 * 5-10 uint48 sequence_number; copied
3949 * 11-12 uint16 length; (ignored)
3950 *
3951 * 13-13 HandshakeType msg_type; (ignored)
3952 * 14-16 uint24 length; (ignored)
3953 * 17-18 uint16 message_seq; copied
3954 * 19-21 uint24 fragment_offset; copied, must be 0
3955 * 22-24 uint24 fragment_length; (ignored)
3956 *
3957 * 25-26 ProtocolVersion client_version; (ignored)
3958 * 27-58 Random random; (ignored)
3959 * 59-xx SessionID session_id; 1 byte len + sid_len content
3960 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3961 * ...
3962 *
3963 * Minimum length is 61 bytes.
3964 */
3965 if( in_len < 61 ||
3966 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3967 in[3] != 0 || in[4] != 0 ||
3968 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3969 {
3970 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3971 }
3972
3973 sid_len = in[59];
3974 if( sid_len > in_len - 61 )
3975 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3976
3977 cookie_len = in[60 + sid_len];
3978 if( cookie_len > in_len - 60 )
3979 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3980
3981 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3982 cli_id, cli_id_len ) == 0 )
3983 {
3984 /* Valid cookie */
3985 return( 0 );
3986 }
3987
3988 /*
3989 * If we get here, we've got an invalid cookie, let's prepare HVR.
3990 *
3991 * 0-0 ContentType type; copied
3992 * 1-2 ProtocolVersion version; copied
3993 * 3-4 uint16 epoch; copied
3994 * 5-10 uint48 sequence_number; copied
3995 * 11-12 uint16 length; olen - 13
3996 *
3997 * 13-13 HandshakeType msg_type; hello_verify_request
3998 * 14-16 uint24 length; olen - 25
3999 * 17-18 uint16 message_seq; copied
4000 * 19-21 uint24 fragment_offset; copied
4001 * 22-24 uint24 fragment_length; olen - 25
4002 *
4003 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4004 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4005 *
4006 * Minimum length is 28.
4007 */
4008 if( buf_len < 28 )
4009 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4010
4011 /* Copy most fields and adapt others */
4012 memcpy( obuf, in, 25 );
4013 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4014 obuf[25] = 0xfe;
4015 obuf[26] = 0xff;
4016
4017 /* Generate and write actual cookie */
4018 p = obuf + 28;
4019 if( f_cookie_write( p_cookie,
4020 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4021 {
4022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4023 }
4024
4025 *olen = p - obuf;
4026
4027 /* Go back and fill length fields */
4028 obuf[27] = (unsigned char)( *olen - 28 );
4029
4030 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4031 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4032 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4033
4034 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4035 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4036
4037 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4038}
4039
4040/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004041 * Handle possible client reconnect with the same UDP quadruplet
4042 * (RFC 6347 Section 4.2.8).
4043 *
4044 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4045 * that looks like a ClientHello.
4046 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004047 * - if the input looks like a ClientHello without cookies,
4048 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004049 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004050 * - if the input looks like a ClientHello with a valid cookie,
4051 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004052 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004053 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004054 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004055 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004056 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4057 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004058 */
4059static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4060{
4061 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004062 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004063
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004064 ret = ssl_check_dtls_clihlo_cookie(
4065 ssl->conf->f_cookie_write,
4066 ssl->conf->f_cookie_check,
4067 ssl->conf->p_cookie,
4068 ssl->cli_id, ssl->cli_id_len,
4069 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004070 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004071
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004072 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4073
4074 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004075 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004076 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004077 * If the error is permanent we'll catch it later,
4078 * if it's not, then hopefully it'll work next time. */
4079 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4080
4081 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004082 }
4083
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004084 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004085 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004086 /* Got a valid cookie, partially reset context */
4087 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4088 {
4089 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4090 return( ret );
4091 }
4092
4093 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004094 }
4095
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004096 return( ret );
4097}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004098#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004099
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004100/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004101 * ContentType type;
4102 * ProtocolVersion version;
4103 * uint16 epoch; // DTLS only
4104 * uint48 sequence_number; // DTLS only
4105 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004106 *
4107 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004108 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004109 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4110 *
4111 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004112 * 1. proceed with the record if this function returns 0
4113 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4114 * 3. return CLIENT_RECONNECT if this function return that value
4115 * 4. drop the whole datagram if this function returns anything else.
4116 * Point 2 is needed when the peer is resending, and we have already received
4117 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004120{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004121 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 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 +02004124
Paul Bakker5121ce52009-01-03 21:22:43 +00004125 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004126 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004127 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004131 ssl->in_msgtype,
4132 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004133
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004134 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4136 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4137 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4138 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004141
4142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004143 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4144 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004145 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4146#endif /* MBEDTLS_SSL_PROTO_DTLS */
4147 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4148 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004151 }
4152
4153 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004154 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4157 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004158 }
4159
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004160 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4163 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004164 }
4165
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004166 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004167 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004168 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004172 }
4173
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004174 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004175 * DTLS-related tests.
4176 * Check epoch before checking length constraint because
4177 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4178 * message gets duplicated before the corresponding Finished message,
4179 * the second ChangeCipherSpec should be discarded because it belongs
4180 * to an old epoch, but not because its length is shorter than
4181 * the minimum record length for packets using the new record transform.
4182 * Note that these two kinds of failures are handled differently,
4183 * as an unexpected record is silently skipped but an invalid
4184 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004185 */
4186#if defined(MBEDTLS_SSL_PROTO_DTLS)
4187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4188 {
4189 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4190
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004191 /* Check epoch (and sequence number) with DTLS */
4192 if( rec_epoch != ssl->in_epoch )
4193 {
4194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4195 "expected %d, received %d",
4196 ssl->in_epoch, rec_epoch ) );
4197
4198#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4199 /*
4200 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4201 * access the first byte of record content (handshake type), as we
4202 * have an active transform (possibly iv_len != 0), so use the
4203 * fact that the record header len is 13 instead.
4204 */
4205 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4206 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4207 rec_epoch == 0 &&
4208 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4209 ssl->in_left > 13 &&
4210 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4211 {
4212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4213 "from the same port" ) );
4214 return( ssl_handle_possible_reconnect( ssl ) );
4215 }
4216 else
4217#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004218 {
4219 /* Consider buffering the record. */
4220 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4221 {
4222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4223 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4224 }
4225
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004226 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004227 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004228 }
4229
4230#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4231 /* Replay detection only works for the current epoch */
4232 if( rec_epoch == ssl->in_epoch &&
4233 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4234 {
4235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4236 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4237 }
4238#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004239
Hanno Becker52c6dc62017-05-26 16:07:36 +01004240 /* Drop unexpected ApplicationData records,
4241 * except at the beginning of renegotiations */
4242 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4244#if defined(MBEDTLS_SSL_RENEGOTIATION)
4245 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4246 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4247#endif
4248 )
4249 {
4250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4251 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4252 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004253 }
4254#endif /* MBEDTLS_SSL_PROTO_DTLS */
4255
Hanno Becker52c6dc62017-05-26 16:07:36 +01004256
4257 /* Check length against bounds of the current transform and version */
4258 if( ssl->transform_in == NULL )
4259 {
4260 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004261 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004262 {
4263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4264 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4265 }
4266 }
4267 else
4268 {
4269 if( ssl->in_msglen < ssl->transform_in->minlen )
4270 {
4271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4272 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4273 }
4274
4275#if defined(MBEDTLS_SSL_PROTO_SSL3)
4276 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004277 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004278 {
4279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4280 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4281 }
4282#endif
4283#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4284 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4285 /*
4286 * TLS encrypted messages can have up to 256 bytes of padding
4287 */
4288 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4289 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004290 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004291 {
4292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4293 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4294 }
4295#endif
4296 }
4297
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004298 return( 0 );
4299}
Paul Bakker5121ce52009-01-03 21:22:43 +00004300
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004301/*
4302 * If applicable, decrypt (and decompress) record content
4303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004305{
4306 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4309 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4312 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 ret = mbedtls_ssl_hw_record_read( ssl );
4317 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4320 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004321 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004322
4323 if( ret == 0 )
4324 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004327 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 {
4329 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332 return( ret );
4333 }
4334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004336 ssl->in_msg, ssl->in_msglen );
4337
Angus Grattond8213d02016-05-25 20:56:48 +10004338 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4341 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 }
4343 }
4344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004346 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004348 {
4349 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004352 return( ret );
4353 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004361 }
4362#endif
4363
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004364 return( 0 );
4365}
4366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004368
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004369/*
4370 * Read a record.
4371 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004372 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4373 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4374 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004375 */
Hanno Becker1097b342018-08-15 14:09:41 +01004376
4377/* Helper functions for mbedtls_ssl_read_record(). */
4378static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004379static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4380static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004381
Hanno Becker327c93b2018-08-15 13:56:18 +01004382int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004383 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004384{
4385 int ret;
4386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004388
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004389 if( ssl->keep_current_message == 0 )
4390 {
4391 do {
Simon Butcher99000142016-10-13 17:21:01 +01004392
Hanno Becker26994592018-08-15 14:14:59 +01004393 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004394 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004395 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004396
Hanno Beckere74d5562018-08-15 14:26:08 +01004397 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004398 {
Hanno Becker40f50842018-08-15 14:48:01 +01004399#if defined(MBEDTLS_SSL_PROTO_DTLS)
4400 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004401
Hanno Becker40f50842018-08-15 14:48:01 +01004402 /* We only check for buffered messages if the
4403 * current datagram is fully consumed. */
4404 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004405 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004406 {
Hanno Becker40f50842018-08-15 14:48:01 +01004407 if( ssl_load_buffered_message( ssl ) == 0 )
4408 have_buffered = 1;
4409 }
4410
4411 if( have_buffered == 0 )
4412#endif /* MBEDTLS_SSL_PROTO_DTLS */
4413 {
4414 ret = ssl_get_next_record( ssl );
4415 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4416 continue;
4417
4418 if( ret != 0 )
4419 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004420 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004421 return( ret );
4422 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004423 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004424 }
4425
4426 ret = mbedtls_ssl_handle_message_type( ssl );
4427
Hanno Becker40f50842018-08-15 14:48:01 +01004428#if defined(MBEDTLS_SSL_PROTO_DTLS)
4429 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4430 {
4431 /* Buffer future message */
4432 ret = ssl_buffer_message( ssl );
4433 if( ret != 0 )
4434 return( ret );
4435
4436 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4437 }
4438#endif /* MBEDTLS_SSL_PROTO_DTLS */
4439
Hanno Becker90333da2017-10-10 11:27:13 +01004440 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4441 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004442
4443 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004444 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004445 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004446 return( ret );
4447 }
4448
Hanno Becker327c93b2018-08-15 13:56:18 +01004449 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004450 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004451 {
4452 mbedtls_ssl_update_handshake_status( ssl );
4453 }
Simon Butcher99000142016-10-13 17:21:01 +01004454 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004455 else
Simon Butcher99000142016-10-13 17:21:01 +01004456 {
Hanno Becker02f59072018-08-15 14:00:24 +01004457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004458 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004459 }
4460
4461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4462
4463 return( 0 );
4464}
4465
Hanno Becker40f50842018-08-15 14:48:01 +01004466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004467static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004468{
Hanno Becker40f50842018-08-15 14:48:01 +01004469 if( ssl->in_left > ssl->next_record_offset )
4470 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004471
Hanno Becker40f50842018-08-15 14:48:01 +01004472 return( 0 );
4473}
4474
4475static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4476{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004477 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004478 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004479 int ret = 0;
4480
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004481 if( hs == NULL )
4482 return( -1 );
4483
Hanno Beckere00ae372018-08-20 09:39:42 +01004484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4485
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004486 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4487 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4488 {
4489 /* Check if we have seen a ChangeCipherSpec before.
4490 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004491 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004492 {
4493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4494 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004495 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004496 }
4497
Hanno Becker39b8bc92018-08-28 17:17:13 +01004498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004499 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4500 ssl->in_msglen = 1;
4501 ssl->in_msg[0] = 1;
4502
4503 /* As long as they are equal, the exact value doesn't matter. */
4504 ssl->in_left = 0;
4505 ssl->next_record_offset = 0;
4506
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004507 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004508 goto exit;
4509 }
Hanno Becker37f95322018-08-16 13:55:32 +01004510
Hanno Beckerb8f50142018-08-28 10:01:34 +01004511#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004512 /* Debug only */
4513 {
4514 unsigned offset;
4515 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4516 {
4517 hs_buf = &hs->buffering.hs[offset];
4518 if( hs_buf->is_valid == 1 )
4519 {
4520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4521 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004522 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004523 }
4524 }
4525 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004526#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004527
4528 /* Check if we have buffered and/or fully reassembled the
4529 * next handshake message. */
4530 hs_buf = &hs->buffering.hs[0];
4531 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4532 {
4533 /* Synthesize a record containing the buffered HS message. */
4534 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4535 ( hs_buf->data[2] << 8 ) |
4536 hs_buf->data[3];
4537
4538 /* Double-check that we haven't accidentally buffered
4539 * a message that doesn't fit into the input buffer. */
4540 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4541 {
4542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4544 }
4545
4546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4547 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4548 hs_buf->data, msg_len + 12 );
4549
4550 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4551 ssl->in_hslen = msg_len + 12;
4552 ssl->in_msglen = msg_len + 12;
4553 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4554
4555 ret = 0;
4556 goto exit;
4557 }
4558 else
4559 {
4560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4561 hs->in_msg_seq ) );
4562 }
4563
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004564 ret = -1;
4565
4566exit:
4567
4568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4569 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004570}
4571
Hanno Beckera02b0b42018-08-21 17:20:27 +01004572static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4573 size_t desired )
4574{
4575 int offset;
4576 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4578 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004579
Hanno Becker01315ea2018-08-21 17:22:17 +01004580 /* Get rid of future records epoch first, if such exist. */
4581 ssl_free_buffered_record( ssl );
4582
4583 /* Check if we have enough space available now. */
4584 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4585 hs->buffering.total_bytes_buffered ) )
4586 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004588 return( 0 );
4589 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004590
Hanno Becker4f432ad2018-08-28 10:02:32 +01004591 /* We don't have enough space to buffer the next expected handshake
4592 * message. Remove buffers used for future messages to gain space,
4593 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004594 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4595 offset >= 0; offset-- )
4596 {
4597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4598 offset ) );
4599
Hanno Beckerb309b922018-08-23 13:18:05 +01004600 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004601
4602 /* Check if we have enough space available now. */
4603 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4604 hs->buffering.total_bytes_buffered ) )
4605 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004607 return( 0 );
4608 }
4609 }
4610
4611 return( -1 );
4612}
4613
Hanno Becker40f50842018-08-15 14:48:01 +01004614static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4615{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004616 int ret = 0;
4617 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4618
4619 if( hs == NULL )
4620 return( 0 );
4621
4622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4623
4624 switch( ssl->in_msgtype )
4625 {
4626 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004628
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004629 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004630 break;
4631
4632 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004633 {
4634 unsigned recv_msg_seq_offset;
4635 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4636 mbedtls_ssl_hs_buffer *hs_buf;
4637 size_t msg_len = ssl->in_hslen - 12;
4638
4639 /* We should never receive an old handshake
4640 * message - double-check nonetheless. */
4641 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4642 {
4643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4645 }
4646
4647 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4648 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4649 {
4650 /* Silently ignore -- message too far in the future */
4651 MBEDTLS_SSL_DEBUG_MSG( 2,
4652 ( "Ignore future HS message with sequence number %u, "
4653 "buffering window %u - %u",
4654 recv_msg_seq, ssl->handshake->in_msg_seq,
4655 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4656
4657 goto exit;
4658 }
4659
4660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4661 recv_msg_seq, recv_msg_seq_offset ) );
4662
4663 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4664
4665 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004666 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004667 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004668 size_t reassembly_buf_sz;
4669
Hanno Becker37f95322018-08-16 13:55:32 +01004670 hs_buf->is_fragmented =
4671 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4672
4673 /* We copy the message back into the input buffer
4674 * after reassembly, so check that it's not too large.
4675 * This is an implementation-specific limitation
4676 * and not one from the standard, hence it is not
4677 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004678 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004679 {
4680 /* Ignore message */
4681 goto exit;
4682 }
4683
Hanno Beckere0b150f2018-08-21 15:51:03 +01004684 /* Check if we have enough space to buffer the message. */
4685 if( hs->buffering.total_bytes_buffered >
4686 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4687 {
4688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4690 }
4691
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004692 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4693 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004694
4695 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4696 hs->buffering.total_bytes_buffered ) )
4697 {
4698 if( recv_msg_seq_offset > 0 )
4699 {
4700 /* If we can't buffer a future message because
4701 * of space limitations -- ignore. */
4702 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",
4703 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4704 (unsigned) hs->buffering.total_bytes_buffered ) );
4705 goto exit;
4706 }
Hanno Beckere1801392018-08-21 16:51:05 +01004707 else
4708 {
4709 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",
4710 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4711 (unsigned) hs->buffering.total_bytes_buffered ) );
4712 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004713
Hanno Beckera02b0b42018-08-21 17:20:27 +01004714 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004715 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004716 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",
4717 (unsigned) msg_len,
4718 (unsigned) reassembly_buf_sz,
4719 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004720 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004721 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4722 goto exit;
4723 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004724 }
4725
4726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4727 msg_len ) );
4728
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004729 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4730 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004731 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004732 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004733 goto exit;
4734 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004735 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004736
4737 /* Prepare final header: copy msg_type, length and message_seq,
4738 * then add standardised fragment_offset and fragment_length */
4739 memcpy( hs_buf->data, ssl->in_msg, 6 );
4740 memset( hs_buf->data + 6, 0, 3 );
4741 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4742
4743 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004744
4745 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004746 }
4747 else
4748 {
4749 /* Make sure msg_type and length are consistent */
4750 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4751 {
4752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4753 /* Ignore */
4754 goto exit;
4755 }
4756 }
4757
Hanno Becker4422bbb2018-08-20 09:40:19 +01004758 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004759 {
4760 size_t frag_len, frag_off;
4761 unsigned char * const msg = hs_buf->data + 12;
4762
4763 /*
4764 * Check and copy current fragment
4765 */
4766
4767 /* Validation of header fields already done in
4768 * mbedtls_ssl_prepare_handshake_record(). */
4769 frag_off = ssl_get_hs_frag_off( ssl );
4770 frag_len = ssl_get_hs_frag_len( ssl );
4771
4772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4773 frag_off, frag_len ) );
4774 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4775
4776 if( hs_buf->is_fragmented )
4777 {
4778 unsigned char * const bitmask = msg + msg_len;
4779 ssl_bitmask_set( bitmask, frag_off, frag_len );
4780 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4781 msg_len ) == 0 );
4782 }
4783 else
4784 {
4785 hs_buf->is_complete = 1;
4786 }
4787
4788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4789 hs_buf->is_complete ? "" : "not yet " ) );
4790 }
4791
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004792 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004793 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004794
4795 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004796 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004797 break;
4798 }
4799
4800exit:
4801
4802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4803 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004804}
4805#endif /* MBEDTLS_SSL_PROTO_DTLS */
4806
Hanno Becker1097b342018-08-15 14:09:41 +01004807static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004809 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004810 * Consume last content-layer message and potentially
4811 * update in_msglen which keeps track of the contents'
4812 * consumption state.
4813 *
4814 * (1) Handshake messages:
4815 * Remove last handshake message, move content
4816 * and adapt in_msglen.
4817 *
4818 * (2) Alert messages:
4819 * Consume whole record content, in_msglen = 0.
4820 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004821 * (3) Change cipher spec:
4822 * Consume whole record content, in_msglen = 0.
4823 *
4824 * (4) Application data:
4825 * Don't do anything - the record layer provides
4826 * the application data as a stream transport
4827 * and consumes through mbedtls_ssl_read only.
4828 *
4829 */
4830
4831 /* Case (1): Handshake messages */
4832 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004833 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004834 /* Hard assertion to be sure that no application data
4835 * is in flight, as corrupting ssl->in_msglen during
4836 * ssl->in_offt != NULL is fatal. */
4837 if( ssl->in_offt != NULL )
4838 {
4839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4840 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4841 }
4842
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004843 /*
4844 * Get next Handshake message in the current record
4845 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004846
Hanno Becker4a810fb2017-05-24 16:27:30 +01004847 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004848 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004849 * current handshake content: If DTLS handshake
4850 * fragmentation is used, that's the fragment
4851 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004852 * size here is faulty and should be changed at
4853 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004854 * (2) While it doesn't seem to cause problems, one
4855 * has to be very careful not to assume that in_hslen
4856 * is always <= in_msglen in a sensible communication.
4857 * Again, it's wrong for DTLS handshake fragmentation.
4858 * The following check is therefore mandatory, and
4859 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004860 * Additionally, ssl->in_hslen might be arbitrarily out of
4861 * bounds after handling a DTLS message with an unexpected
4862 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004863 */
4864 if( ssl->in_hslen < ssl->in_msglen )
4865 {
4866 ssl->in_msglen -= ssl->in_hslen;
4867 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4868 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004869
Hanno Becker4a810fb2017-05-24 16:27:30 +01004870 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4871 ssl->in_msg, ssl->in_msglen );
4872 }
4873 else
4874 {
4875 ssl->in_msglen = 0;
4876 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004877
Hanno Becker4a810fb2017-05-24 16:27:30 +01004878 ssl->in_hslen = 0;
4879 }
4880 /* Case (4): Application data */
4881 else if( ssl->in_offt != NULL )
4882 {
4883 return( 0 );
4884 }
4885 /* Everything else (CCS & Alerts) */
4886 else
4887 {
4888 ssl->in_msglen = 0;
4889 }
4890
Hanno Becker1097b342018-08-15 14:09:41 +01004891 return( 0 );
4892}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004893
Hanno Beckere74d5562018-08-15 14:26:08 +01004894static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4895{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004896 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004897 return( 1 );
4898
4899 return( 0 );
4900}
4901
Hanno Becker5f066e72018-08-16 14:56:31 +01004902#if defined(MBEDTLS_SSL_PROTO_DTLS)
4903
4904static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4905{
4906 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4907 if( hs == NULL )
4908 return;
4909
Hanno Becker01315ea2018-08-21 17:22:17 +01004910 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004911 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004912 hs->buffering.total_bytes_buffered -=
4913 hs->buffering.future_record.len;
4914
4915 mbedtls_free( hs->buffering.future_record.data );
4916 hs->buffering.future_record.data = NULL;
4917 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004918}
4919
4920static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4921{
4922 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4923 unsigned char * rec;
4924 size_t rec_len;
4925 unsigned rec_epoch;
4926
4927 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4928 return( 0 );
4929
4930 if( hs == NULL )
4931 return( 0 );
4932
Hanno Becker5f066e72018-08-16 14:56:31 +01004933 rec = hs->buffering.future_record.data;
4934 rec_len = hs->buffering.future_record.len;
4935 rec_epoch = hs->buffering.future_record.epoch;
4936
4937 if( rec == NULL )
4938 return( 0 );
4939
Hanno Becker4cb782d2018-08-20 11:19:05 +01004940 /* Only consider loading future records if the
4941 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004942 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004943 return( 0 );
4944
Hanno Becker5f066e72018-08-16 14:56:31 +01004945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4946
4947 if( rec_epoch != ssl->in_epoch )
4948 {
4949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4950 goto exit;
4951 }
4952
4953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4954
4955 /* Double-check that the record is not too large */
4956 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
4957 (size_t)( ssl->in_hdr - ssl->in_buf ) )
4958 {
4959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4961 }
4962
4963 memcpy( ssl->in_hdr, rec, rec_len );
4964 ssl->in_left = rec_len;
4965 ssl->next_record_offset = 0;
4966
4967 ssl_free_buffered_record( ssl );
4968
4969exit:
4970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4971 return( 0 );
4972}
4973
4974static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
4975{
4976 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4977 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01004978 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01004979
4980 /* Don't buffer future records outside handshakes. */
4981 if( hs == NULL )
4982 return( 0 );
4983
4984 /* Only buffer handshake records (we are only interested
4985 * in Finished messages). */
4986 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
4987 return( 0 );
4988
4989 /* Don't buffer more than one future epoch record. */
4990 if( hs->buffering.future_record.data != NULL )
4991 return( 0 );
4992
Hanno Becker01315ea2018-08-21 17:22:17 +01004993 /* Don't buffer record if there's not enough buffering space remaining. */
4994 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4995 hs->buffering.total_bytes_buffered ) )
4996 {
4997 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",
4998 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4999 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005000 return( 0 );
5001 }
5002
Hanno Becker5f066e72018-08-16 14:56:31 +01005003 /* Buffer record */
5004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5005 ssl->in_epoch + 1 ) );
5006 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5007 rec_hdr_len + ssl->in_msglen );
5008
5009 /* ssl_parse_record_header() only considers records
5010 * of the next epoch as candidates for buffering. */
5011 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005012 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005013
5014 hs->buffering.future_record.data =
5015 mbedtls_calloc( 1, hs->buffering.future_record.len );
5016 if( hs->buffering.future_record.data == NULL )
5017 {
5018 /* If we run out of RAM trying to buffer a
5019 * record from the next epoch, just ignore. */
5020 return( 0 );
5021 }
5022
Hanno Becker01315ea2018-08-21 17:22:17 +01005023 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005024
Hanno Becker01315ea2018-08-21 17:22:17 +01005025 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005026 return( 0 );
5027}
5028
5029#endif /* MBEDTLS_SSL_PROTO_DTLS */
5030
Hanno Beckere74d5562018-08-15 14:26:08 +01005031static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005032{
5033 int ret;
5034
Hanno Becker5f066e72018-08-16 14:56:31 +01005035#if defined(MBEDTLS_SSL_PROTO_DTLS)
5036 /* We might have buffered a future record; if so,
5037 * and if the epoch matches now, load it.
5038 * On success, this call will set ssl->in_left to
5039 * the length of the buffered record, so that
5040 * the calls to ssl_fetch_input() below will
5041 * essentially be no-ops. */
5042 ret = ssl_load_buffered_record( ssl );
5043 if( ret != 0 )
5044 return( ret );
5045#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005050 return( ret );
5051 }
5052
5053 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005056 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5057 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005058 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005059 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5060 {
5061 ret = ssl_buffer_future_record( ssl );
5062 if( ret != 0 )
5063 return( ret );
5064
5065 /* Fall through to handling of unexpected records */
5066 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5067 }
5068
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005069 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5070 {
5071 /* Skip unexpected record (but not whole datagram) */
5072 ssl->next_record_offset = ssl->in_msglen
5073 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005074
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5076 "(header)" ) );
5077 }
5078 else
5079 {
5080 /* Skip invalid record and the rest of the datagram */
5081 ssl->next_record_offset = 0;
5082 ssl->in_left = 0;
5083
5084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5085 "(header)" ) );
5086 }
5087
5088 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005089 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005090 }
5091#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005092 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005093 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005094
5095 /*
5096 * Read and optionally decrypt the message contents
5097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5099 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005102 return( ret );
5103 }
5104
5105 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005107 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005110 if( ssl->next_record_offset < ssl->in_left )
5111 {
5112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5113 }
5114 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005115 else
5116#endif
5117 ssl->in_left = 0;
5118
5119 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005123 {
5124 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5126 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005127 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005128 /* Except when waiting for Finished as a bad mac here
5129 * probably means something went wrong in the handshake
5130 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5131 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5132 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5133 {
5134#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5135 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5136 {
5137 mbedtls_ssl_send_alert_message( ssl,
5138 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5139 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5140 }
5141#endif
5142 return( ret );
5143 }
5144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005146 if( ssl->conf->badmac_limit != 0 &&
5147 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5150 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005151 }
5152#endif
5153
Hanno Becker4a810fb2017-05-24 16:27:30 +01005154 /* As above, invalid records cause
5155 * dismissal of the whole datagram. */
5156
5157 ssl->next_record_offset = 0;
5158 ssl->in_left = 0;
5159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005161 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005162 }
5163
5164 return( ret );
5165 }
5166 else
5167#endif
5168 {
5169 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5171 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005173 mbedtls_ssl_send_alert_message( ssl,
5174 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5175 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005176 }
5177#endif
5178 return( ret );
5179 }
5180 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005181
Simon Butcher99000142016-10-13 17:21:01 +01005182 return( 0 );
5183}
5184
5185int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5186{
5187 int ret;
5188
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005189 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005190 * Handle particular types of records
5191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005193 {
Simon Butcher99000142016-10-13 17:21:01 +01005194 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5195 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005196 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005198 }
5199
Hanno Beckere678eaa2018-08-21 14:57:46 +01005200 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005201 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005202 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005203 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5205 ssl->in_msglen ) );
5206 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005207 }
5208
Hanno Beckere678eaa2018-08-21 14:57:46 +01005209 if( ssl->in_msg[0] != 1 )
5210 {
5211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5212 ssl->in_msg[0] ) );
5213 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5214 }
5215
5216#if defined(MBEDTLS_SSL_PROTO_DTLS)
5217 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5218 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5219 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5220 {
5221 if( ssl->handshake == NULL )
5222 {
5223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5224 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5225 }
5226
5227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5228 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5229 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005230#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005231 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005234 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005235 if( ssl->in_msglen != 2 )
5236 {
5237 /* Note: Standard allows for more than one 2 byte alert
5238 to be packed in a single message, but Mbed TLS doesn't
5239 currently support this. */
5240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5241 ssl->in_msglen ) );
5242 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5243 }
5244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005246 ssl->in_msg[0], ssl->in_msg[1] ) );
5247
5248 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005249 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005254 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005256 }
5257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5259 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5262 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005263 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005264
5265#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5266 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5267 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5268 {
Hanno Becker90333da2017-10-10 11:27:13 +01005269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005270 /* Will be handled when trying to parse ServerHello */
5271 return( 0 );
5272 }
5273#endif
5274
5275#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5276 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5277 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5278 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5279 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5280 {
5281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5282 /* Will be handled in mbedtls_ssl_parse_certificate() */
5283 return( 0 );
5284 }
5285#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5286
5287 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005288 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005289 }
5290
Hanno Beckerc76c6192017-06-06 10:03:17 +01005291#if defined(MBEDTLS_SSL_PROTO_DTLS)
5292 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5293 ssl->handshake != NULL &&
5294 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5295 {
5296 ssl_handshake_wrapup_free_hs_transform( ssl );
5297 }
5298#endif
5299
Paul Bakker5121ce52009-01-03 21:22:43 +00005300 return( 0 );
5301}
5302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005304{
5305 int ret;
5306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5308 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5309 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005310 {
5311 return( ret );
5312 }
5313
5314 return( 0 );
5315}
5316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005318 unsigned char level,
5319 unsigned char message )
5320{
5321 int ret;
5322
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005323 if( ssl == NULL || ssl->conf == NULL )
5324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005330 ssl->out_msglen = 2;
5331 ssl->out_msg[0] = level;
5332 ssl->out_msg[1] = message;
5333
Hanno Becker67bc7c32018-08-06 11:33:50 +01005334 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005337 return( ret );
5338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005340
5341 return( 0 );
5342}
5343
Paul Bakker5121ce52009-01-03 21:22:43 +00005344/*
5345 * Handshake functions
5346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5348 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5349 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5350 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5351 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5352 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5353 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005354/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005356{
Hanno Becker8759e162017-12-27 21:34:08 +00005357 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5362 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005363 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5364 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005367 ssl->state++;
5368 return( 0 );
5369 }
5370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5372 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005373}
5374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005376{
Hanno Becker8759e162017-12-27 21:34:08 +00005377 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5382 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005383 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5384 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005387 ssl->state++;
5388 return( 0 );
5389 }
5390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005393}
Gilles Peskinef9828522017-05-03 12:28:43 +02005394
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005395#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005396/* Some certificate support -> implement write and parse */
5397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005401 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00005403 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5408 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005409 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5410 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005413 ssl->state++;
5414 return( 0 );
5415 }
5416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005418 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 {
5420 if( ssl->client_auth == 0 )
5421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 ssl->state++;
5424 return( 0 );
5425 }
5426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005428 /*
5429 * If using SSLv3 and got no cert, send an Alert message
5430 * (otherwise an empty Certificate message will be sent).
5431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5433 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005434 {
5435 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5437 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5438 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005441 goto write_msg;
5442 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445#endif /* MBEDTLS_SSL_CLI_C */
5446#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005447 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5452 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 }
5454 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005455#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005458
5459 /*
5460 * 0 . 0 handshake type
5461 * 1 . 3 handshake length
5462 * 4 . 6 length of all certs
5463 * 7 . 9 length of cert. 1
5464 * 10 . n-1 peer certificate
5465 * n . n+2 length of cert. 2
5466 * n+3 . ... upper level cert, etc.
5467 */
5468 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005470
Paul Bakker29087132010-03-21 21:03:34 +00005471 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 {
5473 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005474 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005477 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 }
5480
5481 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5482 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5483 ssl->out_msg[i + 2] = (unsigned char)( n );
5484
5485 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5486 i += n; crt = crt->next;
5487 }
5488
5489 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5490 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5491 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5492
5493 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5495 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005496
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005497#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005498write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005499#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005500
5501 ssl->state++;
5502
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005503 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 return( ret );
5507 }
5508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005510
Paul Bakkered27a042013-04-18 22:46:23 +02005511 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512}
5513
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005514/*
5515 * Once the certificate message is read, parse it into a cert chain and
5516 * perform basic checks, but leave actual verification to the caller
5517 */
5518static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005519{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005520 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005521 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005522 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#if defined(MBEDTLS_SSL_SRV_C)
5525#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005526 /*
5527 * Check if the client sent an empty certificate
5528 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005529 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005531 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005532 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5534 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5535 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005538
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005539 /* The client was asked for a certificate but didn't send
5540 one. The client should know what's going on, so we
5541 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005542 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005543 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005544 }
5545 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5549 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005550 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5554 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5555 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5556 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005559
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005560 /* The client was asked for a certificate but didn't send
5561 one. The client should know what's going on, so we
5562 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005563 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005564 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005565 }
5566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5568 MBEDTLS_SSL_PROTO_TLS1_2 */
5569#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005574 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5575 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005577 }
5578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5580 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005583 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5584 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005586 }
5587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005589
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005592 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005593 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005594
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005595 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005599 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5600 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005602 }
5603
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005604 /* In case we tried to reuse a session but it failed */
5605 if( ssl->session_negotiate->peer_cert != NULL )
5606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5608 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005609 }
5610
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005611 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5617 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005618 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005619 }
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005622
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005623 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005624
5625 while( i < ssl->in_hslen )
5626 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005627 if ( i + 3 > ssl->in_hslen ) {
5628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5629 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5630 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5631 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005633 if( ssl->in_msg[i] != 0 )
5634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005636 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5637 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 }
5640
5641 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5642 | (unsigned int) ssl->in_msg[i + 2];
5643 i += 3;
5644
5645 if( n < 128 || i + n > ssl->in_hslen )
5646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005648 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5649 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005651 }
5652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005654 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005655 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005656 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005657 case 0: /*ok*/
5658 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5659 /* Ignore certificate with an unknown algorithm: maybe a
5660 prior certificate was already trusted. */
5661 break;
5662
5663 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5664 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5665 goto crt_parse_der_failed;
5666
5667 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5668 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5669 goto crt_parse_der_failed;
5670
5671 default:
5672 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5673 crt_parse_der_failed:
5674 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 return( ret );
5677 }
5678
5679 i += n;
5680 }
5681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005684 /*
5685 * On client, make sure the server cert doesn't change during renego to
5686 * avoid "triple handshake" attack: https://secure-resumption.com/
5687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005689 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005691 {
5692 if( ssl->session->peer_cert == NULL )
5693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005695 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5696 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005698 }
5699
5700 if( ssl->session->peer_cert->raw.len !=
5701 ssl->session_negotiate->peer_cert->raw.len ||
5702 memcmp( ssl->session->peer_cert->raw.p,
5703 ssl->session_negotiate->peer_cert->raw.p,
5704 ssl->session->peer_cert->raw.len ) != 0 )
5705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005707 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5708 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005710 }
5711 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005713
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005714 return( 0 );
5715}
5716
5717int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5718{
5719 int ret;
5720 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00005721 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005722#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5723 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5724 ? ssl->handshake->sni_authmode
5725 : ssl->conf->authmode;
5726#else
5727 const int authmode = ssl->conf->authmode;
5728#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005729 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005730
5731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5732
5733 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5734 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5735 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5736 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5737 {
5738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5739 ssl->state++;
5740 return( 0 );
5741 }
5742
5743#if defined(MBEDTLS_SSL_SRV_C)
5744 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5745 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5746 {
5747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5748 ssl->state++;
5749 return( 0 );
5750 }
5751
5752 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5753 authmode == MBEDTLS_SSL_VERIFY_NONE )
5754 {
5755 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005757
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005758 ssl->state++;
5759 return( 0 );
5760 }
5761#endif
5762
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005763#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5764 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005765 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005766 {
5767 goto crt_verify;
5768 }
5769#endif
5770
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005771 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005772 {
5773 /* mbedtls_ssl_read_record may have sent an alert already. We
5774 let it decide whether to alert. */
5775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5776 return( ret );
5777 }
5778
5779 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5780 {
5781#if defined(MBEDTLS_SSL_SRV_C)
5782 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5783 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5784 {
5785 ret = 0;
5786 }
5787#endif
5788
5789 ssl->state++;
5790 return( ret );
5791 }
5792
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005793#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5794 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005795 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005796
5797crt_verify:
5798 if( ssl->handshake->ecrs_enabled)
5799 rs_ctx = &ssl->handshake->ecrs_ctx;
5800#endif
5801
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005802 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005804 mbedtls_x509_crt *ca_chain;
5805 mbedtls_x509_crl *ca_crl;
5806
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005807#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005808 if( ssl->handshake->sni_ca_chain != NULL )
5809 {
5810 ca_chain = ssl->handshake->sni_ca_chain;
5811 ca_crl = ssl->handshake->sni_ca_crl;
5812 }
5813 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005814#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005815 {
5816 ca_chain = ssl->conf->ca_chain;
5817 ca_crl = ssl->conf->ca_crl;
5818 }
5819
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005820 /*
5821 * Main check: verify certificate
5822 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005823 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005824 ssl->session_negotiate->peer_cert,
5825 ca_chain, ca_crl,
5826 ssl->conf->cert_profile,
5827 ssl->hostname,
5828 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005829 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005830
5831 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005833 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005834 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005835
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005836#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5837 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005838 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005839#endif
5840
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005841 /*
5842 * Secondary checks: always done, but change 'ret' only if it was 0
5843 */
5844
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005845#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005847 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005848
5849 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005851 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005852 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005853 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005856 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005858 }
5859 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005860#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005863 ciphersuite_info,
5864 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005865 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005868 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005870 }
5871
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005872 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5873 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5874 * with details encoded in the verification flags. All other kinds
5875 * of error codes, including those from the user provided f_vrfy
5876 * functions, are treated as fatal and lead to a failure of
5877 * ssl_parse_certificate even if verification was optional. */
5878 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5879 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5880 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5881 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005883 }
5884
5885 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5886 {
5887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5888 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5889 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005890
5891 if( ret != 0 )
5892 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005893 uint8_t alert;
5894
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005895 /* The certificate may have been rejected for several reasons.
5896 Pick one and send the corresponding alert. Which alert to send
5897 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005898 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5899 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5900 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5901 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5902 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5903 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5904 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5905 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5906 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5907 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5908 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5909 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5910 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5911 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5912 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5913 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5914 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5915 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5916 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5917 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005918 else
5919 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005920 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5921 alert );
5922 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005923
Hanno Beckere6706e62017-05-15 16:05:15 +01005924#if defined(MBEDTLS_DEBUG_C)
5925 if( ssl->session_negotiate->verify_result != 0 )
5926 {
5927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5928 ssl->session_negotiate->verify_result ) );
5929 }
5930 else
5931 {
5932 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5933 }
5934#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005935 }
5936
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005937 ssl->state++;
5938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005940
5941 return( ret );
5942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5944 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5945 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5946 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5947 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5948 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5949 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005952{
5953 int ret;
5954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005958 ssl->out_msglen = 1;
5959 ssl->out_msg[0] = 1;
5960
Paul Bakker5121ce52009-01-03 21:22:43 +00005961 ssl->state++;
5962
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005963 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005964 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005966 return( ret );
5967 }
5968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005970
5971 return( 0 );
5972}
5973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005975{
5976 int ret;
5977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005979
Hanno Becker327c93b2018-08-15 13:56:18 +01005980 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005983 return( ret );
5984 }
5985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005989 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5990 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005992 }
5993
Hanno Beckere678eaa2018-08-21 14:57:46 +01005994 /* CCS records are only accepted if they have length 1 and content '1',
5995 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005996
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005997 /*
5998 * Switch to our negotiated transform and session parameters for inbound
5999 * data.
6000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006002 ssl->transform_in = ssl->transform_negotiate;
6003 ssl->session_in = ssl->session_negotiate;
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006006 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006009 ssl_dtls_replay_reset( ssl );
6010#endif
6011
6012 /* Increment epoch */
6013 if( ++ssl->in_epoch == 0 )
6014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006016 /* This is highly unlikely to happen for legitimate reasons, so
6017 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006019 }
6020 }
6021 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006023 memset( ssl->in_ctr, 0, 8 );
6024
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006025 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6028 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006033 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6034 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006036 }
6037 }
6038#endif
6039
Paul Bakker5121ce52009-01-03 21:22:43 +00006040 ssl->state++;
6041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006043
6044 return( 0 );
6045}
6046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6048 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006049{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006050 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6053 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6054 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006055 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006056 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6059#if defined(MBEDTLS_SHA512_C)
6060 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006061 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6062 else
6063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064#if defined(MBEDTLS_SHA256_C)
6065 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006066 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006067 else
6068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006072 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006073 }
Paul Bakker380da532012-04-18 16:10:25 +00006074}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6079 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006080 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6081 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6084#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006085 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006088 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006091}
6092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006094 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6097 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006098 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6099 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6102#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006103 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006106 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006109}
6110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6112 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6113static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006114 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006115{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006116 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6117 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006118}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006119#endif
Paul Bakker380da532012-04-18 16:10:25 +00006120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6122#if defined(MBEDTLS_SHA256_C)
6123static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006124 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006125{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006126 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006127}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006128#endif
Paul Bakker380da532012-04-18 16:10:25 +00006129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130#if defined(MBEDTLS_SHA512_C)
6131static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006132 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006133{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006134 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006135}
Paul Bakker769075d2012-11-24 11:26:46 +01006136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006140static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006142{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006143 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006144 mbedtls_md5_context md5;
6145 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006146
Paul Bakker5121ce52009-01-03 21:22:43 +00006147 unsigned char padbuf[48];
6148 unsigned char md5sum[16];
6149 unsigned char sha1sum[20];
6150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006152 if( !session )
6153 session = ssl->session;
6154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006156
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006157 mbedtls_md5_init( &md5 );
6158 mbedtls_sha1_init( &sha1 );
6159
6160 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6161 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006162
6163 /*
6164 * SSLv3:
6165 * hash =
6166 * MD5( master + pad2 +
6167 * MD5( handshake + sender + master + pad1 ) )
6168 * + SHA1( master + pad2 +
6169 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006170 */
6171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006173 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6174 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006175#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006178 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6179 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006180#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006183 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006184
Paul Bakker1ef83d62012-04-11 12:09:53 +00006185 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006186
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006187 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6188 mbedtls_md5_update_ret( &md5, session->master, 48 );
6189 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6190 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006191
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006192 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6193 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6194 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6195 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006196
Paul Bakker1ef83d62012-04-11 12:09:53 +00006197 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006198
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006199 mbedtls_md5_starts_ret( &md5 );
6200 mbedtls_md5_update_ret( &md5, session->master, 48 );
6201 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6202 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6203 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006204
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006205 mbedtls_sha1_starts_ret( &sha1 );
6206 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6207 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6208 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6209 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006212
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006213 mbedtls_md5_free( &md5 );
6214 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006215
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006216 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6217 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6218 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006225static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006227{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006228 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006229 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006230 mbedtls_md5_context md5;
6231 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006232 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006235 if( !session )
6236 session = ssl->session;
6237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006239
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006240 mbedtls_md5_init( &md5 );
6241 mbedtls_sha1_init( &sha1 );
6242
6243 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6244 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006245
Paul Bakker1ef83d62012-04-11 12:09:53 +00006246 /*
6247 * TLSv1:
6248 * hash = PRF( master, finished_label,
6249 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6250 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006253 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6254 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006255#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006258 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6259 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006260#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006263 ? "client finished"
6264 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006265
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006266 mbedtls_md5_finish_ret( &md5, padbuf );
6267 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006268
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006269 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006270 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006273
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006274 mbedtls_md5_free( &md5 );
6275 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006276
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006277 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6284#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006285static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006287{
6288 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006289 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006290 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006291 unsigned char padbuf[32];
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006294 if( !session )
6295 session = ssl->session;
6296
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006297 mbedtls_sha256_init( &sha256 );
6298
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006300
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006301 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006302
6303 /*
6304 * TLSv1.2:
6305 * hash = PRF( master, finished_label,
6306 * Hash( handshake ) )[0.11]
6307 */
6308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309#if !defined(MBEDTLS_SHA256_ALT)
6310 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006311 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006312#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006315 ? "client finished"
6316 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006317
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006318 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006319
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006320 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006321 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006324
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006325 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006326
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006327 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006334static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006336{
6337 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006338 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006339 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006340 unsigned char padbuf[48];
6341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006343 if( !session )
6344 session = ssl->session;
6345
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006346 mbedtls_sha512_init( &sha512 );
6347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006349
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006350 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006351
6352 /*
6353 * TLSv1.2:
6354 * hash = PRF( master, finished_label,
6355 * Hash( handshake ) )[0.11]
6356 */
6357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006359 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6360 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006361#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006364 ? "client finished"
6365 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006366
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006367 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006368
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006369 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006370 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006373
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006374 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006375
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006376 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380#endif /* MBEDTLS_SHA512_C */
6381#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006384{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006386
6387 /*
6388 * Free our handshake params
6389 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006390 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006392 ssl->handshake = NULL;
6393
6394 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006395 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006396 */
6397 if( ssl->transform )
6398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 mbedtls_ssl_transform_free( ssl->transform );
6400 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006401 }
6402 ssl->transform = ssl->transform_negotiate;
6403 ssl->transform_negotiate = NULL;
6404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006406}
6407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006409{
6410 int resume = ssl->handshake->resume;
6411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#if defined(MBEDTLS_SSL_RENEGOTIATION)
6415 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006418 ssl->renego_records_seen = 0;
6419 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006420#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006421
6422 /*
6423 * Free the previous session and switch in the current one
6424 */
Paul Bakker0a597072012-09-25 21:55:46 +00006425 if( ssl->session )
6426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006428 /* RFC 7366 3.1: keep the EtM state */
6429 ssl->session_negotiate->encrypt_then_mac =
6430 ssl->session->encrypt_then_mac;
6431#endif
6432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433 mbedtls_ssl_session_free( ssl->session );
6434 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006435 }
6436 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006437 ssl->session_negotiate = NULL;
6438
Paul Bakker0a597072012-09-25 21:55:46 +00006439 /*
6440 * Add cache entry
6441 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006442 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006443 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006444 resume == 0 )
6445 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006446 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006448 }
Paul Bakker0a597072012-09-25 21:55:46 +00006449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006451 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006452 ssl->handshake->flight != NULL )
6453 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006454 /* Cancel handshake timer */
6455 ssl_set_timer( ssl, 0 );
6456
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006457 /* Keep last flight around in case we need to resend it:
6458 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006460 }
6461 else
6462#endif
6463 ssl_handshake_wrapup_free_hs_transform( ssl );
6464
Paul Bakker48916f92012-09-16 19:57:18 +00006465 ssl->state++;
6466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006468}
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006471{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006472 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006475
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006476 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006477
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006478 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006479
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006480 /*
6481 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6482 * may define some other value. Currently (early 2016), no defined
6483 * ciphersuite does this (and this is unlikely to change as activity has
6484 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006489 ssl->verify_data_len = hash_len;
6490 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006491#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006492
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6495 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006496
6497 /*
6498 * In case of session resuming, invert the client and server
6499 * ChangeCipherSpec messages order.
6500 */
Paul Bakker0a597072012-09-25 21:55:46 +00006501 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006504 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006508 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006510#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 }
6512 else
6513 ssl->state++;
6514
Paul Bakker48916f92012-09-16 19:57:18 +00006515 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006516 * Switch to our negotiated transform and session parameters for outbound
6517 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006522 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006523 {
6524 unsigned char i;
6525
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006526 /* Remember current epoch settings for resending */
6527 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006528 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006529
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006530 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006531 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006532
6533 /* Increment epoch */
6534 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006535 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006536 break;
6537
6538 /* The loop goes to its end iff the counter is wrapping */
6539 if( i == 0 )
6540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6542 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006543 }
6544 }
6545 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006547 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006548
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006549 ssl->transform_out = ssl->transform_negotiate;
6550 ssl->session_out = ssl->session_negotiate;
6551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6553 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6558 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006559 }
6560 }
6561#endif
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006564 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006566#endif
6567
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006568 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006569 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 return( ret );
6572 }
6573
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006574#if defined(MBEDTLS_SSL_PROTO_DTLS)
6575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6576 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6577 {
6578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6579 return( ret );
6580 }
6581#endif
6582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584
6585 return( 0 );
6586}
6587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006589#define SSL_MAX_HASH_LEN 36
6590#else
6591#define SSL_MAX_HASH_LEN 12
6592#endif
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006595{
Paul Bakker23986e52011-04-24 08:57:21 +00006596 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006597 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006598 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006601
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006602 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603
Hanno Becker327c93b2018-08-15 13:56:18 +01006604 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607 return( ret );
6608 }
6609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006613 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6614 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006616 }
6617
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006618 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619#if defined(MBEDTLS_SSL_PROTO_SSL3)
6620 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006621 hash_len = 36;
6622 else
6623#endif
6624 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6627 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006630 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6631 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006633 }
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006636 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006639 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6640 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 }
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006645 ssl->verify_data_len = hash_len;
6646 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006647#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006648
Paul Bakker0a597072012-09-25 21:55:46 +00006649 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006652 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006654#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006656 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006658#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006659 }
6660 else
6661 ssl->state++;
6662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006664 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006666#endif
6667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006669
6670 return( 0 );
6671}
6672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6678 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6679 mbedtls_md5_init( &handshake->fin_md5 );
6680 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006681 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6682 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6685#if defined(MBEDTLS_SHA256_C)
6686 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006687 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006688#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#if defined(MBEDTLS_SHA512_C)
6690 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006691 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006692#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006694
6695 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006696
6697#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6698 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6699 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6700#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702#if defined(MBEDTLS_DHM_C)
6703 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if defined(MBEDTLS_ECDH_C)
6706 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006707#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006708#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006709 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006710#if defined(MBEDTLS_SSL_CLI_C)
6711 handshake->ecjpake_cache = NULL;
6712 handshake->ecjpake_cache_len = 0;
6713#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006714#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006715
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006716#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006717 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006718#endif
6719
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006720#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6721 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6722#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006723}
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6730 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732 mbedtls_md_init( &transform->md_ctx_enc );
6733 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006734}
6735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006737{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006739}
6740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006742{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006743 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006744 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006746 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006748 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006749 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006750
6751 /*
6752 * Either the pointers are now NULL or cleared properly and can be freed.
6753 * Now allocate missing structures.
6754 */
6755 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006756 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006757 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006758 }
Paul Bakker48916f92012-09-16 19:57:18 +00006759
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006760 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006761 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006762 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006763 }
Paul Bakker48916f92012-09-16 19:57:18 +00006764
Paul Bakker82788fb2014-10-20 13:59:19 +02006765 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006766 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006767 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006768 }
Paul Bakker48916f92012-09-16 19:57:18 +00006769
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006770 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006771 if( ssl->handshake == NULL ||
6772 ssl->transform_negotiate == NULL ||
6773 ssl->session_negotiate == NULL )
6774 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 mbedtls_free( ssl->handshake );
6778 mbedtls_free( ssl->transform_negotiate );
6779 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006780
6781 ssl->handshake = NULL;
6782 ssl->transform_negotiate = NULL;
6783 ssl->session_negotiate = NULL;
6784
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006785 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006786 }
6787
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006788 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006790 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006791 ssl_handshake_params_init( ssl->handshake );
6792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6795 {
6796 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006797
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006798 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6799 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6800 else
6801 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006802
6803 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006804 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006805#endif
6806
Paul Bakker48916f92012-09-16 19:57:18 +00006807 return( 0 );
6808}
6809
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006810#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006811/* Dummy cookie callbacks for defaults */
6812static int ssl_cookie_write_dummy( void *ctx,
6813 unsigned char **p, unsigned char *end,
6814 const unsigned char *cli_id, size_t cli_id_len )
6815{
6816 ((void) ctx);
6817 ((void) p);
6818 ((void) end);
6819 ((void) cli_id);
6820 ((void) cli_id_len);
6821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006823}
6824
6825static int ssl_cookie_check_dummy( void *ctx,
6826 const unsigned char *cookie, size_t cookie_len,
6827 const unsigned char *cli_id, size_t cli_id_len )
6828{
6829 ((void) ctx);
6830 ((void) cookie);
6831 ((void) cookie_len);
6832 ((void) cli_id);
6833 ((void) cli_id_len);
6834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006836}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006837#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006838
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006839/* Once ssl->out_hdr as the address of the beginning of the
6840 * next outgoing record is set, deduce the other pointers.
6841 *
6842 * Note: For TLS, we save the implicit record sequence number
6843 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6844 * and the caller has to make sure there's space for this.
6845 */
6846
6847static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6848 mbedtls_ssl_transform *transform )
6849{
6850#if defined(MBEDTLS_SSL_PROTO_DTLS)
6851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6852 {
6853 ssl->out_ctr = ssl->out_hdr + 3;
6854 ssl->out_len = ssl->out_hdr + 11;
6855 ssl->out_iv = ssl->out_hdr + 13;
6856 }
6857 else
6858#endif
6859 {
6860 ssl->out_ctr = ssl->out_hdr - 8;
6861 ssl->out_len = ssl->out_hdr + 3;
6862 ssl->out_iv = ssl->out_hdr + 5;
6863 }
6864
6865 /* Adjust out_msg to make space for explicit IV, if used. */
6866 if( transform != NULL &&
6867 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6868 {
6869 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6870 }
6871 else
6872 ssl->out_msg = ssl->out_iv;
6873}
6874
6875/* Once ssl->in_hdr as the address of the beginning of the
6876 * next incoming record is set, deduce the other pointers.
6877 *
6878 * Note: For TLS, we save the implicit record sequence number
6879 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6880 * and the caller has to make sure there's space for this.
6881 */
6882
6883static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6884 mbedtls_ssl_transform *transform )
6885{
6886#if defined(MBEDTLS_SSL_PROTO_DTLS)
6887 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6888 {
6889 ssl->in_ctr = ssl->in_hdr + 3;
6890 ssl->in_len = ssl->in_hdr + 11;
6891 ssl->in_iv = ssl->in_hdr + 13;
6892 }
6893 else
6894#endif
6895 {
6896 ssl->in_ctr = ssl->in_hdr - 8;
6897 ssl->in_len = ssl->in_hdr + 3;
6898 ssl->in_iv = ssl->in_hdr + 5;
6899 }
6900
6901 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6902 if( transform != NULL &&
6903 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6904 {
6905 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6906 }
6907 else
6908 ssl->in_msg = ssl->in_iv;
6909}
6910
Paul Bakker5121ce52009-01-03 21:22:43 +00006911/*
6912 * Initialize an SSL context
6913 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006914void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6915{
6916 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6917}
6918
6919/*
6920 * Setup an SSL context
6921 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006922
6923static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
6924{
6925 /* Set the incoming and outgoing record pointers. */
6926#if defined(MBEDTLS_SSL_PROTO_DTLS)
6927 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6928 {
6929 ssl->out_hdr = ssl->out_buf;
6930 ssl->in_hdr = ssl->in_buf;
6931 }
6932 else
6933#endif /* MBEDTLS_SSL_PROTO_DTLS */
6934 {
6935 ssl->out_hdr = ssl->out_buf + 8;
6936 ssl->in_hdr = ssl->in_buf + 8;
6937 }
6938
6939 /* Derive other internal pointers. */
6940 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6941 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6942}
6943
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006944int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006945 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006946{
Paul Bakker48916f92012-09-16 19:57:18 +00006947 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006948
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006949 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006950
6951 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006952 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006953 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02006954
6955 /* Set to NULL in case of an error condition */
6956 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02006957
Angus Grattond8213d02016-05-25 20:56:48 +10006958 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6959 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006960 {
Angus Grattond8213d02016-05-25 20:56:48 +10006961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006962 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006963 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10006964 }
6965
6966 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6967 if( ssl->out_buf == NULL )
6968 {
6969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02006970 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02006971 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006972 }
6973
Hanno Becker2a43f6f2018-08-10 11:12:52 +01006974 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006975
Paul Bakker48916f92012-09-16 19:57:18 +00006976 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02006977 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00006978
6979 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02006980
6981error:
6982 mbedtls_free( ssl->in_buf );
6983 mbedtls_free( ssl->out_buf );
6984
6985 ssl->conf = NULL;
6986
6987 ssl->in_buf = NULL;
6988 ssl->out_buf = NULL;
6989
6990 ssl->in_hdr = NULL;
6991 ssl->in_ctr = NULL;
6992 ssl->in_len = NULL;
6993 ssl->in_iv = NULL;
6994 ssl->in_msg = NULL;
6995
6996 ssl->out_hdr = NULL;
6997 ssl->out_ctr = NULL;
6998 ssl->out_len = NULL;
6999 ssl->out_iv = NULL;
7000 ssl->out_msg = NULL;
7001
k-stachowiak9f7798e2018-07-31 16:52:32 +02007002 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003}
7004
7005/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007006 * Reset an initialized and used SSL context for re-use while retaining
7007 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007008 *
7009 * If partial is non-zero, keep data in the input buffer and client ID.
7010 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007011 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007012static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007013{
Paul Bakker48916f92012-09-16 19:57:18 +00007014 int ret;
7015
Hanno Becker7e772132018-08-10 12:38:21 +01007016#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7017 !defined(MBEDTLS_SSL_SRV_C)
7018 ((void) partial);
7019#endif
7020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007022
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007023 /* Cancel any possibly running timer */
7024 ssl_set_timer( ssl, 0 );
7025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026#if defined(MBEDTLS_SSL_RENEGOTIATION)
7027 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007028 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007029
7030 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7032 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007035
Paul Bakker7eb013f2011-10-06 12:37:39 +00007036 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007037 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007038
7039 ssl->in_msgtype = 0;
7040 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007042 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007043 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007046 ssl_dtls_replay_reset( ssl );
7047#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007048
7049 ssl->in_hslen = 0;
7050 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007051
7052 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007053
7054 ssl->out_msgtype = 0;
7055 ssl->out_msglen = 0;
7056 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7058 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007059 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007060#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007061
Hanno Becker19859472018-08-06 09:40:20 +01007062 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7063
Paul Bakker48916f92012-09-16 19:57:18 +00007064 ssl->transform_in = NULL;
7065 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007066
Hanno Becker78640902018-08-13 16:35:15 +01007067 ssl->session_in = NULL;
7068 ssl->session_out = NULL;
7069
Angus Grattond8213d02016-05-25 20:56:48 +10007070 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007071
7072#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007073 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007074#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7075 {
7076 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007077 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007078 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7081 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7084 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7087 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007088 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007089 }
7090#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007091
Paul Bakker48916f92012-09-16 19:57:18 +00007092 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 mbedtls_ssl_transform_free( ssl->transform );
7095 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007096 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007097 }
Paul Bakker48916f92012-09-16 19:57:18 +00007098
Paul Bakkerc0463502013-02-14 11:19:38 +01007099 if( ssl->session )
7100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 mbedtls_ssl_session_free( ssl->session );
7102 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007103 ssl->session = NULL;
7104 }
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007107 ssl->alpn_chosen = NULL;
7108#endif
7109
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007110#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007111#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007112 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007113#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007114 {
7115 mbedtls_free( ssl->cli_id );
7116 ssl->cli_id = NULL;
7117 ssl->cli_id_len = 0;
7118 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007119#endif
7120
Paul Bakker48916f92012-09-16 19:57:18 +00007121 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7122 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007123
7124 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007125}
7126
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007127/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007128 * Reset an initialized and used SSL context for re-use while retaining
7129 * all application-set variables, function pointers and data.
7130 */
7131int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7132{
7133 return( ssl_session_reset_int( ssl, 0 ) );
7134}
7135
7136/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007137 * SSL set accessors
7138 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007139void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007140{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007141 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007142}
7143
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007144void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007145{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007146 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007147}
7148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007150void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007152 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007153}
7154#endif
7155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007157void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007158{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007159 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007160}
7161#endif
7162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007164
Hanno Becker1841b0a2018-08-24 11:13:57 +01007165void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7166 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007167{
7168 ssl->disable_datagram_packing = !allow_packing;
7169}
7170
7171void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7172 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007174 conf->hs_timeout_min = min;
7175 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007176}
7177#endif
7178
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007179void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007180{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007181 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007182}
7183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007185void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007186 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007187 void *p_vrfy )
7188{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007189 conf->f_vrfy = f_vrfy;
7190 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007193
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007194void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007195 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007196 void *p_rng )
7197{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007198 conf->f_rng = f_rng;
7199 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007200}
7201
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007202void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007203 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007204 void *p_dbg )
7205{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007206 conf->f_dbg = f_dbg;
7207 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007208}
7209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007211 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007212 mbedtls_ssl_send_t *f_send,
7213 mbedtls_ssl_recv_t *f_recv,
7214 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007215{
7216 ssl->p_bio = p_bio;
7217 ssl->f_send = f_send;
7218 ssl->f_recv = f_recv;
7219 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007220}
7221
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007222#if defined(MBEDTLS_SSL_PROTO_DTLS)
7223void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7224{
7225 ssl->mtu = mtu;
7226}
7227#endif
7228
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007229void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007230{
7231 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007232}
7233
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007234void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7235 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007236 mbedtls_ssl_set_timer_t *f_set_timer,
7237 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007238{
7239 ssl->p_timer = p_timer;
7240 ssl->f_set_timer = f_set_timer;
7241 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007242
7243 /* Make sure we start with no timer running */
7244 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007245}
7246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007248void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007249 void *p_cache,
7250 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7251 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007252{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007253 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007254 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007255 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SSL_CLI_C)
7260int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007261{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007262 int ret;
7263
7264 if( ssl == NULL ||
7265 session == NULL ||
7266 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007267 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007270 }
7271
7272 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7273 return( ret );
7274
Paul Bakker0a597072012-09-25 21:55:46 +00007275 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007276
7277 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007280
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007281void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007282 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007283{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007284 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7285 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7286 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7287 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007288}
7289
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007290void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007291 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007292 int major, int minor )
7293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007295 return;
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007298 return;
7299
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007300 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007301}
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007304void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007305 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007306{
7307 conf->cert_profile = profile;
7308}
7309
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007310/* Append a new keycert entry to a (possibly empty) list */
7311static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7312 mbedtls_x509_crt *cert,
7313 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007314{
niisato8ee24222018-06-25 19:05:48 +09007315 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007316
niisato8ee24222018-06-25 19:05:48 +09007317 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7318 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007319 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007320
niisato8ee24222018-06-25 19:05:48 +09007321 new_cert->cert = cert;
7322 new_cert->key = key;
7323 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007324
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007325 /* Update head is the list was null, else add to the end */
7326 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007327 {
niisato8ee24222018-06-25 19:05:48 +09007328 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007329 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007330 else
7331 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007332 mbedtls_ssl_key_cert *cur = *head;
7333 while( cur->next != NULL )
7334 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007335 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007336 }
7337
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007338 return( 0 );
7339}
7340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007341int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007342 mbedtls_x509_crt *own_cert,
7343 mbedtls_pk_context *pk_key )
7344{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007345 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007346}
7347
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007348void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007349 mbedtls_x509_crt *ca_chain,
7350 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007351{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007352 conf->ca_chain = ca_chain;
7353 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007356
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007357#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7358int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7359 mbedtls_x509_crt *own_cert,
7360 mbedtls_pk_context *pk_key )
7361{
7362 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7363 own_cert, pk_key ) );
7364}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007365
7366void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7367 mbedtls_x509_crt *ca_chain,
7368 mbedtls_x509_crl *ca_crl )
7369{
7370 ssl->handshake->sni_ca_chain = ca_chain;
7371 ssl->handshake->sni_ca_crl = ca_crl;
7372}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007373
7374void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7375 int authmode )
7376{
7377 ssl->handshake->sni_authmode = authmode;
7378}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007379#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7380
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007381#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007382/*
7383 * Set EC J-PAKE password for current handshake
7384 */
7385int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7386 const unsigned char *pw,
7387 size_t pw_len )
7388{
7389 mbedtls_ecjpake_role role;
7390
Janos Follath8eb64132016-06-03 15:40:57 +01007391 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7393
7394 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7395 role = MBEDTLS_ECJPAKE_SERVER;
7396 else
7397 role = MBEDTLS_ECJPAKE_CLIENT;
7398
7399 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7400 role,
7401 MBEDTLS_MD_SHA256,
7402 MBEDTLS_ECP_DP_SECP256R1,
7403 pw, pw_len ) );
7404}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007405#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007408int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007409 const unsigned char *psk, size_t psk_len,
7410 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007411{
Paul Bakker6db455e2013-09-18 17:29:31 +02007412 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02007414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01007417
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007418 /* Identity len will be encoded on two bytes */
7419 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007420 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007421 {
7422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7423 }
7424
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007425 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02007426 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007427 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007428
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007429 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007430 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007431 conf->psk_len = 0;
7432 }
7433 if( conf->psk_identity != NULL )
7434 {
7435 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02007436 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007437 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02007438 }
7439
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007440 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
7441 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05007442 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007443 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007444 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007445 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02007446 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007447 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05007448 }
Paul Bakker6db455e2013-09-18 17:29:31 +02007449
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007450 conf->psk_len = psk_len;
7451 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02007452
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007453 memcpy( conf->psk, psk, conf->psk_len );
7454 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02007455
7456 return( 0 );
7457}
7458
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007459int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7460 const unsigned char *psk, size_t psk_len )
7461{
7462 if( psk == NULL || ssl->handshake == NULL )
7463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7464
7465 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7467
7468 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007469 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007470 mbedtls_platform_zeroize( ssl->handshake->psk,
7471 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01007472 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01007473 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01007474 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007475
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007476 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007477 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007478
7479 ssl->handshake->psk_len = psk_len;
7480 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7481
7482 return( 0 );
7483}
7484
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007485void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007487 size_t),
7488 void *p_psk )
7489{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007490 conf->f_psk = f_psk;
7491 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007494
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007495#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007496
7497#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007498int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007499{
7500 int ret;
7501
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007502 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7503 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7504 {
7505 mbedtls_mpi_free( &conf->dhm_P );
7506 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007509
7510 return( 0 );
7511}
Hanno Becker470a8c42017-10-04 15:28:46 +01007512#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007513
Hanno Beckera90658f2017-10-04 15:29:08 +01007514int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7515 const unsigned char *dhm_P, size_t P_len,
7516 const unsigned char *dhm_G, size_t G_len )
7517{
7518 int ret;
7519
7520 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7521 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7522 {
7523 mbedtls_mpi_free( &conf->dhm_P );
7524 mbedtls_mpi_free( &conf->dhm_G );
7525 return( ret );
7526 }
7527
7528 return( 0 );
7529}
Paul Bakker5121ce52009-01-03 21:22:43 +00007530
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007531int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007532{
7533 int ret;
7534
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007535 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7536 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7537 {
7538 mbedtls_mpi_free( &conf->dhm_P );
7539 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007540 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007541 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007542
7543 return( 0 );
7544}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007545#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007546
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007547#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7548/*
7549 * Set the minimum length for Diffie-Hellman parameters
7550 */
7551void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7552 unsigned int bitlen )
7553{
7554 conf->dhm_min_bitlen = bitlen;
7555}
7556#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7557
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007558#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007559/*
7560 * Set allowed/preferred hashes for handshake signatures
7561 */
7562void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7563 const int *hashes )
7564{
7565 conf->sig_hashes = hashes;
7566}
Hanno Becker947194e2017-04-07 13:25:49 +01007567#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007568
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007569#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007570/*
7571 * Set the allowed elliptic curves
7572 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007573void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007574 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007576 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007577}
Hanno Becker947194e2017-04-07 13:25:49 +01007578#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007579
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007580#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007582{
Hanno Becker947194e2017-04-07 13:25:49 +01007583 /* Initialize to suppress unnecessary compiler warning */
7584 size_t hostname_len = 0;
7585
7586 /* Check if new hostname is valid before
7587 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007588 if( hostname != NULL )
7589 {
7590 hostname_len = strlen( hostname );
7591
7592 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7594 }
7595
7596 /* Now it's clear that we will overwrite the old hostname,
7597 * so we can free it safely */
7598
7599 if( ssl->hostname != NULL )
7600 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007601 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007602 mbedtls_free( ssl->hostname );
7603 }
7604
7605 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007606
Paul Bakker5121ce52009-01-03 21:22:43 +00007607 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007608 {
7609 ssl->hostname = NULL;
7610 }
7611 else
7612 {
7613 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007614 if( ssl->hostname == NULL )
7615 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007616
Hanno Becker947194e2017-04-07 13:25:49 +01007617 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007618
Hanno Becker947194e2017-04-07 13:25:49 +01007619 ssl->hostname[hostname_len] = '\0';
7620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007621
7622 return( 0 );
7623}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007624#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007625
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007626#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007627void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007629 const unsigned char *, size_t),
7630 void *p_sni )
7631{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007632 conf->f_sni = f_sni;
7633 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007634}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007638int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007639{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007640 size_t cur_len, tot_len;
7641 const char **p;
7642
7643 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007644 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7645 * MUST NOT be truncated."
7646 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007647 */
7648 tot_len = 0;
7649 for( p = protos; *p != NULL; p++ )
7650 {
7651 cur_len = strlen( *p );
7652 tot_len += cur_len;
7653
7654 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007656 }
7657
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007658 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007659
7660 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007661}
7662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007664{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007665 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007668
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007669void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007671 conf->max_major_ver = major;
7672 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007673}
7674
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007675void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007676{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007677 conf->min_major_ver = major;
7678 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007679}
7680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007682void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007683{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007684 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007685}
7686#endif
7687
Janos Follath088ce432017-04-10 12:42:31 +01007688#if defined(MBEDTLS_SSL_SRV_C)
7689void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7690 char cert_req_ca_list )
7691{
7692 conf->cert_req_ca_list = cert_req_ca_list;
7693}
7694#endif
7695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007697void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007698{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007699 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007700}
7701#endif
7702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007704void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007706 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007707}
7708#endif
7709
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007710#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007711void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007712{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007713 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007714}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007715#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007718int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007721 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007724 }
7725
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007726 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007727
7728 return( 0 );
7729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007733void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007734{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007735 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007740void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007741{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007742 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007743}
7744#endif
7745
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007746void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007747{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007748 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007749}
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007752void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007754 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007755}
7756
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007757void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007758{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007759 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007760}
7761
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007762void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007763 const unsigned char period[8] )
7764{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007765 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007770#if defined(MBEDTLS_SSL_CLI_C)
7771void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007772{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007773 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007774}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007775#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007776
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007777#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007778void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7779 mbedtls_ssl_ticket_write_t *f_ticket_write,
7780 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7781 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007782{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007783 conf->f_ticket_write = f_ticket_write;
7784 conf->f_ticket_parse = f_ticket_parse;
7785 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007786}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007789
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007790#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7791void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7792 mbedtls_ssl_export_keys_t *f_export_keys,
7793 void *p_export_keys )
7794{
7795 conf->f_export_keys = f_export_keys;
7796 conf->p_export_keys = p_export_keys;
7797}
7798#endif
7799
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007800#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007801void mbedtls_ssl_conf_async_private_cb(
7802 mbedtls_ssl_config *conf,
7803 mbedtls_ssl_async_sign_t *f_async_sign,
7804 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7805 mbedtls_ssl_async_resume_t *f_async_resume,
7806 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007807 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007808{
7809 conf->f_async_sign_start = f_async_sign;
7810 conf->f_async_decrypt_start = f_async_decrypt;
7811 conf->f_async_resume = f_async_resume;
7812 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007813 conf->p_async_config_data = async_config_data;
7814}
7815
Gilles Peskine8f97af72018-04-26 11:46:10 +02007816void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7817{
7818 return( conf->p_async_config_data );
7819}
7820
Gilles Peskine1febfef2018-04-30 11:54:39 +02007821void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007822{
7823 if( ssl->handshake == NULL )
7824 return( NULL );
7825 else
7826 return( ssl->handshake->user_async_ctx );
7827}
7828
Gilles Peskine1febfef2018-04-30 11:54:39 +02007829void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007830 void *ctx )
7831{
7832 if( ssl->handshake != NULL )
7833 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007834}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007835#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007836
Paul Bakker5121ce52009-01-03 21:22:43 +00007837/*
7838 * SSL get accessors
7839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007841{
7842 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7843}
7844
Hanno Becker8b170a02017-10-10 11:51:19 +01007845int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7846{
7847 /*
7848 * Case A: We're currently holding back
7849 * a message for further processing.
7850 */
7851
7852 if( ssl->keep_current_message == 1 )
7853 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007855 return( 1 );
7856 }
7857
7858 /*
7859 * Case B: Further records are pending in the current datagram.
7860 */
7861
7862#if defined(MBEDTLS_SSL_PROTO_DTLS)
7863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7864 ssl->in_left > ssl->next_record_offset )
7865 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007867 return( 1 );
7868 }
7869#endif /* MBEDTLS_SSL_PROTO_DTLS */
7870
7871 /*
7872 * Case C: A handshake message is being processed.
7873 */
7874
Hanno Becker8b170a02017-10-10 11:51:19 +01007875 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7876 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007878 return( 1 );
7879 }
7880
7881 /*
7882 * Case D: An application data message is being processed
7883 */
7884 if( ssl->in_offt != NULL )
7885 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007887 return( 1 );
7888 }
7889
7890 /*
7891 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01007892 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01007893 * we implement support for multiple alerts in single records.
7894 */
7895
7896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7897 return( 0 );
7898}
7899
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007900uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007901{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007902 if( ssl->session != NULL )
7903 return( ssl->session->verify_result );
7904
7905 if( ssl->session_negotiate != NULL )
7906 return( ssl->session_negotiate->verify_result );
7907
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007908 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007909}
7910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007912{
Paul Bakker926c8e42013-03-06 10:23:34 +01007913 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007914 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007917}
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007922 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007923 {
7924 switch( ssl->minor_ver )
7925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007927 return( "DTLSv1.0" );
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007930 return( "DTLSv1.2" );
7931
7932 default:
7933 return( "unknown (DTLS)" );
7934 }
7935 }
7936#endif
7937
Paul Bakker43ca69c2011-01-15 17:35:19 +00007938 switch( ssl->minor_ver )
7939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007941 return( "SSLv3.0" );
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007944 return( "TLSv1.0" );
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007947 return( "TLSv1.1" );
7948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007950 return( "TLSv1.2" );
7951
Paul Bakker43ca69c2011-01-15 17:35:19 +00007952 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007953 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007954 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007955}
7956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007958{
Hanno Becker3136ede2018-08-17 15:28:19 +01007959 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007961 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007962
Hanno Becker78640902018-08-13 16:35:15 +01007963 if( transform == NULL )
7964 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966#if defined(MBEDTLS_ZLIB_SUPPORT)
7967 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7968 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007969#endif
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 case MBEDTLS_MODE_GCM:
7974 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007975 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007977 transform_expansion = transform->minlen;
7978 break;
7979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007981
7982 block_size = mbedtls_cipher_get_block_size(
7983 &transform->cipher_ctx_enc );
7984
Hanno Becker3136ede2018-08-17 15:28:19 +01007985 /* Expansion due to the addition of the MAC. */
7986 transform_expansion += transform->maclen;
7987
7988 /* Expansion due to the addition of CBC padding;
7989 * Theoretically up to 256 bytes, but we never use
7990 * more than the block size of the underlying cipher. */
7991 transform_expansion += block_size;
7992
7993 /* For TLS 1.1 or higher, an explicit IV is added
7994 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01007995#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7996 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01007997 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007998#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01007999
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008000 break;
8001
8002 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008005 }
8006
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008007 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008008}
8009
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008010#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8011size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8012{
8013 size_t max_len;
8014
8015 /*
8016 * Assume mfl_code is correct since it was checked when set
8017 */
Angus Grattond8213d02016-05-25 20:56:48 +10008018 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008019
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008020 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008021 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008022 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008023 {
Angus Grattond8213d02016-05-25 20:56:48 +10008024 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008025 }
8026
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008027 /* During a handshake, use the value being negotiated */
8028 if( ssl->session_negotiate != NULL &&
8029 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8030 {
8031 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8032 }
8033
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008034 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008035}
8036#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8037
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008038#if defined(MBEDTLS_SSL_PROTO_DTLS)
8039static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8040{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008041 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8042 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8043 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8044 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8045 return ( 0 );
8046
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008047 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8048 return( ssl->mtu );
8049
8050 if( ssl->mtu == 0 )
8051 return( ssl->handshake->mtu );
8052
8053 return( ssl->mtu < ssl->handshake->mtu ?
8054 ssl->mtu : ssl->handshake->mtu );
8055}
8056#endif /* MBEDTLS_SSL_PROTO_DTLS */
8057
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008058int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8059{
8060 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8061
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008062#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8063 !defined(MBEDTLS_SSL_PROTO_DTLS)
8064 (void) ssl;
8065#endif
8066
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008067#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8068 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8069
8070 if( max_len > mfl )
8071 max_len = mfl;
8072#endif
8073
8074#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008075 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008076 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008077 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008078 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8079 const size_t overhead = (size_t) ret;
8080
8081 if( ret < 0 )
8082 return( ret );
8083
8084 if( mtu <= overhead )
8085 {
8086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8087 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8088 }
8089
8090 if( max_len > mtu - overhead )
8091 max_len = mtu - overhead;
8092 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008093#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008094
Hanno Becker0defedb2018-08-10 12:35:02 +01008095#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8096 !defined(MBEDTLS_SSL_PROTO_DTLS)
8097 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008098#endif
8099
8100 return( (int) max_len );
8101}
8102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103#if defined(MBEDTLS_X509_CRT_PARSE_C)
8104const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008105{
8106 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008107 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008108
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008109 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113#if defined(MBEDTLS_SSL_CLI_C)
8114int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008115{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008116 if( ssl == NULL ||
8117 dst == NULL ||
8118 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008119 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008122 }
8123
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008124 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008127
Paul Bakker5121ce52009-01-03 21:22:43 +00008128/*
Paul Bakker1961b702013-01-25 14:49:24 +01008129 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008134
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008135 if( ssl == NULL || ssl->conf == NULL )
8136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008139 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008143 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008145#endif
8146
Paul Bakker1961b702013-01-25 14:49:24 +01008147 return( ret );
8148}
8149
8150/*
8151 * Perform the SSL handshake
8152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008153int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008154{
8155 int ret = 0;
8156
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008157 if( ssl == NULL || ssl->conf == NULL )
8158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008165
8166 if( ret != 0 )
8167 break;
8168 }
8169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008171
8172 return( ret );
8173}
8174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SSL_RENEGOTIATION)
8176#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008177/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008178 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008181{
8182 int ret;
8183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008185
8186 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8188 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008189
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008190 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008191 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008193 return( ret );
8194 }
8195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008197
8198 return( 0 );
8199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008201
8202/*
8203 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204 * - any side: calling mbedtls_ssl_renegotiate(),
8205 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8206 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008207 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008208 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008212{
8213 int ret;
8214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008216
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008217 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8218 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008219
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008220 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8221 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008225 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008226 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008227 ssl->handshake->out_msg_seq = 1;
8228 else
8229 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008230 }
8231#endif
8232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008233 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8234 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008239 return( ret );
8240 }
8241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008243
8244 return( 0 );
8245}
8246
8247/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008248 * Renegotiate current connection on client,
8249 * or request renegotiation on server
8250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008254
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008255 if( ssl == NULL || ssl->conf == NULL )
8256 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008259 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008260 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008266
8267 /* Did we already try/start sending HelloRequest? */
8268 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008269 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008270
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008271 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008272 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008276 /*
8277 * On client, either start the renegotiation process or,
8278 * if already in progress, continue the handshake
8279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008284
8285 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008288 return( ret );
8289 }
8290 }
8291 else
8292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008296 return( ret );
8297 }
8298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008300
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008301 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008302}
8303
8304/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008305 * Check record counters and renegotiate if they're above the limit.
8306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008308{
Andres AG2196c7f2016-12-15 17:01:16 +00008309 size_t ep_len = ssl_ep_len( ssl );
8310 int in_ctr_cmp;
8311 int out_ctr_cmp;
8312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8314 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008315 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008316 {
8317 return( 0 );
8318 }
8319
Andres AG2196c7f2016-12-15 17:01:16 +00008320 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8321 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008322 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008323 ssl->conf->renego_period + ep_len, 8 - ep_len );
8324
8325 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008326 {
8327 return( 0 );
8328 }
8329
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008334
8335/*
8336 * Receive application data decrypted from the SSL layer
8337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008339{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008340 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008341 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008342
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008343 if( ssl == NULL || ssl->conf == NULL )
8344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008352 return( ret );
8353
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008354 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008356 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008357 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008358 return( ret );
8359 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008360 }
8361#endif
8362
Hanno Becker4a810fb2017-05-24 16:27:30 +01008363 /*
8364 * Check if renegotiation is necessary and/or handshake is
8365 * in process. If yes, perform/continue, and fall through
8366 * if an unexpected packet is received while the client
8367 * is waiting for the ServerHello.
8368 *
8369 * (There is no equivalent to the last condition on
8370 * the server-side as it is not treated as within
8371 * a handshake while waiting for the ClientHello
8372 * after a renegotiation request.)
8373 */
8374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008376 ret = ssl_check_ctr_renegotiate( ssl );
8377 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8378 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008381 return( ret );
8382 }
8383#endif
8384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008388 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8389 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008392 return( ret );
8393 }
8394 }
8395
Hanno Beckere41158b2017-10-23 13:30:32 +01008396 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008397 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008398 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008399 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008400 if( ssl->f_get_timer != NULL &&
8401 ssl->f_get_timer( ssl->p_timer ) == -1 )
8402 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008403 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008404 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008405
Hanno Becker327c93b2018-08-15 13:56:18 +01008406 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008407 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008408 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8409 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008410
Hanno Becker4a810fb2017-05-24 16:27:30 +01008411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8412 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008413 }
8414
8415 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008417 {
8418 /*
8419 * OpenSSL sends empty messages to randomize the IV
8420 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008421 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008424 return( 0 );
8425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008427 return( ret );
8428 }
8429 }
8430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008434
Hanno Becker4a810fb2017-05-24 16:27:30 +01008435 /*
8436 * - For client-side, expect SERVER_HELLO_REQUEST.
8437 * - For server-side, expect CLIENT_HELLO.
8438 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8439 */
8440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008442 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008444 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008447
8448 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008451 {
8452 continue;
8453 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008454#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008456 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008457#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008458
Hanno Becker4a810fb2017-05-24 16:27:30 +01008459#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008460 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008464
8465 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008468 {
8469 continue;
8470 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008471#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008473 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008474#endif /* MBEDTLS_SSL_SRV_C */
8475
Hanno Becker21df7f92017-10-17 11:03:26 +01008476#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008477 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008478 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8479 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8480 ssl->conf->allow_legacy_renegotiation ==
8481 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8482 {
8483 /*
8484 * Accept renegotiation request
8485 */
Paul Bakker48916f92012-09-16 19:57:18 +00008486
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008487 /* DTLS clients need to know renego is server-initiated */
8488#if defined(MBEDTLS_SSL_PROTO_DTLS)
8489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8490 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8491 {
8492 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8493 }
8494#endif
8495 ret = ssl_start_renegotiation( ssl );
8496 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8497 ret != 0 )
8498 {
8499 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8500 return( ret );
8501 }
8502 }
8503 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008504#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008505 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008506 /*
8507 * Refuse renegotiation
8508 */
8509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512#if defined(MBEDTLS_SSL_PROTO_SSL3)
8513 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008514 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008515 /* SSLv3 does not have a "no_renegotiation" warning, so
8516 we send a fatal alert and abort the connection. */
8517 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8518 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8519 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008520 }
8521 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008522#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8523#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8524 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8525 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8528 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8529 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008530 {
8531 return( ret );
8532 }
Paul Bakker48916f92012-09-16 19:57:18 +00008533 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008534 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8536 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008540 }
Paul Bakker48916f92012-09-16 19:57:18 +00008541 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008542
Hanno Becker90333da2017-10-10 11:27:13 +01008543 /* At this point, we don't know whether the renegotiation has been
8544 * completed or not. The cases to consider are the following:
8545 * 1) The renegotiation is complete. In this case, no new record
8546 * has been read yet.
8547 * 2) The renegotiation is incomplete because the client received
8548 * an application data record while awaiting the ServerHello.
8549 * 3) The renegotiation is incomplete because the client received
8550 * a non-handshake, non-application data message while awaiting
8551 * the ServerHello.
8552 * In each of these case, looping will be the proper action:
8553 * - For 1), the next iteration will read a new record and check
8554 * if it's application data.
8555 * - For 2), the loop condition isn't satisfied as application data
8556 * is present, hence continue is the same as break
8557 * - For 3), the loop condition is satisfied and read_record
8558 * will re-deliver the message that was held back by the client
8559 * when expecting the ServerHello.
8560 */
8561 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008562 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008563#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008565 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008566 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008567 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008568 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008571 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008573 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008574 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8579 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008582 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008583 }
8584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8588 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008589 }
8590
8591 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008592
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008593 /* We're going to return something now, cancel timer,
8594 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008596 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008597
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008598#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008599 /* If we requested renego but received AppData, resend HelloRequest.
8600 * Do it now, after setting in_offt, to avoid taking this branch
8601 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008603 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008605 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008606 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008609 return( ret );
8610 }
8611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008613#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008614 }
8615
8616 n = ( len < ssl->in_msglen )
8617 ? len : ssl->in_msglen;
8618
8619 memcpy( buf, ssl->in_offt, n );
8620 ssl->in_msglen -= n;
8621
8622 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008623 {
8624 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008625 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008626 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008628 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008629 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008630 /* more data available */
8631 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008635
Paul Bakker23986e52011-04-24 08:57:21 +00008636 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008637}
8638
8639/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008640 * Send application data to be encrypted by the SSL layer, taking care of max
8641 * fragment length and buffer size.
8642 *
8643 * According to RFC 5246 Section 6.2.1:
8644 *
8645 * Zero-length fragments of Application data MAY be sent as they are
8646 * potentially useful as a traffic analysis countermeasure.
8647 *
8648 * Therefore, it is possible that the input message length is 0 and the
8649 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008650 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008651static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008652 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008653{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008654 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8655 const size_t max_len = (size_t) ret;
8656
8657 if( ret < 0 )
8658 {
8659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8660 return( ret );
8661 }
8662
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008663 if( len > max_len )
8664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008666 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008669 "maximum fragment length: %d > %d",
8670 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008672 }
8673 else
8674#endif
8675 len = max_len;
8676 }
Paul Bakker887bd502011-06-08 13:10:54 +00008677
Paul Bakker5121ce52009-01-03 21:22:43 +00008678 if( ssl->out_left != 0 )
8679 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008680 /*
8681 * The user has previously tried to send the data and
8682 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8683 * written. In this case, we expect the high-level write function
8684 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008689 return( ret );
8690 }
8691 }
Paul Bakker887bd502011-06-08 13:10:54 +00008692 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008693 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008694 /*
8695 * The user is trying to send a message the first time, so we need to
8696 * copy the data into the internal buffers and setup the data structure
8697 * to keep track of partial writes
8698 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008699 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008701 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008702
Hanno Becker67bc7c32018-08-06 11:33:50 +01008703 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008706 return( ret );
8707 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008708 }
8709
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008710 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008711}
8712
8713/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008714 * Write application data, doing 1/n-1 splitting if necessary.
8715 *
8716 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008717 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008718 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008721static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008722 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008723{
8724 int ret;
8725
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008726 if( ssl->conf->cbc_record_splitting ==
8727 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008728 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8730 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8731 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008732 {
8733 return( ssl_write_real( ssl, buf, len ) );
8734 }
8735
8736 if( ssl->split_done == 0 )
8737 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008738 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008739 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008740 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008741 }
8742
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008743 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8744 return( ret );
8745 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008746
8747 return( ret + 1 );
8748}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008750
8751/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008752 * Write application data (public-facing wrapper)
8753 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008754int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008755{
8756 int ret;
8757
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008759
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008760 if( ssl == NULL || ssl->conf == NULL )
8761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8762
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008763#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008764 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8765 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008766 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008767 return( ret );
8768 }
8769#endif
8770
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008771 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008772 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008773 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008774 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008776 return( ret );
8777 }
8778 }
8779
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008780#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008781 ret = ssl_write_split( ssl, buf, len );
8782#else
8783 ret = ssl_write_real( ssl, buf, len );
8784#endif
8785
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008787
8788 return( ret );
8789}
8790
8791/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008792 * Notify the peer that the connection is being closed
8793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008795{
8796 int ret;
8797
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008798 if( ssl == NULL || ssl->conf == NULL )
8799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008802
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008803 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8809 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8810 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008813 return( ret );
8814 }
8815 }
8816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008818
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008819 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008820}
8821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008823{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008824 if( transform == NULL )
8825 return;
8826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008828 deflateEnd( &transform->ctx_deflate );
8829 inflateEnd( &transform->ctx_inflate );
8830#endif
8831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8833 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 mbedtls_md_free( &transform->md_ctx_enc );
8836 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008837
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008838 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008839}
8840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841#if defined(MBEDTLS_X509_CRT_PARSE_C)
8842static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008845
8846 while( cur != NULL )
8847 {
8848 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008850 cur = next;
8851 }
8852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008854
Hanno Becker0271f962018-08-16 13:23:47 +01008855#if defined(MBEDTLS_SSL_PROTO_DTLS)
8856
8857static void ssl_buffering_free( mbedtls_ssl_context *ssl )
8858{
8859 unsigned offset;
8860 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8861
8862 if( hs == NULL )
8863 return;
8864
Hanno Becker283f5ef2018-08-24 09:34:47 +01008865 ssl_free_buffered_record( ssl );
8866
Hanno Becker0271f962018-08-16 13:23:47 +01008867 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01008868 ssl_buffering_free_slot( ssl, offset );
8869}
8870
8871static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
8872 uint8_t slot )
8873{
8874 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
8875 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01008876
8877 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
8878 return;
8879
Hanno Beckere605b192018-08-21 15:59:07 +01008880 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01008881 {
Hanno Beckere605b192018-08-21 15:59:07 +01008882 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01008883 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01008884 mbedtls_free( hs_buf->data );
8885 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01008886 }
8887}
8888
8889#endif /* MBEDTLS_SSL_PROTO_DTLS */
8890
Gilles Peskine9b562d52018-04-25 20:32:43 +02008891void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008892{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008893 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8894
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008895 if( handshake == NULL )
8896 return;
8897
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008898#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8899 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8900 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008901 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008902 handshake->async_in_progress = 0;
8903 }
8904#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8905
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008906#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8907 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8908 mbedtls_md5_free( &handshake->fin_md5 );
8909 mbedtls_sha1_free( &handshake->fin_sha1 );
8910#endif
8911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8912#if defined(MBEDTLS_SHA256_C)
8913 mbedtls_sha256_free( &handshake->fin_sha256 );
8914#endif
8915#if defined(MBEDTLS_SHA512_C)
8916 mbedtls_sha512_free( &handshake->fin_sha512 );
8917#endif
8918#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920#if defined(MBEDTLS_DHM_C)
8921 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008922#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923#if defined(MBEDTLS_ECDH_C)
8924 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008925#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008926#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008927 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008928#if defined(MBEDTLS_SSL_CLI_C)
8929 mbedtls_free( handshake->ecjpake_cache );
8930 handshake->ecjpake_cache = NULL;
8931 handshake->ecjpake_cache_len = 0;
8932#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008933#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008934
Janos Follath4ae5c292016-02-10 11:27:43 +00008935#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8936 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008937 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008939#endif
8940
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008941#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8942 if( handshake->psk != NULL )
8943 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008944 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008945 mbedtls_free( handshake->psk );
8946 }
8947#endif
8948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8950 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008951 /*
8952 * Free only the linked list wrapper, not the keys themselves
8953 * since the belong to the SNI callback
8954 */
8955 if( handshake->sni_key_cert != NULL )
8956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008958
8959 while( cur != NULL )
8960 {
8961 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008963 cur = next;
8964 }
8965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008967
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008968#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008969 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008970#endif
8971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972#if defined(MBEDTLS_SSL_PROTO_DTLS)
8973 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008974 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01008975 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008976#endif
8977
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008978 mbedtls_platform_zeroize( handshake,
8979 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008980}
8981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008983{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008984 if( session == NULL )
8985 return;
8986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008988 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 mbedtls_x509_crt_free( session->peer_cert );
8991 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008992 }
Paul Bakkered27a042013-04-18 22:46:23 +02008993#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008994
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008995#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008997#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008998
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008999 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009000}
9001
Paul Bakker5121ce52009-01-03 21:22:43 +00009002/*
9003 * Free an SSL context
9004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009006{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009007 if( ssl == NULL )
9008 return;
9009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009011
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009012 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009013 {
Angus Grattond8213d02016-05-25 20:56:48 +10009014 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009016 }
9017
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009018 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009019 {
Angus Grattond8213d02016-05-25 20:56:48 +10009020 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009022 }
9023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009025 if( ssl->compress_buf != NULL )
9026 {
Angus Grattond8213d02016-05-25 20:56:48 +10009027 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009029 }
9030#endif
9031
Paul Bakker48916f92012-09-16 19:57:18 +00009032 if( ssl->transform )
9033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 mbedtls_ssl_transform_free( ssl->transform );
9035 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009036 }
9037
9038 if( ssl->handshake )
9039 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009040 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9042 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 mbedtls_free( ssl->handshake );
9045 mbedtls_free( ssl->transform_negotiate );
9046 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009047 }
9048
Paul Bakkerc0463502013-02-14 11:19:38 +01009049 if( ssl->session )
9050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051 mbedtls_ssl_session_free( ssl->session );
9052 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009053 }
9054
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009055#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009056 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009057 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009058 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009060 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9064 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9067 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009068 }
9069#endif
9070
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009071#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009073#endif
9074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009076
Paul Bakker86f04f42013-02-14 11:20:09 +01009077 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009078 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009079}
9080
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009081/*
9082 * Initialze mbedtls_ssl_config
9083 */
9084void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9085{
9086 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9087}
9088
Simon Butcherc97b6972015-12-27 23:48:17 +00009089#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009090static int ssl_preset_default_hashes[] = {
9091#if defined(MBEDTLS_SHA512_C)
9092 MBEDTLS_MD_SHA512,
9093 MBEDTLS_MD_SHA384,
9094#endif
9095#if defined(MBEDTLS_SHA256_C)
9096 MBEDTLS_MD_SHA256,
9097 MBEDTLS_MD_SHA224,
9098#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009099#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009100 MBEDTLS_MD_SHA1,
9101#endif
9102 MBEDTLS_MD_NONE
9103};
Simon Butcherc97b6972015-12-27 23:48:17 +00009104#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009105
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009106static int ssl_preset_suiteb_ciphersuites[] = {
9107 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9108 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9109 0
9110};
9111
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009112#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009113static int ssl_preset_suiteb_hashes[] = {
9114 MBEDTLS_MD_SHA256,
9115 MBEDTLS_MD_SHA384,
9116 MBEDTLS_MD_NONE
9117};
9118#endif
9119
9120#if defined(MBEDTLS_ECP_C)
9121static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9122 MBEDTLS_ECP_DP_SECP256R1,
9123 MBEDTLS_ECP_DP_SECP384R1,
9124 MBEDTLS_ECP_DP_NONE
9125};
9126#endif
9127
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009128/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009129 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009130 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009131int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009132 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009133{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009134#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009135 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009136#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009137
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009138 /* Use the functions here so that they are covered in tests,
9139 * but otherwise access member directly for efficiency */
9140 mbedtls_ssl_conf_endpoint( conf, endpoint );
9141 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009142
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009143 /*
9144 * Things that are common to all presets
9145 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009146#if defined(MBEDTLS_SSL_CLI_C)
9147 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9148 {
9149 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9150#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9151 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9152#endif
9153 }
9154#endif
9155
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009156#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009157 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009158#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009159
9160#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9161 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9162#endif
9163
9164#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9165 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9166#endif
9167
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009168#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9169 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9170#endif
9171
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009172#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009173 conf->f_cookie_write = ssl_cookie_write_dummy;
9174 conf->f_cookie_check = ssl_cookie_check_dummy;
9175#endif
9176
9177#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9178 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9179#endif
9180
Janos Follath088ce432017-04-10 12:42:31 +01009181#if defined(MBEDTLS_SSL_SRV_C)
9182 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9183#endif
9184
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009185#if defined(MBEDTLS_SSL_PROTO_DTLS)
9186 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9187 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9188#endif
9189
9190#if defined(MBEDTLS_SSL_RENEGOTIATION)
9191 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009192 memset( conf->renego_period, 0x00, 2 );
9193 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009194#endif
9195
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009196#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9197 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9198 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009199 const unsigned char dhm_p[] =
9200 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9201 const unsigned char dhm_g[] =
9202 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9203
Hanno Beckera90658f2017-10-04 15:29:08 +01009204 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9205 dhm_p, sizeof( dhm_p ),
9206 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009207 {
9208 return( ret );
9209 }
9210 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009211#endif
9212
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009213 /*
9214 * Preset-specific defaults
9215 */
9216 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009217 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009218 /*
9219 * NSA Suite B
9220 */
9221 case MBEDTLS_SSL_PRESET_SUITEB:
9222 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9223 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9224 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9225 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9226
9227 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9228 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9229 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9230 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9231 ssl_preset_suiteb_ciphersuites;
9232
9233#if defined(MBEDTLS_X509_CRT_PARSE_C)
9234 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009235#endif
9236
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009237#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009238 conf->sig_hashes = ssl_preset_suiteb_hashes;
9239#endif
9240
9241#if defined(MBEDTLS_ECP_C)
9242 conf->curve_list = ssl_preset_suiteb_curves;
9243#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009244 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009245
9246 /*
9247 * Default
9248 */
9249 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009250 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9251 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9252 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9253 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9254 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9255 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9256 MBEDTLS_SSL_MIN_MINOR_VERSION :
9257 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009258 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9259 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9260
9261#if defined(MBEDTLS_SSL_PROTO_DTLS)
9262 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9263 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9264#endif
9265
9266 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9267 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9268 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9269 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9270 mbedtls_ssl_list_ciphersuites();
9271
9272#if defined(MBEDTLS_X509_CRT_PARSE_C)
9273 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9274#endif
9275
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009276#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009277 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009278#endif
9279
9280#if defined(MBEDTLS_ECP_C)
9281 conf->curve_list = mbedtls_ecp_grp_id_list();
9282#endif
9283
9284#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9285 conf->dhm_min_bitlen = 1024;
9286#endif
9287 }
9288
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009289 return( 0 );
9290}
9291
9292/*
9293 * Free mbedtls_ssl_config
9294 */
9295void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9296{
9297#if defined(MBEDTLS_DHM_C)
9298 mbedtls_mpi_free( &conf->dhm_P );
9299 mbedtls_mpi_free( &conf->dhm_G );
9300#endif
9301
9302#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9303 if( conf->psk != NULL )
9304 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009305 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009306 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009307 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009308 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009309 }
9310
9311 if( conf->psk_identity != NULL )
9312 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009313 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009314 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009315 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009316 conf->psk_identity_len = 0;
9317 }
9318#endif
9319
9320#if defined(MBEDTLS_X509_CRT_PARSE_C)
9321 ssl_key_cert_free( conf->key_cert );
9322#endif
9323
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009324 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009325}
9326
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009327#if defined(MBEDTLS_PK_C) && \
9328 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009329/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#if defined(MBEDTLS_RSA_C)
9335 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9336 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009337#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338#if defined(MBEDTLS_ECDSA_C)
9339 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9340 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009341#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009343}
9344
Hanno Becker7e5437a2017-04-28 17:15:26 +01009345unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9346{
9347 switch( type ) {
9348 case MBEDTLS_PK_RSA:
9349 return( MBEDTLS_SSL_SIG_RSA );
9350 case MBEDTLS_PK_ECDSA:
9351 case MBEDTLS_PK_ECKEY:
9352 return( MBEDTLS_SSL_SIG_ECDSA );
9353 default:
9354 return( MBEDTLS_SSL_SIG_ANON );
9355 }
9356}
9357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009359{
9360 switch( sig )
9361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362#if defined(MBEDTLS_RSA_C)
9363 case MBEDTLS_SSL_SIG_RSA:
9364 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#if defined(MBEDTLS_ECDSA_C)
9367 case MBEDTLS_SSL_SIG_ECDSA:
9368 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009369#endif
9370 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009372 }
9373}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009374#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009375
Hanno Becker7e5437a2017-04-28 17:15:26 +01009376#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9377 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9378
9379/* Find an entry in a signature-hash set matching a given hash algorithm. */
9380mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9381 mbedtls_pk_type_t sig_alg )
9382{
9383 switch( sig_alg )
9384 {
9385 case MBEDTLS_PK_RSA:
9386 return( set->rsa );
9387 case MBEDTLS_PK_ECDSA:
9388 return( set->ecdsa );
9389 default:
9390 return( MBEDTLS_MD_NONE );
9391 }
9392}
9393
9394/* Add a signature-hash-pair to a signature-hash set */
9395void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9396 mbedtls_pk_type_t sig_alg,
9397 mbedtls_md_type_t md_alg )
9398{
9399 switch( sig_alg )
9400 {
9401 case MBEDTLS_PK_RSA:
9402 if( set->rsa == MBEDTLS_MD_NONE )
9403 set->rsa = md_alg;
9404 break;
9405
9406 case MBEDTLS_PK_ECDSA:
9407 if( set->ecdsa == MBEDTLS_MD_NONE )
9408 set->ecdsa = md_alg;
9409 break;
9410
9411 default:
9412 break;
9413 }
9414}
9415
9416/* Allow exactly one hash algorithm for each signature. */
9417void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9418 mbedtls_md_type_t md_alg )
9419{
9420 set->rsa = md_alg;
9421 set->ecdsa = md_alg;
9422}
9423
9424#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9425 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9426
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009427/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009428 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009431{
9432 switch( hash )
9433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434#if defined(MBEDTLS_MD5_C)
9435 case MBEDTLS_SSL_HASH_MD5:
9436 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009437#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438#if defined(MBEDTLS_SHA1_C)
9439 case MBEDTLS_SSL_HASH_SHA1:
9440 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009441#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442#if defined(MBEDTLS_SHA256_C)
9443 case MBEDTLS_SSL_HASH_SHA224:
9444 return( MBEDTLS_MD_SHA224 );
9445 case MBEDTLS_SSL_HASH_SHA256:
9446 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009447#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448#if defined(MBEDTLS_SHA512_C)
9449 case MBEDTLS_SSL_HASH_SHA384:
9450 return( MBEDTLS_MD_SHA384 );
9451 case MBEDTLS_SSL_HASH_SHA512:
9452 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009453#endif
9454 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009456 }
9457}
9458
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009459/*
9460 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9461 */
9462unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9463{
9464 switch( md )
9465 {
9466#if defined(MBEDTLS_MD5_C)
9467 case MBEDTLS_MD_MD5:
9468 return( MBEDTLS_SSL_HASH_MD5 );
9469#endif
9470#if defined(MBEDTLS_SHA1_C)
9471 case MBEDTLS_MD_SHA1:
9472 return( MBEDTLS_SSL_HASH_SHA1 );
9473#endif
9474#if defined(MBEDTLS_SHA256_C)
9475 case MBEDTLS_MD_SHA224:
9476 return( MBEDTLS_SSL_HASH_SHA224 );
9477 case MBEDTLS_MD_SHA256:
9478 return( MBEDTLS_SSL_HASH_SHA256 );
9479#endif
9480#if defined(MBEDTLS_SHA512_C)
9481 case MBEDTLS_MD_SHA384:
9482 return( MBEDTLS_SSL_HASH_SHA384 );
9483 case MBEDTLS_MD_SHA512:
9484 return( MBEDTLS_SSL_HASH_SHA512 );
9485#endif
9486 default:
9487 return( MBEDTLS_SSL_HASH_NONE );
9488 }
9489}
9490
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009491#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009492/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009493 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009494 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009495 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009496int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009499
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009500 if( ssl->conf->curve_list == NULL )
9501 return( -1 );
9502
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009503 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009504 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009505 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009506
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009507 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009508}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009509#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009510
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009511#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009512/*
9513 * Check if a hash proposed by the peer is in our list.
9514 * Return 0 if we're willing to use it, -1 otherwise.
9515 */
9516int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9517 mbedtls_md_type_t md )
9518{
9519 const int *cur;
9520
9521 if( ssl->conf->sig_hashes == NULL )
9522 return( -1 );
9523
9524 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9525 if( *cur == (int) md )
9526 return( 0 );
9527
9528 return( -1 );
9529}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009530#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_X509_CRT_PARSE_C)
9533int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9534 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009535 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009536 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009537{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009538 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009540 int usage = 0;
9541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009543 const char *ext_oid;
9544 size_t ext_len;
9545#endif
9546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9548 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009549 ((void) cert);
9550 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009551 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009552#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9555 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009556 {
9557 /* Server part of the key exchange */
9558 switch( ciphersuite->key_exchange )
9559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 case MBEDTLS_KEY_EXCHANGE_RSA:
9561 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009562 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009563 break;
9564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9566 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9567 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9568 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009569 break;
9570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9572 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009573 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009574 break;
9575
9576 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577 case MBEDTLS_KEY_EXCHANGE_NONE:
9578 case MBEDTLS_KEY_EXCHANGE_PSK:
9579 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9580 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009581 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009582 usage = 0;
9583 }
9584 }
9585 else
9586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9588 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009589 }
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009592 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009593 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009594 ret = -1;
9595 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009596#else
9597 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9601 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9604 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009605 }
9606 else
9607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9609 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009610 }
9611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009613 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009614 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009615 ret = -1;
9616 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009618
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009619 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009622
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009623/*
9624 * Convert version numbers to/from wire format
9625 * and, for DTLS, to/from TLS equivalent.
9626 *
9627 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009628 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009629 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9630 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009633 unsigned char ver[2] )
9634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635#if defined(MBEDTLS_SSL_PROTO_DTLS)
9636 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009639 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9640
9641 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9642 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9643 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009644 else
9645#else
9646 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009647#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009648 {
9649 ver[0] = (unsigned char) major;
9650 ver[1] = (unsigned char) minor;
9651 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009652}
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009655 const unsigned char ver[2] )
9656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657#if defined(MBEDTLS_SSL_PROTO_DTLS)
9658 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009659 {
9660 *major = 255 - ver[0] + 2;
9661 *minor = 255 - ver[1] + 1;
9662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009664 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9665 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009666 else
9667#else
9668 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009669#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009670 {
9671 *major = ver[0];
9672 *minor = ver[1];
9673 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009674}
9675
Simon Butcher99000142016-10-13 17:21:01 +01009676int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9677{
9678#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9679 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9680 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9681
9682 switch( md )
9683 {
9684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9685#if defined(MBEDTLS_MD5_C)
9686 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009687 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009688#endif
9689#if defined(MBEDTLS_SHA1_C)
9690 case MBEDTLS_SSL_HASH_SHA1:
9691 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9692 break;
9693#endif
9694#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9695#if defined(MBEDTLS_SHA512_C)
9696 case MBEDTLS_SSL_HASH_SHA384:
9697 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9698 break;
9699#endif
9700#if defined(MBEDTLS_SHA256_C)
9701 case MBEDTLS_SSL_HASH_SHA256:
9702 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9703 break;
9704#endif
9705 default:
9706 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9707 }
9708
9709 return 0;
9710#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9711 (void) ssl;
9712 (void) md;
9713
9714 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9715#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9716}
9717
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009718#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9719 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9720int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9721 unsigned char *output,
9722 unsigned char *data, size_t data_len )
9723{
9724 int ret = 0;
9725 mbedtls_md5_context mbedtls_md5;
9726 mbedtls_sha1_context mbedtls_sha1;
9727
9728 mbedtls_md5_init( &mbedtls_md5 );
9729 mbedtls_sha1_init( &mbedtls_sha1 );
9730
9731 /*
9732 * digitally-signed struct {
9733 * opaque md5_hash[16];
9734 * opaque sha_hash[20];
9735 * };
9736 *
9737 * md5_hash
9738 * MD5(ClientHello.random + ServerHello.random
9739 * + ServerParams);
9740 * sha_hash
9741 * SHA(ClientHello.random + ServerHello.random
9742 * + ServerParams);
9743 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009744 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009745 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009747 goto exit;
9748 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009749 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009750 ssl->handshake->randbytes, 64 ) ) != 0 )
9751 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009753 goto exit;
9754 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009755 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009756 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009757 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009758 goto exit;
9759 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009760 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009761 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009763 goto exit;
9764 }
9765
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009766 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009767 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009769 goto exit;
9770 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009771 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009772 ssl->handshake->randbytes, 64 ) ) != 0 )
9773 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009775 goto exit;
9776 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009777 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009778 data_len ) ) != 0 )
9779 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009780 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009781 goto exit;
9782 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009783 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009784 output + 16 ) ) != 0 )
9785 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009787 goto exit;
9788 }
9789
9790exit:
9791 mbedtls_md5_free( &mbedtls_md5 );
9792 mbedtls_sha1_free( &mbedtls_sha1 );
9793
9794 if( ret != 0 )
9795 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9796 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9797
9798 return( ret );
9799
9800}
9801#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9802 MBEDTLS_SSL_PROTO_TLS1_1 */
9803
9804#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9805 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9806int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009807 unsigned char *hash, size_t *hashlen,
9808 unsigned char *data, size_t data_len,
9809 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009810{
9811 int ret = 0;
9812 mbedtls_md_context_t ctx;
9813 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009814 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009815
9816 mbedtls_md_init( &ctx );
9817
9818 /*
9819 * digitally-signed struct {
9820 * opaque client_random[32];
9821 * opaque server_random[32];
9822 * ServerDHParams params;
9823 * };
9824 */
9825 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9826 {
9827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9828 goto exit;
9829 }
9830 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9831 {
9832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9833 goto exit;
9834 }
9835 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9836 {
9837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9838 goto exit;
9839 }
9840 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9841 {
9842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9843 goto exit;
9844 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009845 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009846 {
9847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9848 goto exit;
9849 }
9850
9851exit:
9852 mbedtls_md_free( &ctx );
9853
9854 if( ret != 0 )
9855 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9856 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9857
9858 return( ret );
9859}
9860#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9861 MBEDTLS_SSL_PROTO_TLS1_2 */
9862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863#endif /* MBEDTLS_SSL_TLS_C */