blob: c34ab624b36be65b0dfbeac0e3dd83aedd8cd9df [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
Hanno Becker7d0a5692018-10-23 15:26:22 +0100610#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
611 defined(MBEDTLS_USE_PSA_CRYPTO)
612static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
613{
614 if( ssl->conf->f_psk != NULL )
615 {
616 /* If we've used a callback to select the PSK,
617 * the static configuration is irrelevant. */
618 if( ssl->handshake->psk_opaque != 0 )
619 return( 1 );
620
621 return( 0 );
622 }
623
624 if( ssl->conf->psk_opaque != 0 )
625 return( 1 );
626
627 return( 0 );
628}
629#endif /* MBEDTLS_USE_PSA_CRYPTO &&
630 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000633{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200634 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000635#if defined(MBEDTLS_USE_PSA_CRYPTO)
636 int psa_fallthrough;
637#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 unsigned char keyblk[256];
640 unsigned char *key1;
641 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100642 unsigned char *mac_enc;
643 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000644 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200645 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000646 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 const mbedtls_cipher_info_t *cipher_info;
648 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100649
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000650 /* cf. RFC 5246, Section 8.1:
651 * "The master secret is always exactly 48 bytes in length." */
652 size_t const master_secret_len = 48;
653
Hanno Becker35b23c72018-10-23 12:10:41 +0100654#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
655 unsigned char session_hash[48];
656#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_ssl_session *session = ssl->session_negotiate;
659 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
660 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100665 if( cipher_info == NULL )
666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100668 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100670 }
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100673 if( md_info == NULL )
674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100676 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100678 }
679
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000681 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_SSL_PROTO_SSL3)
684 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 {
Paul Bakker48916f92012-09-16 19:57:18 +0000686 handshake->tls_prf = ssl3_prf;
687 handshake->calc_verify = ssl_calc_verify_ssl;
688 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200690 else
691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
693 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000694 {
Paul Bakker48916f92012-09-16 19:57:18 +0000695 handshake->tls_prf = tls1_prf;
696 handshake->calc_verify = ssl_calc_verify_tls;
697 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000698 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200699 else
700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
702#if defined(MBEDTLS_SHA512_C)
703 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
704 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000705 {
Paul Bakker48916f92012-09-16 19:57:18 +0000706 handshake->tls_prf = tls_prf_sha384;
707 handshake->calc_verify = ssl_calc_verify_tls_sha384;
708 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000709 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000710 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_SHA256_C)
713 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000714 {
Paul Bakker48916f92012-09-16 19:57:18 +0000715 handshake->tls_prf = tls_prf_sha256;
716 handshake->calc_verify = ssl_calc_verify_tls_sha256;
717 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200719 else
720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200725 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000726
727 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000728 * SSLv3:
729 * master =
730 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
731 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
732 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200733 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200734 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 * master = PRF( premaster, "master secret", randbytes )[0..47]
736 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100737 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
740 }
741 else
742 {
743 /* The label for the KDF used for key expansion.
744 * This is either "master secret" or "extended master secret"
745 * depending on whether the Extended Master Secret extension
746 * is used. */
747 char const *lbl = "master secret";
748
749 /* The salt for the KDF used for key expansion.
750 * - If the Extended Master Secret extension is not used,
751 * this is ClientHello.Random + ServerHello.Random
752 * (see Sect. 8.1 in RFC 5246).
753 * - If the Extended Master Secret extension is used,
754 * this is the transcript of the handshake so far.
755 * (see Sect. 4 in RFC 7627). */
756 unsigned char const *salt = handshake->randbytes;
757 size_t salt_len = 64;
758
759#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
760 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
761 ssl->transform_negotiate->ciphersuite_info;
762 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
763#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
766 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200769
Hanno Becker35b23c72018-10-23 12:10:41 +0100770 lbl = "extended master secret";
771 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200772 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
774 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100777 if( md_type == MBEDTLS_MD_SHA384 )
778 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200779 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100780#endif /* MBEDTLS_SHA512_C */
781 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200782 }
783 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100785 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200786
Hanno Becker35b23c72018-10-23 12:10:41 +0100787 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200788 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100789#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
790
Hanno Becker7d0a5692018-10-23 15:26:22 +0100791#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
792 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
793 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
794 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
795 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100796 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100797 /* Perform PSK-to-MS expansion in a single step. */
798 psa_status_t status;
799 psa_algorithm_t alg;
800 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500801 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100802
803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
804
805 psk = ssl->conf->psk_opaque;
806 if( ssl->handshake->psk_opaque != 0 )
807 psk = ssl->handshake->psk_opaque;
808
809 if( md_type == MBEDTLS_MD_SHA384 )
810 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
811 else
812 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
813
814 status = psa_key_derivation( &generator, psk, alg,
815 salt, salt_len,
816 (unsigned char const *) lbl,
817 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000818 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100819 if( status != PSA_SUCCESS )
820 {
821 psa_generator_abort( &generator );
822 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
823 }
824
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000825 status = psa_generator_read( &generator, session->master,
826 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100827 if( status != PSA_SUCCESS )
828 {
829 psa_generator_abort( &generator );
830 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
831 }
832
833 status = psa_generator_abort( &generator );
834 if( status != PSA_SUCCESS )
835 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100836 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100837 else
838#endif
839 {
840 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
841 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000842 session->master,
843 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100844 if( ret != 0 )
845 {
846 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
847 return( ret );
848 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200849
Hanno Becker7d0a5692018-10-23 15:26:22 +0100850 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
851 handshake->premaster,
852 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100853
Hanno Becker7d0a5692018-10-23 15:26:22 +0100854 mbedtls_platform_zeroize( handshake->premaster,
855 sizeof(handshake->premaster) );
856 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
859 /*
860 * Swap the client and server random values.
861 */
Paul Bakker48916f92012-09-16 19:57:18 +0000862 memcpy( tmp, handshake->randbytes, 64 );
863 memcpy( handshake->randbytes, tmp + 32, 32 );
864 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500865 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
867 /*
868 * SSLv3:
869 * key block =
870 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
871 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
872 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
873 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
874 * ...
875 *
876 * TLSv1:
877 * key block = PRF( master, "key expansion", randbytes )
878 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100879 ret = handshake->tls_prf( session->master, 48, "key expansion",
880 handshake->randbytes, 64, keyblk, 256 );
881 if( ret != 0 )
882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100884 return( ret );
885 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
888 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
889 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
890 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
891 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500893 mbedtls_platform_zeroize( handshake->randbytes,
894 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
896 /*
897 * Determine the appropriate key, IV and MAC length.
898 */
Paul Bakker68884e32013-01-07 18:20:04 +0100899
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200900 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200903 cipher_info->mode == MBEDTLS_MODE_CCM ||
904 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000906 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200907
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200908 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000909 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200910
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200911 /* All modes haves 96-bit IVs;
912 * GCM and CCM has 4 implicit and 8 explicit bytes
913 * ChachaPoly has all 12 bytes implicit
914 */
Paul Bakker68884e32013-01-07 18:20:04 +0100915 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200916 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
917 transform->fixed_ivlen = 12;
918 else
919 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200920
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200921 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
922 taglen = transform->ciphersuite_info->flags &
923 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
924
925
926 /* Minimum length of encrypted record */
927 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
928 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100929 }
930 else
931 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200932 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
934 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200937 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100938 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200940 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000941 mac_key_len = mbedtls_md_get_size( md_info );
942 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200945 /*
946 * If HMAC is to be truncated, we shall keep the leftmost bytes,
947 * (rfc 6066 page 13 or rfc 2104 section 4),
948 * so we only need to adjust the length here.
949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000953
954#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
955 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000956 * HMAC implementation which also truncates the key
957 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000958 mac_key_len = transform->maclen;
959#endif
960 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200962
963 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100964 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200966 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200968 transform->minlen = transform->maclen;
969 else
Paul Bakker68884e32013-01-07 18:20:04 +0100970 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200971 /*
972 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100973 * 1. if EtM is in use: one block plus MAC
974 * otherwise: * first multiple of blocklen greater than maclen
975 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
978 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100979 {
980 transform->minlen = transform->maclen
981 + cipher_info->block_size;
982 }
983 else
984#endif
985 {
986 transform->minlen = transform->maclen
987 + cipher_info->block_size
988 - transform->maclen % cipher_info->block_size;
989 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
992 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
993 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200994 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100995 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200996#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
998 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
999 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001000 {
1001 transform->minlen += transform->ivlen;
1002 }
1003 else
1004#endif
1005 {
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 );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001008 }
Paul Bakker68884e32013-01-07 18:20:04 +01001009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 }
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001013 transform->keylen, transform->minlen, transform->ivlen,
1014 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
1016 /*
1017 * Finally setup the cipher contexts, IVs and MAC secrets.
1018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001020 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001022 key1 = keyblk + mac_key_len * 2;
1023 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakker68884e32013-01-07 18:20:04 +01001025 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001026 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001028 /*
1029 * This is not used in TLS v1.1.
1030 */
Paul Bakker48916f92012-09-16 19:57:18 +00001031 iv_copy_len = ( transform->fixed_ivlen ) ?
1032 transform->fixed_ivlen : transform->ivlen;
1033 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1034 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001035 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 }
1037 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#endif /* MBEDTLS_SSL_CLI_C */
1039#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001040 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001042 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1043 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Hanno Becker81c7b182017-11-09 18:39:33 +00001045 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001046 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001048 /*
1049 * This is not used in TLS v1.1.
1050 */
Paul Bakker48916f92012-09-16 19:57:18 +00001051 iv_copy_len = ( transform->fixed_ivlen ) ?
1052 transform->fixed_ivlen : transform->ivlen;
1053 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1054 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001055 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001062 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_SSL_PROTO_SSL3)
1065 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001066 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001067 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1070 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001071 }
1072
Hanno Becker81c7b182017-11-09 18:39:33 +00001073 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1074 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001075 }
1076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1078#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1079 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1080 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001081 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001082 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1083 For AEAD-based ciphersuites, there is nothing to do here. */
1084 if( mac_key_len != 0 )
1085 {
1086 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1087 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1088 }
Paul Bakker68884e32013-01-07 18:20:04 +01001089 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001090 else
1091#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001095 }
Paul Bakker68884e32013-01-07 18:20:04 +01001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1098 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001099 {
1100 int ret = 0;
1101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001105 transform->iv_enc, transform->iv_dec,
1106 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001107 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001108 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1111 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001112 }
1113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001115
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001116#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1117 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001118 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001119 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1120 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001121 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001122 iv_copy_len );
1123 }
1124#endif
1125
Hanno Beckerf704bef2018-11-16 15:21:18 +00001126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001127
1128 /* Only use PSA-based ciphers for TLS-1.2.
1129 * That's relevant at least for TLS-1.0, where
1130 * we assume that mbedtls_cipher_crypt() updates
1131 * the structure field for the IV, which the PSA-based
1132 * implementation currently doesn't. */
1133#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1134 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001135 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001136 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1137 cipher_info, taglen );
1138 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1139 {
1140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1141 return( ret );
1142 }
1143
1144 if( ret == 0 )
1145 {
1146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1147 psa_fallthrough = 0;
1148 }
1149 else
1150 {
1151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1152 psa_fallthrough = 1;
1153 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001154 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001155 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001156 psa_fallthrough = 1;
1157#else
1158 psa_fallthrough = 1;
1159#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001160
Hanno Beckercb1cc802018-11-17 22:27:38 +00001161 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001162#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001163 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001164 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001167 return( ret );
1168 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001169
Hanno Beckerf704bef2018-11-16 15:21:18 +00001170#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001171 /* Only use PSA-based ciphers for TLS-1.2.
1172 * That's relevant at least for TLS-1.0, where
1173 * we assume that mbedtls_cipher_crypt() updates
1174 * the structure field for the IV, which the PSA-based
1175 * implementation currently doesn't. */
1176#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1177 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001178 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001179 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1180 cipher_info, taglen );
1181 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1182 {
1183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1184 return( ret );
1185 }
1186
1187 if( ret == 0 )
1188 {
1189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1190 psa_fallthrough = 0;
1191 }
1192 else
1193 {
1194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1195 psa_fallthrough = 1;
1196 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001197 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001198 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001199 psa_fallthrough = 1;
1200#else
1201 psa_fallthrough = 1;
1202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001203
Hanno Beckercb1cc802018-11-17 22:27:38 +00001204 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001206 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001207 cipher_info ) ) != 0 )
1208 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001210 return( ret );
1211 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001214 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001218 return( ret );
1219 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001222 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001226 return( ret );
1227 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_CIPHER_MODE_CBC)
1230 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1233 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001236 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001237 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1240 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001243 return( ret );
1244 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001248 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001251 // Initialize compression
1252 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001254 {
Paul Bakker16770332013-10-11 09:59:44 +02001255 if( ssl->compress_buf == NULL )
1256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001258 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001259 if( ssl->compress_buf == NULL )
1260 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001262 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001263 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001264 }
1265 }
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001268
Paul Bakker48916f92012-09-16 19:57:18 +00001269 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1270 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001271
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001272 if( deflateInit( &transform->ctx_deflate,
1273 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001274 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1277 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001278 }
1279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 return( 0 );
1285}
1286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_SSL_PROTO_SSL3)
1288void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001289{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001290 mbedtls_md5_context md5;
1291 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 unsigned char pad_1[48];
1293 unsigned char pad_2[48];
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001297 mbedtls_md5_init( &md5 );
1298 mbedtls_sha1_init( &sha1 );
1299
1300 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1301 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302
Paul Bakker380da532012-04-18 16:10:25 +00001303 memset( pad_1, 0x36, 48 );
1304 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001306 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1307 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1308 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001310 mbedtls_md5_starts_ret( &md5 );
1311 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1312 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1313 mbedtls_md5_update_ret( &md5, hash, 16 );
1314 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001316 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1317 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1318 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001319
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001320 mbedtls_sha1_starts_ret( &sha1 );
1321 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1322 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1323 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1324 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001328
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001329 mbedtls_md5_free( &md5 );
1330 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001331
Paul Bakker380da532012-04-18 16:10:25 +00001332 return;
1333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1337void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001338{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001339 mbedtls_md5_context md5;
1340 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001343
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001344 mbedtls_md5_init( &md5 );
1345 mbedtls_sha1_init( &sha1 );
1346
1347 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1348 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001349
Andrzej Kurekeb342242019-01-29 09:14:33 -05001350 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001351 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001355
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001356 mbedtls_md5_free( &md5 );
1357 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001358
Paul Bakker380da532012-04-18 16:10:25 +00001359 return;
1360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1364#if defined(MBEDTLS_SHA256_C)
1365void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001366{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001367#if defined(MBEDTLS_USE_PSA_CRYPTO)
1368 size_t hash_size;
1369 psa_status_t status;
1370 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1371
1372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1373 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1374 if( status != PSA_SUCCESS )
1375 {
1376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1377 return;
1378 }
1379
1380 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1381 if( status != PSA_SUCCESS )
1382 {
1383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1384 return;
1385 }
1386 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1388#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001389 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001390
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001391 mbedtls_sha256_init( &sha256 );
1392
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001394
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001395 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001396 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001400
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001401 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001402#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001403 return;
1404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_SHA512_C)
1408void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001409{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001410#if defined(MBEDTLS_USE_PSA_CRYPTO)
1411 size_t hash_size;
1412 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001413 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001414
1415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001416 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001417 if( status != PSA_SUCCESS )
1418 {
1419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1420 return;
1421 }
1422
Andrzej Kurek972fba52019-01-30 03:29:12 -05001423 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001424 if( status != PSA_SUCCESS )
1425 {
1426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1427 return;
1428 }
1429 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1431#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001432 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001433
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001434 mbedtls_sha512_init( &sha512 );
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001438 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001439 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001444 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001445#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 return;
1447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_SHA512_C */
1449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1452int 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 +02001453{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001454 unsigned char *p = ssl->handshake->premaster;
1455 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001456 const unsigned char *psk = ssl->conf->psk;
1457 size_t psk_len = ssl->conf->psk_len;
1458
1459 /* If the psk callback was called, use its result */
1460 if( ssl->handshake->psk != NULL )
1461 {
1462 psk = ssl->handshake->psk;
1463 psk_len = ssl->handshake->psk_len;
1464 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001465
1466 /*
1467 * PMS = struct {
1468 * opaque other_secret<0..2^16-1>;
1469 * opaque psk<0..2^16-1>;
1470 * };
1471 * with "other_secret" depending on the particular key exchange
1472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1474 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001475 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001476 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001478
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001479 *(p++) = (unsigned char)( psk_len >> 8 );
1480 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001481
1482 if( end < p || (size_t)( end - p ) < psk_len )
1483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1484
1485 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001486 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001487 }
1488 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1490#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1491 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001492 {
1493 /*
1494 * other_secret already set by the ClientKeyExchange message,
1495 * and is 48 bytes long
1496 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001497 if( end - p < 2 )
1498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1499
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001500 *p++ = 0;
1501 *p++ = 48;
1502 p += 48;
1503 }
1504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1506#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1507 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001508 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001509 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001510 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001511
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001512 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001514 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001515 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001518 return( ret );
1519 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001520 *(p++) = (unsigned char)( len >> 8 );
1521 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001522 p += len;
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001525 }
1526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1528#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1529 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001530 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001531 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001532 size_t zlen;
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001535 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001536 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001539 return( ret );
1540 }
1541
1542 *(p++) = (unsigned char)( zlen >> 8 );
1543 *(p++) = (unsigned char)( zlen );
1544 p += zlen;
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547 }
1548 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001553 }
1554
1555 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001556 if( end - p < 2 )
1557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001558
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001559 *(p++) = (unsigned char)( psk_len >> 8 );
1560 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001561
1562 if( end < p || (size_t)( end - p ) < psk_len )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001565 memcpy( p, psk, psk_len );
1566 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567
1568 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1569
1570 return( 0 );
1571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001575/*
1576 * SSLv3.0 MAC functions
1577 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001578#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001579static void ssl_mac( mbedtls_md_context_t *md_ctx,
1580 const unsigned char *secret,
1581 const unsigned char *buf, size_t len,
1582 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001583 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001584{
1585 unsigned char header[11];
1586 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001587 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1589 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001590
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001591 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001593 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001594 else
Paul Bakker68884e32013-01-07 18:20:04 +01001595 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001596
1597 memcpy( header, ctr, 8 );
1598 header[ 8] = (unsigned char) type;
1599 header[ 9] = (unsigned char)( len >> 8 );
1600 header[10] = (unsigned char)( len );
1601
Paul Bakker68884e32013-01-07 18:20:04 +01001602 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 mbedtls_md_starts( md_ctx );
1604 mbedtls_md_update( md_ctx, secret, md_size );
1605 mbedtls_md_update( md_ctx, padding, padlen );
1606 mbedtls_md_update( md_ctx, header, 11 );
1607 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001608 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Paul Bakker68884e32013-01-07 18:20:04 +01001610 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 mbedtls_md_starts( md_ctx );
1612 mbedtls_md_update( md_ctx, secret, md_size );
1613 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001614 mbedtls_md_update( md_ctx, out, md_size );
1615 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1620 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001621 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001622#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001623#endif
1624
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001625/* The function below is only used in the Lucky 13 counter-measure in
1626 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1627#if defined(SSL_SOME_MODES_USE_MAC) && \
1628 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1629 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1630 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1631/* This function makes sure every byte in the memory region is accessed
1632 * (in ascending addresses order) */
1633static void ssl_read_memory( unsigned char *p, size_t len )
1634{
1635 unsigned char acc = 0;
1636 volatile unsigned char force;
1637
1638 for( ; len != 0; p++, len-- )
1639 acc ^= *p;
1640
1641 force = acc;
1642 (void) force;
1643}
1644#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1645
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001646/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001647 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001652 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001656 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001660 }
1661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001665 ssl->out_msg, ssl->out_msglen );
1666
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001668 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001670#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 if( mode == MBEDTLS_MODE_STREAM ||
1672 ( mode == MBEDTLS_MODE_CBC
1673#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1674 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001675#endif
1676 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_SSL_PROTO_SSL3)
1679 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001680 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001681 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001682
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683 ssl_mac( &ssl->transform_out->md_ctx_enc,
1684 ssl->transform_out->mac_enc,
1685 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001686 ssl->out_ctr, ssl->out_msgtype,
1687 mac );
1688
1689 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001690 }
1691 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001692#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1694 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001696 {
Hanno Becker992b6872017-11-09 18:57:39 +00001697 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1700 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1701 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1702 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001703 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001704 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001706
1707 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001708 }
1709 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001710#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001714 }
1715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 ssl->out_msg + ssl->out_msglen,
1718 ssl->transform_out->maclen );
1719
1720 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001721 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001722 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001723#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 /*
1726 * Encrypt
1727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1729 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001731 int ret;
1732 size_t olen = 0;
1733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 "including %d bytes of padding",
1736 ssl->out_msglen, 0 ) );
1737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001739 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001740 ssl->transform_out->ivlen,
1741 ssl->out_msg, ssl->out_msglen,
1742 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001745 return( ret );
1746 }
1747
1748 if( ssl->out_msglen != olen )
1749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1751 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001752 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 }
Paul Bakker68884e32013-01-07 18:20:04 +01001754 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001756#if defined(MBEDTLS_GCM_C) || \
1757 defined(MBEDTLS_CCM_C) || \
1758 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001760 mode == MBEDTLS_MODE_CCM ||
1761 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001762 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001763 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001764 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001765 unsigned char *enc_msg;
1766 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001767 unsigned char iv[12];
1768 mbedtls_ssl_transform *transform = ssl->transform_out;
1769 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001771 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001772
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001773 /*
1774 * Prepare additional authenticated data
1775 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001776 memcpy( add_data, ssl->out_ctr, 8 );
1777 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001779 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001780 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1781 add_data[12] = ssl->out_msglen & 0xFF;
1782
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001783 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001784
Paul Bakker68884e32013-01-07 18:20:04 +01001785 /*
1786 * Generate IV
1787 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001788 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1789 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001790 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001791 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1792 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1793 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1794
1795 }
1796 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1797 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001798 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001799 unsigned char i;
1800
1801 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1802
1803 for( i = 0; i < 8; i++ )
1804 iv[i+4] ^= ssl->out_ctr[i];
1805 }
1806 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001807 {
1808 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001811 }
1812
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001813 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1814 iv, transform->ivlen );
1815 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1816 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001817
Paul Bakker68884e32013-01-07 18:20:04 +01001818 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001819 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001820 */
1821 enc_msg = ssl->out_msg;
1822 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001823 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001826 "including 0 bytes of padding",
1827 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001828
Paul Bakker68884e32013-01-07 18:20:04 +01001829 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001830 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001831 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001832 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1833 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001834 add_data, 13,
1835 enc_msg, enc_msglen,
1836 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001837 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001840 return( ret );
1841 }
1842
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001843 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001847 }
1848
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001849 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001850 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1856#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001857 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001860 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001861 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001862 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001863
Paul Bakker48916f92012-09-16 19:57:18 +00001864 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1865 ssl->transform_out->ivlen;
1866 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 padlen = 0;
1868
1869 for( i = 0; i <= padlen; i++ )
1870 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1871
1872 ssl->out_msglen += padlen + 1;
1873
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001874 enc_msglen = ssl->out_msglen;
1875 enc_msg = ssl->out_msg;
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001878 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001879 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1880 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001883 {
1884 /*
1885 * Generate IV
1886 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001887 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001888 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001889 if( ret != 0 )
1890 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001891
Paul Bakker92be97b2013-01-02 17:30:03 +01001892 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001893 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001894
1895 /*
1896 * Fix pointer positions and message length with added IV
1897 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001898 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001899 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001900 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001901 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001905 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001906 ssl->out_msglen, ssl->transform_out->ivlen,
1907 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001910 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001911 ssl->transform_out->ivlen,
1912 enc_msg, enc_msglen,
1913 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001916 return( ret );
1917 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001918
Paul Bakkercca5b812013-08-31 17:40:26 +02001919 if( enc_msglen != olen )
1920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001923 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1926 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001927 {
1928 /*
1929 * Save IV in SSL3 and TLS1
1930 */
1931 memcpy( ssl->transform_out->iv_enc,
1932 ssl->transform_out->cipher_ctx_enc.iv,
1933 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001935#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001938 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001939 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001940 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1941
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001942 /*
1943 * MAC(MAC_write_key, seq_num +
1944 * TLSCipherText.type +
1945 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001946 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001947 * IV + // except for TLS 1.0
1948 * ENC(content + padding + padding_length));
1949 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001950 unsigned char pseudo_hdr[13];
1951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001953
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001954 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1955 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001956 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1957 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1962 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001963 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001964 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966
Hanno Becker3d8c9072018-01-05 16:24:22 +00001967 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1968 ssl->transform_out->maclen );
1969
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001971 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001975 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001977 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983 /* Make extra sure authentication was performed, exactly once */
1984 if( auth_done != 1 )
1985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001988 }
1989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001991
1992 return( 0 );
1993}
1994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001998 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001999#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002000 size_t padlen = 0, correct = 1;
2001#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002005 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002009 }
2010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002012
Paul Bakker48916f92012-09-16 19:57:18 +00002013 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002016 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 }
2019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2021 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002022 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002023 int ret;
2024 size_t olen = 0;
2025
Paul Bakker68884e32013-01-07 18:20:04 +01002026 padlen = 0;
2027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002029 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002030 ssl->transform_in->ivlen,
2031 ssl->in_msg, ssl->in_msglen,
2032 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002035 return( ret );
2036 }
2037
2038 if( ssl->in_msglen != olen )
2039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2041 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
Paul Bakker68884e32013-01-07 18:20:04 +01002044 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002046#if defined(MBEDTLS_GCM_C) || \
2047 defined(MBEDTLS_CCM_C) || \
2048 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002050 mode == MBEDTLS_MODE_CCM ||
2051 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002052 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002053 int ret;
2054 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002055 unsigned char *dec_msg;
2056 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002057 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 unsigned char iv[12];
2059 mbedtls_ssl_transform *transform = ssl->transform_in;
2060 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002062 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002063
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 /*
2065 * Compute and update sizes
2066 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002067 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002070 "+ taglen (%d)", ssl->in_msglen,
2071 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002073 }
2074 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2075
Paul Bakker68884e32013-01-07 18:20:04 +01002076 dec_msg = ssl->in_msg;
2077 dec_msg_result = ssl->in_msg;
2078 ssl->in_msglen = dec_msglen;
2079
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002080 /*
2081 * Prepare additional authenticated data
2082 */
Paul Bakker68884e32013-01-07 18:20:04 +01002083 memcpy( add_data, ssl->in_ctr, 8 );
2084 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002086 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002087 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2088 add_data[12] = ssl->in_msglen & 0xFF;
2089
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002091
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002092 /*
2093 * Prepare IV
2094 */
2095 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2096 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002097 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002098 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2099 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002100
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002101 }
2102 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2103 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002104 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002105 unsigned char i;
2106
2107 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2108
2109 for( i = 0; i < 8; i++ )
2110 iv[i+4] ^= ssl->in_ctr[i];
2111 }
2112 else
2113 {
2114 /* Reminder if we ever add an AEAD mode with a different size */
2115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2117 }
2118
2119 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002121
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002122 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002123 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002126 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002127 add_data, 13,
2128 dec_msg, dec_msglen,
2129 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002130 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2135 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002136
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002137 return( ret );
2138 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002139 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002140
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002141 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2144 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002145 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002146 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2149#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002150 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 {
Paul Bakker45829992013-01-03 14:52:21 +01002153 /*
2154 * Decrypt and check the padding
2155 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002156 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002157 unsigned char *dec_msg;
2158 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002159 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002160 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002161 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002162
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 /*
Paul Bakker45829992013-01-03 14:52:21 +01002164 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2167 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002168 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002169#endif
Paul Bakker45829992013-01-03 14:52:21 +01002170
2171 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2172 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002175 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2176 ssl->transform_in->ivlen,
2177 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002179 }
2180
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002181 dec_msglen = ssl->in_msglen;
2182 dec_msg = ssl->in_msg;
2183 dec_msg_result = ssl->in_msg;
2184
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002185 /*
2186 * Authenticate before decrypt if enabled
2187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2189 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002190 {
Hanno Becker992b6872017-11-09 18:57:39 +00002191 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002192 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002195
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002196 dec_msglen -= ssl->transform_in->maclen;
2197 ssl->in_msglen -= ssl->transform_in->maclen;
2198
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002199 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2200 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2201 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2202 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2207 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002208 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002209 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002213 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002214 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002215 ssl->transform_in->maclen );
2216
Hanno Becker992b6872017-11-09 18:57:39 +00002217 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2218 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002224 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002227
2228 /*
2229 * Check length sanity
2230 */
2231 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002236 }
2237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002239 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002240 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002243 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002244 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002245 dec_msglen -= ssl->transform_in->ivlen;
2246 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002247
Paul Bakker48916f92012-09-16 19:57:18 +00002248 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002249 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002254 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002255 ssl->transform_in->ivlen,
2256 dec_msg, dec_msglen,
2257 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002260 return( ret );
2261 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002262
Paul Bakkercca5b812013-08-31 17:40:26 +02002263 if( dec_msglen != olen )
2264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002267 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2270 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002271 {
2272 /*
2273 * Save IV in SSL3 and TLS1
2274 */
2275 memcpy( ssl->transform_in->iv_dec,
2276 ssl->transform_in->cipher_ctx_dec.iv,
2277 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002279#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
2281 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002282
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002283 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002284 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_SSL_DEBUG_ALL)
2287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002288 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002289#endif
Paul Bakker45829992013-01-03 14:52:21 +01002290 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002291 correct = 0;
2292 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294#if defined(MBEDTLS_SSL_PROTO_SSL3)
2295 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 {
Paul Bakker48916f92012-09-16 19:57:18 +00002297 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299#if defined(MBEDTLS_SSL_DEBUG_ALL)
2300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002302 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002303#endif
Paul Bakker45829992013-01-03 14:52:21 +01002304 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306 }
2307 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2309#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2310 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2311 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 {
2313 /*
Paul Bakker45829992013-01-03 14:52:21 +01002314 * TLSv1+: always check the padding up to the first failure
2315 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002316 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002317 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002318 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002319 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002320
Paul Bakker956c9e02013-12-19 14:42:28 +01002321 /*
2322 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002323 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002324 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002325 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002326 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002327 *
2328 * In both cases we reset padding_idx to a safe value (0) to
2329 * prevent out-of-buffer reads.
2330 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002331 correct &= ( padlen <= ssl->in_msglen );
2332 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002333 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002334
2335 padding_idx *= correct;
2336
Angus Grattonb512bc12018-06-19 15:57:50 +10002337 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002338 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002339 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002340 pad_count += real_count *
2341 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2342 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002343
2344 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002347 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002349#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002350 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002352 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2354 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2357 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002358 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002359
2360 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002362 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002364 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2367 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002370#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002373#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
2375 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002376 * Authenticate if not done yet.
2377 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002379#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002380 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002381 {
Hanno Becker992b6872017-11-09 18:57:39 +00002382 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002383
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002384 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002386 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2387 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#if defined(MBEDTLS_SSL_PROTO_SSL3)
2390 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002391 {
2392 ssl_mac( &ssl->transform_in->md_ctx_dec,
2393 ssl->transform_in->mac_dec,
2394 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002395 ssl->in_ctr, ssl->in_msgtype,
2396 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002397 }
2398 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2400#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2401 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2402 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002403 {
2404 /*
2405 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002406 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002407 *
2408 * Known timing attacks:
2409 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2410 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002411 * To compensate for different timings for the MAC calculation
2412 * depending on how much padding was removed (which is determined
2413 * by padlen), process extra_run more blocks through the hash
2414 * function.
2415 *
2416 * The formula in the paper is
2417 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2418 * where L1 is the size of the header plus the decrypted message
2419 * plus CBC padding and L2 is the size of the header plus the
2420 * decrypted message. This is for an underlying hash function
2421 * with 64-byte blocks.
2422 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2423 * correctly. We round down instead of up, so -56 is the correct
2424 * value for our calculations instead of -55.
2425 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002426 * Repeat the formula rather than defining a block_size variable.
2427 * This avoids requiring division by a variable at runtime
2428 * (which would be marginally less efficient and would require
2429 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002430 */
2431 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002432
2433 /*
2434 * The next two sizes are the minimum and maximum values of
2435 * in_msglen over all padlen values.
2436 *
2437 * They're independent of padlen, since we previously did
2438 * in_msglen -= padlen.
2439 *
2440 * Note that max_len + maclen is never more than the buffer
2441 * length, as we previously did in_msglen -= maclen too.
2442 */
2443 const size_t max_len = ssl->in_msglen + padlen;
2444 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2445
Gilles Peskine20b44082018-05-29 14:06:49 +02002446 switch( ssl->transform_in->ciphersuite_info->mac )
2447 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002448#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2449 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002450 case MBEDTLS_MD_MD5:
2451 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002452 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002453 /* 8 bytes of message size, 64-byte compression blocks */
2454 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2455 ( 13 + ssl->in_msglen + 8 ) / 64;
2456 break;
2457#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002458#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002459 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002460 /* 16 bytes of message size, 128-byte compression blocks */
2461 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2462 ( 13 + ssl->in_msglen + 16 ) / 128;
2463 break;
2464#endif
2465 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2468 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002469
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002470 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2473 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2474 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2475 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002477 /* Make sure we access everything even when padlen > 0. This
2478 * makes the synchronisation requirements for just-in-time
2479 * Prime+Probe attacks much tighter and hopefully impractical. */
2480 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002481 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002482
2483 /* Call mbedtls_md_process at least once due to cache attacks
2484 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002485 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002489
2490 /* Make sure we access all the memory that could contain the MAC,
2491 * before we check it in the next code block. This makes the
2492 * synchronisation requirements for just-in-time Prime+Probe
2493 * attacks much tighter and hopefully impractical. */
2494 ssl_read_memory( ssl->in_msg + min_len,
2495 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002496 }
2497 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2499 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2502 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002504
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002505#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002506 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2507 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2508 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Hanno Becker992b6872017-11-09 18:57:39 +00002511 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2512 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_SSL_DEBUG_ALL)
2515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002516#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002517 correct = 0;
2518 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002519 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002520 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002521
2522 /*
2523 * Finally check the correct flag
2524 */
2525 if( correct == 0 )
2526 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002527#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002528
2529 /* Make extra sure authentication was performed, exactly once */
2530 if( auth_done != 1 )
2531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
2536 if( ssl->in_msglen == 0 )
2537 {
Angus Gratton34817922018-06-19 15:58:22 +10002538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2539 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2540 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2541 {
2542 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2544 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2545 }
2546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2547
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 ssl->nb_zero++;
2549
2550 /*
2551 * Three or more empty messages may be a DoS attack
2552 * (excessive CPU consumption).
2553 */
2554 if( ssl->nb_zero > 3 )
2555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
2560 }
2561 else
2562 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002566 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002567 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002568 }
2569 else
2570#endif
2571 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002572 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002573 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2574 if( ++ssl->in_ctr[i - 1] != 0 )
2575 break;
2576
2577 /* The loop goes to its end iff the counter is wrapping */
2578 if( i == ssl_ep_len( ssl ) )
2579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2581 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002582 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002583 }
2584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 return( 0 );
2588}
2589
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002590#undef MAC_NONE
2591#undef MAC_PLAINTEXT
2592#undef MAC_CIPHERTEXT
2593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002595/*
2596 * Compression/decompression functions
2597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002599{
2600 int ret;
2601 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002602 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002603 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002604 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002607
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002608 if( len_pre == 0 )
2609 return( 0 );
2610
Paul Bakker2770fbd2012-07-03 13:30:23 +00002611 memcpy( msg_pre, ssl->out_msg, len_pre );
2612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002614 ssl->out_msglen ) );
2615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002617 ssl->out_msg, ssl->out_msglen );
2618
Paul Bakker48916f92012-09-16 19:57:18 +00002619 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2620 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2621 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002622 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002623
Paul Bakker48916f92012-09-16 19:57:18 +00002624 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002625 if( ret != Z_OK )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2628 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002629 }
2630
Angus Grattond8213d02016-05-25 20:56:48 +10002631 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002632 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002635 ssl->out_msglen ) );
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002638 ssl->out_msg, ssl->out_msglen );
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002641
2642 return( 0 );
2643}
2644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002646{
2647 int ret;
2648 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002649 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002650 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002651 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002654
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002655 if( len_pre == 0 )
2656 return( 0 );
2657
Paul Bakker2770fbd2012-07-03 13:30:23 +00002658 memcpy( msg_pre, ssl->in_msg, len_pre );
2659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002661 ssl->in_msglen ) );
2662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002664 ssl->in_msg, ssl->in_msglen );
2665
Paul Bakker48916f92012-09-16 19:57:18 +00002666 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2667 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2668 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002669 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002670 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002671
Paul Bakker48916f92012-09-16 19:57:18 +00002672 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002673 if( ret != Z_OK )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2676 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002677 }
2678
Angus Grattond8213d02016-05-25 20:56:48 +10002679 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002680 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002683 ssl->in_msglen ) );
2684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002686 ssl->in_msg, ssl->in_msglen );
2687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002689
2690 return( 0 );
2691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2695static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#if defined(MBEDTLS_SSL_PROTO_DTLS)
2698static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002699{
2700 /* If renegotiation is not enforced, retransmit until we would reach max
2701 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002702 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002703 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002704 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002705 unsigned char doublings = 1;
2706
2707 while( ratio != 0 )
2708 {
2709 ++doublings;
2710 ratio >>= 1;
2711 }
2712
2713 if( ++ssl->renego_records_seen > doublings )
2714 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002716 return( 0 );
2717 }
2718 }
2719
2720 return( ssl_write_hello_request( ssl ) );
2721}
2722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002724
Paul Bakker5121ce52009-01-03 21:22:43 +00002725/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002726 * Fill the input message buffer by appending data to it.
2727 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002728 *
2729 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2730 * available (from this read and/or a previous one). Otherwise, an error code
2731 * is returned (possibly EOF or WANT_READ).
2732 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002733 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2734 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2735 * since we always read a whole datagram at once.
2736 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002737 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002738 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002741{
Paul Bakker23986e52011-04-24 08:57:21 +00002742 int ret;
2743 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002747 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002750 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002752 }
2753
Angus Grattond8213d02016-05-25 20:56:48 +10002754 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002758 }
2759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002763 uint32_t timeout;
2764
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002765 /* Just to be sure */
2766 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2767 {
2768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2769 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2771 }
2772
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002773 /*
2774 * The point is, we need to always read a full datagram at once, so we
2775 * sometimes read more then requested, and handle the additional data.
2776 * It could be the rest of the current record (while fetching the
2777 * header) and/or some other records in the same datagram.
2778 */
2779
2780 /*
2781 * Move to the next record in the already read datagram if applicable
2782 */
2783 if( ssl->next_record_offset != 0 )
2784 {
2785 if( ssl->in_left < ssl->next_record_offset )
2786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002789 }
2790
2791 ssl->in_left -= ssl->next_record_offset;
2792
2793 if( ssl->in_left != 0 )
2794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002796 ssl->next_record_offset ) );
2797 memmove( ssl->in_hdr,
2798 ssl->in_hdr + ssl->next_record_offset,
2799 ssl->in_left );
2800 }
2801
2802 ssl->next_record_offset = 0;
2803 }
2804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002807
2808 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002809 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002810 */
2811 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002814 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002815 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002816
2817 /*
2818 * A record can't be split accross datagrams. If we need to read but
2819 * are not at the beginning of a new record, the caller did something
2820 * wrong.
2821 */
2822 if( ssl->in_left != 0 )
2823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2825 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002826 }
2827
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002828 /*
2829 * Don't even try to read if time's out already.
2830 * This avoids by-passing the timer when repeatedly receiving messages
2831 * that will end up being dropped.
2832 */
2833 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002834 {
2835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002836 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002837 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002838 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002839 {
Angus Grattond8213d02016-05-25 20:56:48 +10002840 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002843 timeout = ssl->handshake->retransmit_timeout;
2844 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002845 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002848
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002849 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002850 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2851 timeout );
2852 else
2853 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002856
2857 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002859 }
2860
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002861 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002864 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002867 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002868 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002871 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002872 }
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002877 return( ret );
2878 }
2879
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002880 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002881 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002883 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002885 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002886 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002889 return( ret );
2890 }
2891
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002892 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002893 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002895 }
2896
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 if( ret < 0 )
2898 return( ret );
2899
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002900 ssl->in_left = ret;
2901 }
2902 else
2903#endif
2904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002906 ssl->in_left, nb_want ) );
2907
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002908 while( ssl->in_left < nb_want )
2909 {
2910 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002911
2912 if( ssl_check_timer( ssl ) != 0 )
2913 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2914 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002915 {
2916 if( ssl->f_recv_timeout != NULL )
2917 {
2918 ret = ssl->f_recv_timeout( ssl->p_bio,
2919 ssl->in_hdr + ssl->in_left, len,
2920 ssl->conf->read_timeout );
2921 }
2922 else
2923 {
2924 ret = ssl->f_recv( ssl->p_bio,
2925 ssl->in_hdr + ssl->in_left, len );
2926 }
2927 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002930 ssl->in_left, nb_want ) );
2931 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002932
2933 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002935
2936 if( ret < 0 )
2937 return( ret );
2938
mohammad160352aecb92018-03-28 23:41:40 -07002939 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002940 {
Darryl Green11999bb2018-03-13 15:22:58 +00002941 MBEDTLS_SSL_DEBUG_MSG( 1,
2942 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002943 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002944 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2945 }
2946
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002947 ssl->in_left += ret;
2948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 }
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
2953 return( 0 );
2954}
2955
2956/*
2957 * Flush any data not yet written
2958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002960{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002961 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002962 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002966 if( ssl->f_send == NULL )
2967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002969 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002971 }
2972
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002973 /* Avoid incrementing counter if data is flushed */
2974 if( ssl->out_left == 0 )
2975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002977 return( 0 );
2978 }
2979
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 while( ssl->out_left > 0 )
2981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2983 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Hanno Becker2b1e3542018-08-06 11:19:13 +01002985 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002986 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
2990 if( ret <= 0 )
2991 return( ret );
2992
mohammad160352aecb92018-03-28 23:41:40 -07002993 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002994 {
Darryl Green11999bb2018-03-13 15:22:58 +00002995 MBEDTLS_SSL_DEBUG_MSG( 1,
2996 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002997 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2999 }
3000
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 ssl->out_left -= ret;
3002 }
3003
Hanno Becker2b1e3542018-08-06 11:19:13 +01003004#if defined(MBEDTLS_SSL_PROTO_DTLS)
3005 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003006 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003007 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003008 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003009 else
3010#endif
3011 {
3012 ssl->out_hdr = ssl->out_buf + 8;
3013 }
3014 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
3018 return( 0 );
3019}
3020
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003021/*
3022 * Functions to handle the DTLS retransmission state machine
3023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003025/*
3026 * Append current handshake message to current outgoing flight
3027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3032 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3033 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003034
3035 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003036 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003037 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003040 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003041 }
3042
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003043 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003044 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003047 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003048 }
3049
3050 /* Copy current handshake message with headers */
3051 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3052 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003053 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003054 msg->next = NULL;
3055
3056 /* Append to the current flight */
3057 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003058 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003059 else
3060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003062 while( cur->next != NULL )
3063 cur = cur->next;
3064 cur->next = msg;
3065 }
3066
Hanno Becker3b235902018-08-06 09:54:53 +01003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003068 return( 0 );
3069}
3070
3071/*
3072 * Free the current flight of handshake messages
3073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 mbedtls_ssl_flight_item *cur = flight;
3077 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003078
3079 while( cur != NULL )
3080 {
3081 next = cur->next;
3082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 mbedtls_free( cur->p );
3084 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003085
3086 cur = next;
3087 }
3088}
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3091static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003092#endif
3093
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003094/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003095 * Swap transform_out and out_ctr with the alternative ones
3096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003100 unsigned char tmp_out_ctr[8];
3101
3102 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003105 return;
3106 }
3107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003109
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003110 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003111 tmp_transform = ssl->transform_out;
3112 ssl->transform_out = ssl->handshake->alt_transform_out;
3113 ssl->handshake->alt_transform_out = tmp_transform;
3114
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003115 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003116 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3117 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003118 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003119
3120 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003121 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3124 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3129 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003130 }
3131 }
3132#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003133}
3134
3135/*
3136 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003137 */
3138int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3139{
3140 int ret = 0;
3141
3142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3143
3144 ret = mbedtls_ssl_flight_transmit( ssl );
3145
3146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3147
3148 return( ret );
3149}
3150
3151/*
3152 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003153 *
3154 * Need to remember the current message in case flush_output returns
3155 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003156 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003157 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003158int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003159{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003160 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003164 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003166
3167 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003168 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003169 ssl_swap_epochs( ssl );
3170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003172 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173
3174 while( ssl->handshake->cur_msg != NULL )
3175 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003176 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003177 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003178
Hanno Beckere1dcb032018-08-17 16:47:58 +01003179 int const is_finished =
3180 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3181 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3182
Hanno Becker04da1892018-08-14 13:22:10 +01003183 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3184 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3185
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003186 /* Swap epochs before sending Finished: we can't do it after
3187 * sending ChangeCipherSpec, in case write returns WANT_READ.
3188 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003189 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003190 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003192 ssl_swap_epochs( ssl );
3193 }
3194
Hanno Becker67bc7c32018-08-06 11:33:50 +01003195 ret = ssl_get_remaining_payload_in_datagram( ssl );
3196 if( ret < 0 )
3197 return( ret );
3198 max_frag_len = (size_t) ret;
3199
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003200 /* CCS is copied as is, while HS messages may need fragmentation */
3201 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3202 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003203 if( max_frag_len == 0 )
3204 {
3205 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3206 return( ret );
3207
3208 continue;
3209 }
3210
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003211 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003212 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003213 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003214
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003215 /* Update position inside current message */
3216 ssl->handshake->cur_msg_p += cur->len;
3217 }
3218 else
3219 {
3220 const unsigned char * const p = ssl->handshake->cur_msg_p;
3221 const size_t hs_len = cur->len - 12;
3222 const size_t frag_off = p - ( cur->p + 12 );
3223 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003224 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003225
Hanno Beckere1dcb032018-08-17 16:47:58 +01003226 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003227 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003228 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003229 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003230
Hanno Becker67bc7c32018-08-06 11:33:50 +01003231 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3232 return( ret );
3233
3234 continue;
3235 }
3236 max_hs_frag_len = max_frag_len - 12;
3237
3238 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3239 max_hs_frag_len : rem_len;
3240
3241 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003242 {
3243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003244 (unsigned) cur_hs_frag_len,
3245 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003246 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003247
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003248 /* Messages are stored with handshake headers as if not fragmented,
3249 * copy beginning of headers then fill fragmentation fields.
3250 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3251 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003253 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3254 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3255 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3256
Hanno Becker67bc7c32018-08-06 11:33:50 +01003257 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3258 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3259 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003260
3261 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3262
Hanno Becker3f7b9732018-08-28 09:53:25 +01003263 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003264 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3265 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003266 ssl->out_msgtype = cur->type;
3267
3268 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003269 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003270 }
3271
3272 /* If done with the current message move to the next one if any */
3273 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3274 {
3275 if( cur->next != NULL )
3276 {
3277 ssl->handshake->cur_msg = cur->next;
3278 ssl->handshake->cur_msg_p = cur->next->p + 12;
3279 }
3280 else
3281 {
3282 ssl->handshake->cur_msg = NULL;
3283 ssl->handshake->cur_msg_p = NULL;
3284 }
3285 }
3286
3287 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003288 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003291 return( ret );
3292 }
3293 }
3294
Hanno Becker67bc7c32018-08-06 11:33:50 +01003295 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3296 return( ret );
3297
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003298 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3300 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003301 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003304 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3305 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003306
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308
3309 return( 0 );
3310}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003311
3312/*
3313 * To be called when the last message of an incoming flight is received.
3314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003316{
3317 /* We won't need to resend that one any more */
3318 ssl_flight_free( ssl->handshake->flight );
3319 ssl->handshake->flight = NULL;
3320 ssl->handshake->cur_msg = NULL;
3321
3322 /* The next incoming flight will start with this msg_seq */
3323 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3324
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003325 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003326 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003327
Hanno Becker0271f962018-08-16 13:23:47 +01003328 /* Clear future message buffering structure. */
3329 ssl_buffering_free( ssl );
3330
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003331 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003332 ssl_set_timer( ssl, 0 );
3333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3335 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003338 }
3339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003341}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003342
3343/*
3344 * To be called when the last message of an outgoing flight is send.
3345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003347{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003348 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003349 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3352 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003355 }
3356 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003360
Paul Bakker5121ce52009-01-03 21:22:43 +00003361/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003362 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364
3365/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003366 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003367 *
3368 * - fill in handshake headers
3369 * - update handshake checksum
3370 * - DTLS: save message for resending
3371 * - then pass to the record layer
3372 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003373 * DTLS: except for HelloRequest, messages are only queued, and will only be
3374 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003375 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003376 * Inputs:
3377 * - ssl->out_msglen: 4 + actual handshake message len
3378 * (4 is the size of handshake headers for TLS)
3379 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3380 * - ssl->out_msg + 4: the handshake message body
3381 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003382 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003383 * - ssl->out_msglen: the length of the record contents
3384 * (including handshake headers but excluding record headers)
3385 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003386 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003387int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003388{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003389 int ret;
3390 const size_t hs_len = ssl->out_msglen - 4;
3391 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3394
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003395 /*
3396 * Sanity checks
3397 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003398 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003399 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3400 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003401 /* In SSLv3, the client might send a NoCertificate alert. */
3402#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3403 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3404 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3405 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3406#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3407 {
3408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3409 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3410 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003413 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3414 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3415 ssl->handshake == NULL )
3416 {
3417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003422 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003423 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003425 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003428 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003430
Hanno Beckerb50a2532018-08-06 11:52:54 +01003431 /* Double-check that we did not exceed the bounds
3432 * of the outgoing record buffer.
3433 * This should never fail as the various message
3434 * writing functions must obey the bounds of the
3435 * outgoing record buffer, but better be safe.
3436 *
3437 * Note: We deliberately do not check for the MTU or MFL here.
3438 */
3439 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3440 {
3441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3442 "size %u, maximum %u",
3443 (unsigned) ssl->out_msglen,
3444 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3446 }
3447
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003448 /*
3449 * Fill handshake headers
3450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003453 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3454 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3455 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003456
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003457 /*
3458 * DTLS has additional fields in the Handshake layer,
3459 * between the length field and the actual payload:
3460 * uint16 message_seq;
3461 * uint24 fragment_offset;
3462 * uint24 fragment_length;
3463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003465 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003466 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003467 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003468 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003469 {
3470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3471 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003472 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003473 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3475 }
3476
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003477 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003478 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003479
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003480 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003481 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003482 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003483 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3484 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3485 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003486 }
3487 else
3488 {
3489 ssl->out_msg[4] = 0;
3490 ssl->out_msg[5] = 0;
3491 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003492
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003493 /* Handshake hashes are computed without fragmentation,
3494 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003495 memset( ssl->out_msg + 6, 0x00, 3 );
3496 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003497 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003499
Hanno Becker0207e532018-08-28 10:28:28 +01003500 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003501 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3502 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 }
3504
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003505 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003507 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003508 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3509 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003510 {
3511 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514 return( ret );
3515 }
3516 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003517 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003518#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003519 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003521 {
3522 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3523 return( ret );
3524 }
3525 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003526
3527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3528
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003529 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003530}
3531
3532/*
3533 * Record layer functions
3534 */
3535
3536/*
3537 * Write current record.
3538 *
3539 * Uses:
3540 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3541 * - ssl->out_msglen: length of the record content (excl headers)
3542 * - ssl->out_msg: record content
3543 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003544int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003545{
3546 int ret, done = 0;
3547 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003548 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003549
3550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003553 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003555 {
3556 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559 return( ret );
3560 }
3561
3562 len = ssl->out_msglen;
3563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3567 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 ret = mbedtls_ssl_hw_record_write( ssl );
3572 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3575 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003576 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003577
3578 if( ret == 0 )
3579 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003580 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003582 if( !done )
3583 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003584 unsigned i;
3585 size_t protected_record_size;
3586
Paul Bakker05ef8352012-05-08 09:17:57 +00003587 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003589 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003590
Hanno Becker19859472018-08-06 09:40:20 +01003591 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003592 ssl->out_len[0] = (unsigned char)( len >> 8 );
3593 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003594
Paul Bakker48916f92012-09-16 19:57:18 +00003595 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003596 {
3597 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003600 return( ret );
3601 }
3602
3603 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003604 ssl->out_len[0] = (unsigned char)( len >> 8 );
3605 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003606 }
3607
Hanno Becker2b1e3542018-08-06 11:19:13 +01003608 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3609
3610#if defined(MBEDTLS_SSL_PROTO_DTLS)
3611 /* In case of DTLS, double-check that we don't exceed
3612 * the remaining space in the datagram. */
3613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3614 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003615 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003616 if( ret < 0 )
3617 return( ret );
3618
3619 if( protected_record_size > (size_t) ret )
3620 {
3621 /* Should never happen */
3622 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3623 }
3624 }
3625#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003628 "version = [%d:%d], msglen = %d",
3629 ssl->out_hdr[0], ssl->out_hdr[1],
3630 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003633 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003634
3635 ssl->out_left += protected_record_size;
3636 ssl->out_hdr += protected_record_size;
3637 ssl_update_out_pointers( ssl, ssl->transform_out );
3638
Hanno Becker04484622018-08-06 09:49:38 +01003639 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3640 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3641 break;
3642
3643 /* The loop goes to its end iff the counter is wrapping */
3644 if( i == ssl_ep_len( ssl ) )
3645 {
3646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3647 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 }
3650
Hanno Becker67bc7c32018-08-06 11:33:50 +01003651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003652 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3653 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003654 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003655 size_t remaining;
3656 ret = ssl_get_remaining_payload_in_datagram( ssl );
3657 if( ret < 0 )
3658 {
3659 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3660 ret );
3661 return( ret );
3662 }
3663
3664 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003665 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003666 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003667 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003668 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003669 else
3670 {
Hanno Becker513815a2018-08-20 11:56:09 +01003671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003672 }
3673 }
3674#endif /* MBEDTLS_SSL_PROTO_DTLS */
3675
3676 if( ( flush == SSL_FORCE_FLUSH ) &&
3677 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 return( ret );
3681 }
3682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003684
3685 return( 0 );
3686}
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003689
3690static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3691{
3692 if( ssl->in_msglen < ssl->in_hslen ||
3693 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3694 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3695 {
3696 return( 1 );
3697 }
3698 return( 0 );
3699}
Hanno Becker44650b72018-08-16 12:51:11 +01003700
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003701static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003702{
3703 return( ( ssl->in_msg[9] << 16 ) |
3704 ( ssl->in_msg[10] << 8 ) |
3705 ssl->in_msg[11] );
3706}
3707
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003708static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003709{
3710 return( ( ssl->in_msg[6] << 16 ) |
3711 ( ssl->in_msg[7] << 8 ) |
3712 ssl->in_msg[8] );
3713}
3714
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003715static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003716{
3717 uint32_t msg_len, frag_off, frag_len;
3718
3719 msg_len = ssl_get_hs_total_len( ssl );
3720 frag_off = ssl_get_hs_frag_off( ssl );
3721 frag_len = ssl_get_hs_frag_len( ssl );
3722
3723 if( frag_off > msg_len )
3724 return( -1 );
3725
3726 if( frag_len > msg_len - frag_off )
3727 return( -1 );
3728
3729 if( frag_len + 12 > ssl->in_msglen )
3730 return( -1 );
3731
3732 return( 0 );
3733}
3734
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003735/*
3736 * Mark bits in bitmask (used for DTLS HS reassembly)
3737 */
3738static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3739{
3740 unsigned int start_bits, end_bits;
3741
3742 start_bits = 8 - ( offset % 8 );
3743 if( start_bits != 8 )
3744 {
3745 size_t first_byte_idx = offset / 8;
3746
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003747 /* Special case */
3748 if( len <= start_bits )
3749 {
3750 for( ; len != 0; len-- )
3751 mask[first_byte_idx] |= 1 << ( start_bits - len );
3752
3753 /* Avoid potential issues with offset or len becoming invalid */
3754 return;
3755 }
3756
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003757 offset += start_bits; /* Now offset % 8 == 0 */
3758 len -= start_bits;
3759
3760 for( ; start_bits != 0; start_bits-- )
3761 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3762 }
3763
3764 end_bits = len % 8;
3765 if( end_bits != 0 )
3766 {
3767 size_t last_byte_idx = ( offset + len ) / 8;
3768
3769 len -= end_bits; /* Now len % 8 == 0 */
3770
3771 for( ; end_bits != 0; end_bits-- )
3772 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3773 }
3774
3775 memset( mask + offset / 8, 0xFF, len / 8 );
3776}
3777
3778/*
3779 * Check that bitmask is full
3780 */
3781static int ssl_bitmask_check( unsigned char *mask, size_t len )
3782{
3783 size_t i;
3784
3785 for( i = 0; i < len / 8; i++ )
3786 if( mask[i] != 0xFF )
3787 return( -1 );
3788
3789 for( i = 0; i < len % 8; i++ )
3790 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3791 return( -1 );
3792
3793 return( 0 );
3794}
3795
Hanno Becker56e205e2018-08-16 09:06:12 +01003796/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003797static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003798 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003799{
Hanno Becker56e205e2018-08-16 09:06:12 +01003800 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003801
Hanno Becker56e205e2018-08-16 09:06:12 +01003802 alloc_len = 12; /* Handshake header */
3803 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003804
Hanno Beckerd07df862018-08-16 09:14:58 +01003805 if( add_bitmap )
3806 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003807
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003808 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003809}
Hanno Becker56e205e2018-08-16 09:06:12 +01003810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003812
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003813static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003814{
3815 return( ( ssl->in_msg[1] << 16 ) |
3816 ( ssl->in_msg[2] << 8 ) |
3817 ssl->in_msg[3] );
3818}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003819
Simon Butcher99000142016-10-13 17:21:01 +01003820int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003825 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003827 }
3828
Hanno Becker12555c62018-08-16 12:47:53 +01003829 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003832 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003833 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003836 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003837 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003838 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003839 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003840
Hanno Becker44650b72018-08-16 12:51:11 +01003841 if( ssl_check_hs_header( ssl ) != 0 )
3842 {
3843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3844 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3845 }
3846
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003847 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003848 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3849 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3850 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3851 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003852 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003853 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3854 {
3855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3856 recv_msg_seq,
3857 ssl->handshake->in_msg_seq ) );
3858 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3859 }
3860
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003861 /* Retransmit only on last message from previous flight, to avoid
3862 * too many retransmissions.
3863 * Besides, No sane server ever retransmits HelloVerifyRequest */
3864 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003868 "message_seq = %d, start_of_flight = %d",
3869 recv_msg_seq,
3870 ssl->handshake->in_flight_start_seq ) );
3871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003875 return( ret );
3876 }
3877 }
3878 else
3879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003881 "message_seq = %d, expected = %d",
3882 recv_msg_seq,
3883 ssl->handshake->in_msg_seq ) );
3884 }
3885
Hanno Becker90333da2017-10-10 11:27:13 +01003886 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003887 }
3888 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003889
Hanno Becker6d97ef52018-08-16 13:09:04 +01003890 /* Message reassembly is handled alongside buffering of future
3891 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003892 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003893 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003894 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003897 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003898 }
3899 }
3900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003902 /* With TLS we don't handle fragmentation (for now) */
3903 if( ssl->in_msglen < ssl->in_hslen )
3904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3906 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003907 }
3908
Simon Butcher99000142016-10-13 17:21:01 +01003909 return( 0 );
3910}
3911
3912void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3913{
Hanno Becker0271f962018-08-16 13:23:47 +01003914 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003915
Hanno Becker0271f962018-08-16 13:23:47 +01003916 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003917 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003918 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003919 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003920
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003921 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003923 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003924 ssl->handshake != NULL )
3925 {
Hanno Becker0271f962018-08-16 13:23:47 +01003926 unsigned offset;
3927 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003928
Hanno Becker0271f962018-08-16 13:23:47 +01003929 /* Increment handshake sequence number */
3930 hs->in_msg_seq++;
3931
3932 /*
3933 * Clear up handshake buffering and reassembly structure.
3934 */
3935
3936 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003937 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003938
3939 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003940 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3941 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003942 offset++, hs_buf++ )
3943 {
3944 *hs_buf = *(hs_buf + 1);
3945 }
3946
3947 /* Create a fresh last entry */
3948 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003949 }
3950#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003951}
3952
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003953/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003954 * DTLS anti-replay: RFC 6347 4.1.2.6
3955 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003956 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3957 * Bit n is set iff record number in_window_top - n has been seen.
3958 *
3959 * Usually, in_window_top is the last record number seen and the lsb of
3960 * in_window is set. The only exception is the initial state (record number 0
3961 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3964static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003965{
3966 ssl->in_window_top = 0;
3967 ssl->in_window = 0;
3968}
3969
3970static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3971{
3972 return( ( (uint64_t) buf[0] << 40 ) |
3973 ( (uint64_t) buf[1] << 32 ) |
3974 ( (uint64_t) buf[2] << 24 ) |
3975 ( (uint64_t) buf[3] << 16 ) |
3976 ( (uint64_t) buf[4] << 8 ) |
3977 ( (uint64_t) buf[5] ) );
3978}
3979
3980/*
3981 * Return 0 if sequence number is acceptable, -1 otherwise
3982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003984{
3985 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3986 uint64_t bit;
3987
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003988 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003989 return( 0 );
3990
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003991 if( rec_seqnum > ssl->in_window_top )
3992 return( 0 );
3993
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003994 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003995
3996 if( bit >= 64 )
3997 return( -1 );
3998
3999 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4000 return( -1 );
4001
4002 return( 0 );
4003}
4004
4005/*
4006 * Update replay window on new validated record
4007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004009{
4010 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4011
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004012 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004013 return;
4014
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004015 if( rec_seqnum > ssl->in_window_top )
4016 {
4017 /* Update window_top and the contents of the window */
4018 uint64_t shift = rec_seqnum - ssl->in_window_top;
4019
4020 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004021 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004022 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004023 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004024 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004025 ssl->in_window |= 1;
4026 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004027
4028 ssl->in_window_top = rec_seqnum;
4029 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004030 else
4031 {
4032 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004033 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004034
4035 if( bit < 64 ) /* Always true, but be extra sure */
4036 ssl->in_window |= (uint64_t) 1 << bit;
4037 }
4038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004040
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004041#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004042/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004043static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4044
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004045/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004046 * Without any SSL context, check if a datagram looks like a ClientHello with
4047 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004048 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004049 *
4050 * - if cookie is valid, return 0
4051 * - if ClientHello looks superficially valid but cookie is not,
4052 * fill obuf and set olen, then
4053 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4054 * - otherwise return a specific error code
4055 */
4056static int ssl_check_dtls_clihlo_cookie(
4057 mbedtls_ssl_cookie_write_t *f_cookie_write,
4058 mbedtls_ssl_cookie_check_t *f_cookie_check,
4059 void *p_cookie,
4060 const unsigned char *cli_id, size_t cli_id_len,
4061 const unsigned char *in, size_t in_len,
4062 unsigned char *obuf, size_t buf_len, size_t *olen )
4063{
4064 size_t sid_len, cookie_len;
4065 unsigned char *p;
4066
4067 if( f_cookie_write == NULL || f_cookie_check == NULL )
4068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4069
4070 /*
4071 * Structure of ClientHello with record and handshake headers,
4072 * and expected values. We don't need to check a lot, more checks will be
4073 * done when actually parsing the ClientHello - skipping those checks
4074 * avoids code duplication and does not make cookie forging any easier.
4075 *
4076 * 0-0 ContentType type; copied, must be handshake
4077 * 1-2 ProtocolVersion version; copied
4078 * 3-4 uint16 epoch; copied, must be 0
4079 * 5-10 uint48 sequence_number; copied
4080 * 11-12 uint16 length; (ignored)
4081 *
4082 * 13-13 HandshakeType msg_type; (ignored)
4083 * 14-16 uint24 length; (ignored)
4084 * 17-18 uint16 message_seq; copied
4085 * 19-21 uint24 fragment_offset; copied, must be 0
4086 * 22-24 uint24 fragment_length; (ignored)
4087 *
4088 * 25-26 ProtocolVersion client_version; (ignored)
4089 * 27-58 Random random; (ignored)
4090 * 59-xx SessionID session_id; 1 byte len + sid_len content
4091 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4092 * ...
4093 *
4094 * Minimum length is 61 bytes.
4095 */
4096 if( in_len < 61 ||
4097 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4098 in[3] != 0 || in[4] != 0 ||
4099 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4100 {
4101 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4102 }
4103
4104 sid_len = in[59];
4105 if( sid_len > in_len - 61 )
4106 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4107
4108 cookie_len = in[60 + sid_len];
4109 if( cookie_len > in_len - 60 )
4110 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4111
4112 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4113 cli_id, cli_id_len ) == 0 )
4114 {
4115 /* Valid cookie */
4116 return( 0 );
4117 }
4118
4119 /*
4120 * If we get here, we've got an invalid cookie, let's prepare HVR.
4121 *
4122 * 0-0 ContentType type; copied
4123 * 1-2 ProtocolVersion version; copied
4124 * 3-4 uint16 epoch; copied
4125 * 5-10 uint48 sequence_number; copied
4126 * 11-12 uint16 length; olen - 13
4127 *
4128 * 13-13 HandshakeType msg_type; hello_verify_request
4129 * 14-16 uint24 length; olen - 25
4130 * 17-18 uint16 message_seq; copied
4131 * 19-21 uint24 fragment_offset; copied
4132 * 22-24 uint24 fragment_length; olen - 25
4133 *
4134 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4135 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4136 *
4137 * Minimum length is 28.
4138 */
4139 if( buf_len < 28 )
4140 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4141
4142 /* Copy most fields and adapt others */
4143 memcpy( obuf, in, 25 );
4144 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4145 obuf[25] = 0xfe;
4146 obuf[26] = 0xff;
4147
4148 /* Generate and write actual cookie */
4149 p = obuf + 28;
4150 if( f_cookie_write( p_cookie,
4151 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4152 {
4153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4154 }
4155
4156 *olen = p - obuf;
4157
4158 /* Go back and fill length fields */
4159 obuf[27] = (unsigned char)( *olen - 28 );
4160
4161 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4162 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4163 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4164
4165 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4166 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4167
4168 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4169}
4170
4171/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004172 * Handle possible client reconnect with the same UDP quadruplet
4173 * (RFC 6347 Section 4.2.8).
4174 *
4175 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4176 * that looks like a ClientHello.
4177 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004178 * - if the input looks like a ClientHello without cookies,
4179 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004180 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004181 * - if the input looks like a ClientHello with a valid cookie,
4182 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004183 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004184 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004185 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004186 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004187 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4188 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004189 */
4190static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4191{
4192 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004193 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004194
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004195 ret = ssl_check_dtls_clihlo_cookie(
4196 ssl->conf->f_cookie_write,
4197 ssl->conf->f_cookie_check,
4198 ssl->conf->p_cookie,
4199 ssl->cli_id, ssl->cli_id_len,
4200 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004201 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004202
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004203 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4204
4205 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004206 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004207 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004208 * If the error is permanent we'll catch it later,
4209 * if it's not, then hopefully it'll work next time. */
4210 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4211
4212 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004213 }
4214
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004215 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004216 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004217 /* Got a valid cookie, partially reset context */
4218 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4219 {
4220 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4221 return( ret );
4222 }
4223
4224 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004225 }
4226
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004227 return( ret );
4228}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004229#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004230
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004231/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004232 * ContentType type;
4233 * ProtocolVersion version;
4234 * uint16 epoch; // DTLS only
4235 * uint48 sequence_number; // DTLS only
4236 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004237 *
4238 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004239 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004240 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4241 *
4242 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004243 * 1. proceed with the record if this function returns 0
4244 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4245 * 3. return CLIENT_RECONNECT if this function return that value
4246 * 4. drop the whole datagram if this function returns anything else.
4247 * Point 2 is needed when the peer is resending, and we have already received
4248 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004251{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004252 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 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 +02004255
Paul Bakker5121ce52009-01-03 21:22:43 +00004256 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004257 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004258 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004261 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004262 ssl->in_msgtype,
4263 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004264
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004265 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4267 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4268 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4269 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004272
4273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004274 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4275 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004276 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4277#endif /* MBEDTLS_SSL_PROTO_DTLS */
4278 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4279 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004282 }
4283
4284 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004285 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4288 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004289 }
4290
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004291 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4294 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295 }
4296
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004297 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004298 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004299 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4302 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004303 }
4304
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004305 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004306 * DTLS-related tests.
4307 * Check epoch before checking length constraint because
4308 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4309 * message gets duplicated before the corresponding Finished message,
4310 * the second ChangeCipherSpec should be discarded because it belongs
4311 * to an old epoch, but not because its length is shorter than
4312 * the minimum record length for packets using the new record transform.
4313 * Note that these two kinds of failures are handled differently,
4314 * as an unexpected record is silently skipped but an invalid
4315 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004316 */
4317#if defined(MBEDTLS_SSL_PROTO_DTLS)
4318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4319 {
4320 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4321
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004322 /* Check epoch (and sequence number) with DTLS */
4323 if( rec_epoch != ssl->in_epoch )
4324 {
4325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4326 "expected %d, received %d",
4327 ssl->in_epoch, rec_epoch ) );
4328
4329#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4330 /*
4331 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4332 * access the first byte of record content (handshake type), as we
4333 * have an active transform (possibly iv_len != 0), so use the
4334 * fact that the record header len is 13 instead.
4335 */
4336 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4337 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4338 rec_epoch == 0 &&
4339 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4340 ssl->in_left > 13 &&
4341 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4342 {
4343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4344 "from the same port" ) );
4345 return( ssl_handle_possible_reconnect( ssl ) );
4346 }
4347 else
4348#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004349 {
4350 /* Consider buffering the record. */
4351 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4352 {
4353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4354 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4355 }
4356
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004357 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004358 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004359 }
4360
4361#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4362 /* Replay detection only works for the current epoch */
4363 if( rec_epoch == ssl->in_epoch &&
4364 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4365 {
4366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4367 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4368 }
4369#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004370
Hanno Becker52c6dc62017-05-26 16:07:36 +01004371 /* Drop unexpected ApplicationData records,
4372 * except at the beginning of renegotiations */
4373 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4374 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4375#if defined(MBEDTLS_SSL_RENEGOTIATION)
4376 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4377 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4378#endif
4379 )
4380 {
4381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4382 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4383 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004384 }
4385#endif /* MBEDTLS_SSL_PROTO_DTLS */
4386
Hanno Becker52c6dc62017-05-26 16:07:36 +01004387
4388 /* Check length against bounds of the current transform and version */
4389 if( ssl->transform_in == NULL )
4390 {
4391 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004392 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004393 {
4394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4395 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4396 }
4397 }
4398 else
4399 {
4400 if( ssl->in_msglen < ssl->transform_in->minlen )
4401 {
4402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4403 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4404 }
4405
4406#if defined(MBEDTLS_SSL_PROTO_SSL3)
4407 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004408 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004409 {
4410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4411 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4412 }
4413#endif
4414#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4415 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4416 /*
4417 * TLS encrypted messages can have up to 256 bytes of padding
4418 */
4419 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4420 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004421 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004422 {
4423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4424 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4425 }
4426#endif
4427 }
4428
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004429 return( 0 );
4430}
Paul Bakker5121ce52009-01-03 21:22:43 +00004431
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004432/*
4433 * If applicable, decrypt (and decompress) record content
4434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004436{
4437 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4440 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4443 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 ret = mbedtls_ssl_hw_record_read( ssl );
4448 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4451 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004452 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004453
4454 if( ret == 0 )
4455 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004458 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004459 {
4460 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 return( ret );
4464 }
4465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 ssl->in_msg, ssl->in_msglen );
4468
Angus Grattond8213d02016-05-25 20:56:48 +10004469 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4472 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004473 }
4474 }
4475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004477 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004479 {
4480 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004483 return( ret );
4484 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004492 }
4493#endif
4494
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004495 return( 0 );
4496}
4497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004499
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004500/*
4501 * Read a record.
4502 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004503 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4504 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4505 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004506 */
Hanno Becker1097b342018-08-15 14:09:41 +01004507
4508/* Helper functions for mbedtls_ssl_read_record(). */
4509static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004510static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4511static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004512
Hanno Becker327c93b2018-08-15 13:56:18 +01004513int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004514 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004515{
4516 int ret;
4517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004519
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004520 if( ssl->keep_current_message == 0 )
4521 {
4522 do {
Simon Butcher99000142016-10-13 17:21:01 +01004523
Hanno Becker26994592018-08-15 14:14:59 +01004524 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004525 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004526 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004527
Hanno Beckere74d5562018-08-15 14:26:08 +01004528 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004529 {
Hanno Becker40f50842018-08-15 14:48:01 +01004530#if defined(MBEDTLS_SSL_PROTO_DTLS)
4531 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004532
Hanno Becker40f50842018-08-15 14:48:01 +01004533 /* We only check for buffered messages if the
4534 * current datagram is fully consumed. */
4535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004536 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004537 {
Hanno Becker40f50842018-08-15 14:48:01 +01004538 if( ssl_load_buffered_message( ssl ) == 0 )
4539 have_buffered = 1;
4540 }
4541
4542 if( have_buffered == 0 )
4543#endif /* MBEDTLS_SSL_PROTO_DTLS */
4544 {
4545 ret = ssl_get_next_record( ssl );
4546 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4547 continue;
4548
4549 if( ret != 0 )
4550 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004551 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004552 return( ret );
4553 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004554 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004555 }
4556
4557 ret = mbedtls_ssl_handle_message_type( ssl );
4558
Hanno Becker40f50842018-08-15 14:48:01 +01004559#if defined(MBEDTLS_SSL_PROTO_DTLS)
4560 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4561 {
4562 /* Buffer future message */
4563 ret = ssl_buffer_message( ssl );
4564 if( ret != 0 )
4565 return( ret );
4566
4567 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4568 }
4569#endif /* MBEDTLS_SSL_PROTO_DTLS */
4570
Hanno Becker90333da2017-10-10 11:27:13 +01004571 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4572 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004573
4574 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004575 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004576 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004577 return( ret );
4578 }
4579
Hanno Becker327c93b2018-08-15 13:56:18 +01004580 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004581 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004582 {
4583 mbedtls_ssl_update_handshake_status( ssl );
4584 }
Simon Butcher99000142016-10-13 17:21:01 +01004585 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004586 else
Simon Butcher99000142016-10-13 17:21:01 +01004587 {
Hanno Becker02f59072018-08-15 14:00:24 +01004588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004589 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004590 }
4591
4592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4593
4594 return( 0 );
4595}
4596
Hanno Becker40f50842018-08-15 14:48:01 +01004597#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004598static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004599{
Hanno Becker40f50842018-08-15 14:48:01 +01004600 if( ssl->in_left > ssl->next_record_offset )
4601 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004602
Hanno Becker40f50842018-08-15 14:48:01 +01004603 return( 0 );
4604}
4605
4606static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4607{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004608 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004609 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004610 int ret = 0;
4611
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004612 if( hs == NULL )
4613 return( -1 );
4614
Hanno Beckere00ae372018-08-20 09:39:42 +01004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4616
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004617 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4618 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4619 {
4620 /* Check if we have seen a ChangeCipherSpec before.
4621 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004622 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004623 {
4624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4625 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004626 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004627 }
4628
Hanno Becker39b8bc92018-08-28 17:17:13 +01004629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004630 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4631 ssl->in_msglen = 1;
4632 ssl->in_msg[0] = 1;
4633
4634 /* As long as they are equal, the exact value doesn't matter. */
4635 ssl->in_left = 0;
4636 ssl->next_record_offset = 0;
4637
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004638 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004639 goto exit;
4640 }
Hanno Becker37f95322018-08-16 13:55:32 +01004641
Hanno Beckerb8f50142018-08-28 10:01:34 +01004642#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004643 /* Debug only */
4644 {
4645 unsigned offset;
4646 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4647 {
4648 hs_buf = &hs->buffering.hs[offset];
4649 if( hs_buf->is_valid == 1 )
4650 {
4651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4652 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004653 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004654 }
4655 }
4656 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004657#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004658
4659 /* Check if we have buffered and/or fully reassembled the
4660 * next handshake message. */
4661 hs_buf = &hs->buffering.hs[0];
4662 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4663 {
4664 /* Synthesize a record containing the buffered HS message. */
4665 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4666 ( hs_buf->data[2] << 8 ) |
4667 hs_buf->data[3];
4668
4669 /* Double-check that we haven't accidentally buffered
4670 * a message that doesn't fit into the input buffer. */
4671 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4672 {
4673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4675 }
4676
4677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4678 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4679 hs_buf->data, msg_len + 12 );
4680
4681 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4682 ssl->in_hslen = msg_len + 12;
4683 ssl->in_msglen = msg_len + 12;
4684 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4685
4686 ret = 0;
4687 goto exit;
4688 }
4689 else
4690 {
4691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4692 hs->in_msg_seq ) );
4693 }
4694
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004695 ret = -1;
4696
4697exit:
4698
4699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4700 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004701}
4702
Hanno Beckera02b0b42018-08-21 17:20:27 +01004703static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4704 size_t desired )
4705{
4706 int offset;
4707 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4709 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004710
Hanno Becker01315ea2018-08-21 17:22:17 +01004711 /* Get rid of future records epoch first, if such exist. */
4712 ssl_free_buffered_record( ssl );
4713
4714 /* Check if we have enough space available now. */
4715 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4716 hs->buffering.total_bytes_buffered ) )
4717 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004719 return( 0 );
4720 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004721
Hanno Becker4f432ad2018-08-28 10:02:32 +01004722 /* We don't have enough space to buffer the next expected handshake
4723 * message. Remove buffers used for future messages to gain space,
4724 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004725 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4726 offset >= 0; offset-- )
4727 {
4728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4729 offset ) );
4730
Hanno Beckerb309b922018-08-23 13:18:05 +01004731 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004732
4733 /* Check if we have enough space available now. */
4734 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4735 hs->buffering.total_bytes_buffered ) )
4736 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004738 return( 0 );
4739 }
4740 }
4741
4742 return( -1 );
4743}
4744
Hanno Becker40f50842018-08-15 14:48:01 +01004745static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4746{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004747 int ret = 0;
4748 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4749
4750 if( hs == NULL )
4751 return( 0 );
4752
4753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4754
4755 switch( ssl->in_msgtype )
4756 {
4757 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004759
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004760 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004761 break;
4762
4763 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004764 {
4765 unsigned recv_msg_seq_offset;
4766 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4767 mbedtls_ssl_hs_buffer *hs_buf;
4768 size_t msg_len = ssl->in_hslen - 12;
4769
4770 /* We should never receive an old handshake
4771 * message - double-check nonetheless. */
4772 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4773 {
4774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4775 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4776 }
4777
4778 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4779 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4780 {
4781 /* Silently ignore -- message too far in the future */
4782 MBEDTLS_SSL_DEBUG_MSG( 2,
4783 ( "Ignore future HS message with sequence number %u, "
4784 "buffering window %u - %u",
4785 recv_msg_seq, ssl->handshake->in_msg_seq,
4786 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4787
4788 goto exit;
4789 }
4790
4791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4792 recv_msg_seq, recv_msg_seq_offset ) );
4793
4794 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4795
4796 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004797 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004798 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004799 size_t reassembly_buf_sz;
4800
Hanno Becker37f95322018-08-16 13:55:32 +01004801 hs_buf->is_fragmented =
4802 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4803
4804 /* We copy the message back into the input buffer
4805 * after reassembly, so check that it's not too large.
4806 * This is an implementation-specific limitation
4807 * and not one from the standard, hence it is not
4808 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004809 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004810 {
4811 /* Ignore message */
4812 goto exit;
4813 }
4814
Hanno Beckere0b150f2018-08-21 15:51:03 +01004815 /* Check if we have enough space to buffer the message. */
4816 if( hs->buffering.total_bytes_buffered >
4817 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4818 {
4819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4820 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4821 }
4822
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004823 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4824 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004825
4826 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4827 hs->buffering.total_bytes_buffered ) )
4828 {
4829 if( recv_msg_seq_offset > 0 )
4830 {
4831 /* If we can't buffer a future message because
4832 * of space limitations -- ignore. */
4833 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",
4834 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4835 (unsigned) hs->buffering.total_bytes_buffered ) );
4836 goto exit;
4837 }
Hanno Beckere1801392018-08-21 16:51:05 +01004838 else
4839 {
4840 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",
4841 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4842 (unsigned) hs->buffering.total_bytes_buffered ) );
4843 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004844
Hanno Beckera02b0b42018-08-21 17:20:27 +01004845 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004846 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004847 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",
4848 (unsigned) msg_len,
4849 (unsigned) reassembly_buf_sz,
4850 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004851 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004852 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4853 goto exit;
4854 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004855 }
4856
4857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4858 msg_len ) );
4859
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004860 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4861 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004862 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004863 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004864 goto exit;
4865 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004866 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004867
4868 /* Prepare final header: copy msg_type, length and message_seq,
4869 * then add standardised fragment_offset and fragment_length */
4870 memcpy( hs_buf->data, ssl->in_msg, 6 );
4871 memset( hs_buf->data + 6, 0, 3 );
4872 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4873
4874 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004875
4876 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004877 }
4878 else
4879 {
4880 /* Make sure msg_type and length are consistent */
4881 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4882 {
4883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4884 /* Ignore */
4885 goto exit;
4886 }
4887 }
4888
Hanno Becker4422bbb2018-08-20 09:40:19 +01004889 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004890 {
4891 size_t frag_len, frag_off;
4892 unsigned char * const msg = hs_buf->data + 12;
4893
4894 /*
4895 * Check and copy current fragment
4896 */
4897
4898 /* Validation of header fields already done in
4899 * mbedtls_ssl_prepare_handshake_record(). */
4900 frag_off = ssl_get_hs_frag_off( ssl );
4901 frag_len = ssl_get_hs_frag_len( ssl );
4902
4903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4904 frag_off, frag_len ) );
4905 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4906
4907 if( hs_buf->is_fragmented )
4908 {
4909 unsigned char * const bitmask = msg + msg_len;
4910 ssl_bitmask_set( bitmask, frag_off, frag_len );
4911 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4912 msg_len ) == 0 );
4913 }
4914 else
4915 {
4916 hs_buf->is_complete = 1;
4917 }
4918
4919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4920 hs_buf->is_complete ? "" : "not yet " ) );
4921 }
4922
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004923 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004924 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004925
4926 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004927 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004928 break;
4929 }
4930
4931exit:
4932
4933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4934 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004935}
4936#endif /* MBEDTLS_SSL_PROTO_DTLS */
4937
Hanno Becker1097b342018-08-15 14:09:41 +01004938static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004939{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004940 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004941 * Consume last content-layer message and potentially
4942 * update in_msglen which keeps track of the contents'
4943 * consumption state.
4944 *
4945 * (1) Handshake messages:
4946 * Remove last handshake message, move content
4947 * and adapt in_msglen.
4948 *
4949 * (2) Alert messages:
4950 * Consume whole record content, in_msglen = 0.
4951 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004952 * (3) Change cipher spec:
4953 * Consume whole record content, in_msglen = 0.
4954 *
4955 * (4) Application data:
4956 * Don't do anything - the record layer provides
4957 * the application data as a stream transport
4958 * and consumes through mbedtls_ssl_read only.
4959 *
4960 */
4961
4962 /* Case (1): Handshake messages */
4963 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004964 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004965 /* Hard assertion to be sure that no application data
4966 * is in flight, as corrupting ssl->in_msglen during
4967 * ssl->in_offt != NULL is fatal. */
4968 if( ssl->in_offt != NULL )
4969 {
4970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4971 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4972 }
4973
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004974 /*
4975 * Get next Handshake message in the current record
4976 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004977
Hanno Becker4a810fb2017-05-24 16:27:30 +01004978 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004979 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004980 * current handshake content: If DTLS handshake
4981 * fragmentation is used, that's the fragment
4982 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004983 * size here is faulty and should be changed at
4984 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004985 * (2) While it doesn't seem to cause problems, one
4986 * has to be very careful not to assume that in_hslen
4987 * is always <= in_msglen in a sensible communication.
4988 * Again, it's wrong for DTLS handshake fragmentation.
4989 * The following check is therefore mandatory, and
4990 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004991 * Additionally, ssl->in_hslen might be arbitrarily out of
4992 * bounds after handling a DTLS message with an unexpected
4993 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004994 */
4995 if( ssl->in_hslen < ssl->in_msglen )
4996 {
4997 ssl->in_msglen -= ssl->in_hslen;
4998 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4999 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005000
Hanno Becker4a810fb2017-05-24 16:27:30 +01005001 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5002 ssl->in_msg, ssl->in_msglen );
5003 }
5004 else
5005 {
5006 ssl->in_msglen = 0;
5007 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005008
Hanno Becker4a810fb2017-05-24 16:27:30 +01005009 ssl->in_hslen = 0;
5010 }
5011 /* Case (4): Application data */
5012 else if( ssl->in_offt != NULL )
5013 {
5014 return( 0 );
5015 }
5016 /* Everything else (CCS & Alerts) */
5017 else
5018 {
5019 ssl->in_msglen = 0;
5020 }
5021
Hanno Becker1097b342018-08-15 14:09:41 +01005022 return( 0 );
5023}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005024
Hanno Beckere74d5562018-08-15 14:26:08 +01005025static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5026{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005027 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005028 return( 1 );
5029
5030 return( 0 );
5031}
5032
Hanno Becker5f066e72018-08-16 14:56:31 +01005033#if defined(MBEDTLS_SSL_PROTO_DTLS)
5034
5035static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5036{
5037 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5038 if( hs == NULL )
5039 return;
5040
Hanno Becker01315ea2018-08-21 17:22:17 +01005041 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005042 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005043 hs->buffering.total_bytes_buffered -=
5044 hs->buffering.future_record.len;
5045
5046 mbedtls_free( hs->buffering.future_record.data );
5047 hs->buffering.future_record.data = NULL;
5048 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005049}
5050
5051static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5052{
5053 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5054 unsigned char * rec;
5055 size_t rec_len;
5056 unsigned rec_epoch;
5057
5058 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5059 return( 0 );
5060
5061 if( hs == NULL )
5062 return( 0 );
5063
Hanno Becker5f066e72018-08-16 14:56:31 +01005064 rec = hs->buffering.future_record.data;
5065 rec_len = hs->buffering.future_record.len;
5066 rec_epoch = hs->buffering.future_record.epoch;
5067
5068 if( rec == NULL )
5069 return( 0 );
5070
Hanno Becker4cb782d2018-08-20 11:19:05 +01005071 /* Only consider loading future records if the
5072 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005073 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005074 return( 0 );
5075
Hanno Becker5f066e72018-08-16 14:56:31 +01005076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5077
5078 if( rec_epoch != ssl->in_epoch )
5079 {
5080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5081 goto exit;
5082 }
5083
5084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5085
5086 /* Double-check that the record is not too large */
5087 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5088 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5089 {
5090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5092 }
5093
5094 memcpy( ssl->in_hdr, rec, rec_len );
5095 ssl->in_left = rec_len;
5096 ssl->next_record_offset = 0;
5097
5098 ssl_free_buffered_record( ssl );
5099
5100exit:
5101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5102 return( 0 );
5103}
5104
5105static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5106{
5107 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5108 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005109 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005110
5111 /* Don't buffer future records outside handshakes. */
5112 if( hs == NULL )
5113 return( 0 );
5114
5115 /* Only buffer handshake records (we are only interested
5116 * in Finished messages). */
5117 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5118 return( 0 );
5119
5120 /* Don't buffer more than one future epoch record. */
5121 if( hs->buffering.future_record.data != NULL )
5122 return( 0 );
5123
Hanno Becker01315ea2018-08-21 17:22:17 +01005124 /* Don't buffer record if there's not enough buffering space remaining. */
5125 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5126 hs->buffering.total_bytes_buffered ) )
5127 {
5128 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",
5129 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5130 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005131 return( 0 );
5132 }
5133
Hanno Becker5f066e72018-08-16 14:56:31 +01005134 /* Buffer record */
5135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5136 ssl->in_epoch + 1 ) );
5137 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5138 rec_hdr_len + ssl->in_msglen );
5139
5140 /* ssl_parse_record_header() only considers records
5141 * of the next epoch as candidates for buffering. */
5142 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005143 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005144
5145 hs->buffering.future_record.data =
5146 mbedtls_calloc( 1, hs->buffering.future_record.len );
5147 if( hs->buffering.future_record.data == NULL )
5148 {
5149 /* If we run out of RAM trying to buffer a
5150 * record from the next epoch, just ignore. */
5151 return( 0 );
5152 }
5153
Hanno Becker01315ea2018-08-21 17:22:17 +01005154 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005155
Hanno Becker01315ea2018-08-21 17:22:17 +01005156 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005157 return( 0 );
5158}
5159
5160#endif /* MBEDTLS_SSL_PROTO_DTLS */
5161
Hanno Beckere74d5562018-08-15 14:26:08 +01005162static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005163{
5164 int ret;
5165
Hanno Becker5f066e72018-08-16 14:56:31 +01005166#if defined(MBEDTLS_SSL_PROTO_DTLS)
5167 /* We might have buffered a future record; if so,
5168 * and if the epoch matches now, load it.
5169 * On success, this call will set ssl->in_left to
5170 * the length of the buffered record, so that
5171 * the calls to ssl_fetch_input() below will
5172 * essentially be no-ops. */
5173 ret = ssl_load_buffered_record( ssl );
5174 if( ret != 0 )
5175 return( ret );
5176#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005181 return( ret );
5182 }
5183
5184 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5188 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005189 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005190 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5191 {
5192 ret = ssl_buffer_future_record( ssl );
5193 if( ret != 0 )
5194 return( ret );
5195
5196 /* Fall through to handling of unexpected records */
5197 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5198 }
5199
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005200 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5201 {
5202 /* Skip unexpected record (but not whole datagram) */
5203 ssl->next_record_offset = ssl->in_msglen
5204 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005205
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5207 "(header)" ) );
5208 }
5209 else
5210 {
5211 /* Skip invalid record and the rest of the datagram */
5212 ssl->next_record_offset = 0;
5213 ssl->in_left = 0;
5214
5215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5216 "(header)" ) );
5217 }
5218
5219 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005220 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005221 }
5222#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005223 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005224 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005225
5226 /*
5227 * Read and optionally decrypt the message contents
5228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5230 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005233 return( ret );
5234 }
5235
5236 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005238 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005241 if( ssl->next_record_offset < ssl->in_left )
5242 {
5243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5244 }
5245 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005246 else
5247#endif
5248 ssl->in_left = 0;
5249
5250 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005253 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005254 {
5255 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5257 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005258 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005259 /* Except when waiting for Finished as a bad mac here
5260 * probably means something went wrong in the handshake
5261 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5262 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5263 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5264 {
5265#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5266 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5267 {
5268 mbedtls_ssl_send_alert_message( ssl,
5269 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5270 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5271 }
5272#endif
5273 return( ret );
5274 }
5275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005277 if( ssl->conf->badmac_limit != 0 &&
5278 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5281 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005282 }
5283#endif
5284
Hanno Becker4a810fb2017-05-24 16:27:30 +01005285 /* As above, invalid records cause
5286 * dismissal of the whole datagram. */
5287
5288 ssl->next_record_offset = 0;
5289 ssl->in_left = 0;
5290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005292 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005293 }
5294
5295 return( ret );
5296 }
5297 else
5298#endif
5299 {
5300 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5302 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005304 mbedtls_ssl_send_alert_message( ssl,
5305 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5306 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005307 }
5308#endif
5309 return( ret );
5310 }
5311 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005312
Simon Butcher99000142016-10-13 17:21:01 +01005313 return( 0 );
5314}
5315
5316int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5317{
5318 int ret;
5319
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005320 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005321 * Handle particular types of records
5322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 {
Simon Butcher99000142016-10-13 17:21:01 +01005325 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5326 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005327 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005329 }
5330
Hanno Beckere678eaa2018-08-21 14:57:46 +01005331 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005332 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005333 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005334 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5336 ssl->in_msglen ) );
5337 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005338 }
5339
Hanno Beckere678eaa2018-08-21 14:57:46 +01005340 if( ssl->in_msg[0] != 1 )
5341 {
5342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5343 ssl->in_msg[0] ) );
5344 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5345 }
5346
5347#if defined(MBEDTLS_SSL_PROTO_DTLS)
5348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5349 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5350 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5351 {
5352 if( ssl->handshake == NULL )
5353 {
5354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5355 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5356 }
5357
5358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5359 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5360 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005361#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005362 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005365 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005366 if( ssl->in_msglen != 2 )
5367 {
5368 /* Note: Standard allows for more than one 2 byte alert
5369 to be packed in a single message, but Mbed TLS doesn't
5370 currently support this. */
5371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5372 ssl->in_msglen ) );
5373 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5374 }
5375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005377 ssl->in_msg[0], ssl->in_msg[1] ) );
5378
5379 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005380 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005385 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005387 }
5388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5390 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5393 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005394 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005395
5396#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5397 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5398 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5399 {
Hanno Becker90333da2017-10-10 11:27:13 +01005400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005401 /* Will be handled when trying to parse ServerHello */
5402 return( 0 );
5403 }
5404#endif
5405
5406#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5407 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5408 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5409 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5410 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5411 {
5412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5413 /* Will be handled in mbedtls_ssl_parse_certificate() */
5414 return( 0 );
5415 }
5416#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5417
5418 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005419 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005420 }
5421
Hanno Beckerc76c6192017-06-06 10:03:17 +01005422#if defined(MBEDTLS_SSL_PROTO_DTLS)
5423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5424 ssl->handshake != NULL &&
5425 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5426 {
5427 ssl_handshake_wrapup_free_hs_transform( ssl );
5428 }
5429#endif
5430
Paul Bakker5121ce52009-01-03 21:22:43 +00005431 return( 0 );
5432}
5433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005435{
5436 int ret;
5437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5439 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5440 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005441 {
5442 return( ret );
5443 }
5444
5445 return( 0 );
5446}
5447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005449 unsigned char level,
5450 unsigned char message )
5451{
5452 int ret;
5453
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005454 if( ssl == NULL || ssl->conf == NULL )
5455 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005461 ssl->out_msglen = 2;
5462 ssl->out_msg[0] = level;
5463 ssl->out_msg[1] = message;
5464
Hanno Becker67bc7c32018-08-06 11:33:50 +01005465 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005468 return( ret );
5469 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005471
5472 return( 0 );
5473}
5474
Paul Bakker5121ce52009-01-03 21:22:43 +00005475/*
5476 * Handshake functions
5477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5479 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5480 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5481 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5482 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5483 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5484 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005485/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5493 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005494 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5495 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005498 ssl->state++;
5499 return( 0 );
5500 }
5501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005504}
5505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5513 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005514 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5515 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005518 ssl->state++;
5519 return( 0 );
5520 }
5521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005524}
Gilles Peskinef9828522017-05-03 12:28:43 +02005525
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005526#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005527/* Some certificate support -> implement write and parse */
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005530{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005532 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 const mbedtls_x509_crt *crt;
5534 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5539 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005540 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5541 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005544 ssl->state++;
5545 return( 0 );
5546 }
5547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005550 {
5551 if( ssl->client_auth == 0 )
5552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005554 ssl->state++;
5555 return( 0 );
5556 }
5557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005559 /*
5560 * If using SSLv3 and got no cert, send an Alert message
5561 * (otherwise an empty Certificate message will be sent).
5562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5564 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005565 {
5566 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5568 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5569 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 goto write_msg;
5573 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576#endif /* MBEDTLS_SSL_CLI_C */
5577#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005578 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5583 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005584 }
5585 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005586#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005589
5590 /*
5591 * 0 . 0 handshake type
5592 * 1 . 3 handshake length
5593 * 4 . 6 length of all certs
5594 * 7 . 9 length of cert. 1
5595 * 10 . n-1 peer certificate
5596 * n . n+2 length of cert. 2
5597 * n+3 . ... upper level cert, etc.
5598 */
5599 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005601
Paul Bakker29087132010-03-21 21:03:34 +00005602 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 {
5604 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005605 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005608 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005610 }
5611
5612 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5613 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5614 ssl->out_msg[i + 2] = (unsigned char)( n );
5615
5616 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5617 i += n; crt = crt->next;
5618 }
5619
5620 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5621 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5622 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5623
5624 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5626 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005627
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005628#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005629write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005630#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005631
5632 ssl->state++;
5633
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005634 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005637 return( ret );
5638 }
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641
Paul Bakkered27a042013-04-18 22:46:23 +02005642 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005643}
5644
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005645/*
5646 * Once the certificate message is read, parse it into a cert chain and
5647 * perform basic checks, but leave actual verification to the caller
5648 */
5649static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005650{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005651 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005652 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005653 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655#if defined(MBEDTLS_SSL_SRV_C)
5656#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 /*
5658 * Check if the client sent an empty certificate
5659 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005662 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005663 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5665 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5666 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005669
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005670 /* The client was asked for a certificate but didn't send
5671 one. The client should know what's going on, so we
5672 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005673 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005674 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005675 }
5676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5680 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5685 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5686 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5687 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005690
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005691 /* The client was asked for a certificate but didn't send
5692 one. The client should know what's going on, so we
5693 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005694 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005695 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005696 }
5697 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5699 MBEDTLS_SSL_PROTO_TLS1_2 */
5700#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5706 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005708 }
5709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5711 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005714 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 }
5718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005720
Paul Bakker5121ce52009-01-03 21:22:43 +00005721 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005723 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005724 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005725
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005726 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005730 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5731 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005733 }
5734
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005735 /* In case we tried to reuse a session but it failed */
5736 if( ssl->session_negotiate->peer_cert != NULL )
5737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5739 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005740 }
5741
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005742 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005744 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5748 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005749 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005750 }
5751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005753
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005754 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005755
5756 while( i < ssl->in_hslen )
5757 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005758 if ( i + 3 > ssl->in_hslen ) {
5759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5760 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5761 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5762 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005764 if( ssl->in_msg[i] != 0 )
5765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005767 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5768 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005770 }
5771
5772 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5773 | (unsigned int) ssl->in_msg[i + 2];
5774 i += 3;
5775
5776 if( n < 128 || i + n > ssl->in_hslen )
5777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005779 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5780 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 }
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005785 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005786 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005787 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005788 case 0: /*ok*/
5789 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5790 /* Ignore certificate with an unknown algorithm: maybe a
5791 prior certificate was already trusted. */
5792 break;
5793
5794 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5795 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5796 goto crt_parse_der_failed;
5797
5798 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5799 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5800 goto crt_parse_der_failed;
5801
5802 default:
5803 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5804 crt_parse_der_failed:
5805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005807 return( ret );
5808 }
5809
5810 i += n;
5811 }
5812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005814
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005815 /*
5816 * On client, make sure the server cert doesn't change during renego to
5817 * avoid "triple handshake" attack: https://secure-resumption.com/
5818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005820 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005822 {
5823 if( ssl->session->peer_cert == NULL )
5824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005826 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5827 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005829 }
5830
5831 if( ssl->session->peer_cert->raw.len !=
5832 ssl->session_negotiate->peer_cert->raw.len ||
5833 memcmp( ssl->session->peer_cert->raw.p,
5834 ssl->session_negotiate->peer_cert->raw.p,
5835 ssl->session->peer_cert->raw.len ) != 0 )
5836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5839 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005841 }
5842 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005844
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005845 return( 0 );
5846}
5847
5848int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5849{
5850 int ret;
5851 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5852 ssl->transform_negotiate->ciphersuite_info;
5853#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5854 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5855 ? ssl->handshake->sni_authmode
5856 : ssl->conf->authmode;
5857#else
5858 const int authmode = ssl->conf->authmode;
5859#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005860 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005861
5862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5863
5864 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5865 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5866 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5867 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5868 {
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5870 ssl->state++;
5871 return( 0 );
5872 }
5873
5874#if defined(MBEDTLS_SSL_SRV_C)
5875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5876 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5877 {
5878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5879 ssl->state++;
5880 return( 0 );
5881 }
5882
5883 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5884 authmode == MBEDTLS_SSL_VERIFY_NONE )
5885 {
5886 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005888
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005889 ssl->state++;
5890 return( 0 );
5891 }
5892#endif
5893
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005894#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5895 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005896 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005897 {
5898 goto crt_verify;
5899 }
5900#endif
5901
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005902 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005903 {
5904 /* mbedtls_ssl_read_record may have sent an alert already. We
5905 let it decide whether to alert. */
5906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5907 return( ret );
5908 }
5909
5910 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5911 {
5912#if defined(MBEDTLS_SSL_SRV_C)
5913 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5914 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5915 {
5916 ret = 0;
5917 }
5918#endif
5919
5920 ssl->state++;
5921 return( ret );
5922 }
5923
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005924#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5925 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005926 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005927
5928crt_verify:
5929 if( ssl->handshake->ecrs_enabled)
5930 rs_ctx = &ssl->handshake->ecrs_ctx;
5931#endif
5932
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005933 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005934 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005935 mbedtls_x509_crt *ca_chain;
5936 mbedtls_x509_crl *ca_crl;
5937
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005938#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005939 if( ssl->handshake->sni_ca_chain != NULL )
5940 {
5941 ca_chain = ssl->handshake->sni_ca_chain;
5942 ca_crl = ssl->handshake->sni_ca_crl;
5943 }
5944 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005945#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005946 {
5947 ca_chain = ssl->conf->ca_chain;
5948 ca_crl = ssl->conf->ca_crl;
5949 }
5950
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005951 /*
5952 * Main check: verify certificate
5953 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005954 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005955 ssl->session_negotiate->peer_cert,
5956 ca_chain, ca_crl,
5957 ssl->conf->cert_profile,
5958 ssl->hostname,
5959 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005960 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005961
5962 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005965 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005966
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005967#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5968 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005969 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005970#endif
5971
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005972 /*
5973 * Secondary checks: always done, but change 'ret' only if it was 0
5974 */
5975
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005976#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005979
5980 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005982 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005983 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005984 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005987 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005989 }
5990 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005991#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005994 ciphersuite_info,
5995 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005996 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005999 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006001 }
6002
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006003 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6004 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6005 * with details encoded in the verification flags. All other kinds
6006 * of error codes, including those from the user provided f_vrfy
6007 * functions, are treated as fatal and lead to a failure of
6008 * ssl_parse_certificate even if verification was optional. */
6009 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6010 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6011 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6012 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006013 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006014 }
6015
6016 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6017 {
6018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6019 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6020 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006021
6022 if( ret != 0 )
6023 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006024 uint8_t alert;
6025
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006026 /* The certificate may have been rejected for several reasons.
6027 Pick one and send the corresponding alert. Which alert to send
6028 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006029 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6030 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6031 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6032 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6033 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6034 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6035 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6036 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6037 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6038 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6039 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6040 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6041 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6042 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6043 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6044 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6045 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6046 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6047 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6048 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006049 else
6050 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006051 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6052 alert );
6053 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006054
Hanno Beckere6706e62017-05-15 16:05:15 +01006055#if defined(MBEDTLS_DEBUG_C)
6056 if( ssl->session_negotiate->verify_result != 0 )
6057 {
6058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6059 ssl->session_negotiate->verify_result ) );
6060 }
6061 else
6062 {
6063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6064 }
6065#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006066 }
6067
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006068 ssl->state++;
6069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006071
6072 return( ret );
6073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6075 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6076 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6077 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6078 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6079 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6080 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006083{
6084 int ret;
6085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006089 ssl->out_msglen = 1;
6090 ssl->out_msg[0] = 1;
6091
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 ssl->state++;
6093
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006094 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097 return( ret );
6098 }
6099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006101
6102 return( 0 );
6103}
6104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006106{
6107 int ret;
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006110
Hanno Becker327c93b2018-08-15 13:56:18 +01006111 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 return( ret );
6115 }
6116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6121 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 }
6124
Hanno Beckere678eaa2018-08-21 14:57:46 +01006125 /* CCS records are only accepted if they have length 1 and content '1',
6126 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006127
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006128 /*
6129 * Switch to our negotiated transform and session parameters for inbound
6130 * data.
6131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006133 ssl->transform_in = ssl->transform_negotiate;
6134 ssl->session_in = ssl->session_negotiate;
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006137 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006140 ssl_dtls_replay_reset( ssl );
6141#endif
6142
6143 /* Increment epoch */
6144 if( ++ssl->in_epoch == 0 )
6145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006147 /* This is highly unlikely to happen for legitimate reasons, so
6148 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006150 }
6151 }
6152 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006154 memset( ssl->in_ctr, 0, 8 );
6155
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006156 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6159 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006164 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6165 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006167 }
6168 }
6169#endif
6170
Paul Bakker5121ce52009-01-03 21:22:43 +00006171 ssl->state++;
6172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006174
6175 return( 0 );
6176}
6177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6179 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006180{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006181 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6184 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6185 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006186 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006187 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006188#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6190#if defined(MBEDTLS_SHA512_C)
6191 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006192 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6193 else
6194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195#if defined(MBEDTLS_SHA256_C)
6196 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006197 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006198 else
6199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006203 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006204 }
Paul Bakker380da532012-04-18 16:10:25 +00006205}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6210 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006211 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6212 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6215#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006216#if defined(MBEDTLS_USE_PSA_CRYPTO)
6217 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6218#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006219 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006220#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006224 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006225#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006226 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006227#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006230}
6231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006233 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6236 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006237 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6238 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6241#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006242#if defined(MBEDTLS_USE_PSA_CRYPTO)
6243 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6244#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006245 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006246#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006249#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006250 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006251#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006252 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006253#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006254#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006256}
6257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6259 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6260static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006261 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006262{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006263 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6264 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006265}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006266#endif
Paul Bakker380da532012-04-18 16:10:25 +00006267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6269#if defined(MBEDTLS_SHA256_C)
6270static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006271 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006272{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006273#if defined(MBEDTLS_USE_PSA_CRYPTO)
6274 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6275#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006276 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006277#endif
Paul Bakker380da532012-04-18 16:10:25 +00006278}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006279#endif
Paul Bakker380da532012-04-18 16:10:25 +00006280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281#if defined(MBEDTLS_SHA512_C)
6282static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006283 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006284{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006285#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006286 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006287#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006288 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006289#endif
Paul Bakker380da532012-04-18 16:10:25 +00006290}
Paul Bakker769075d2012-11-24 11:26:46 +01006291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006295static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006297{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006298 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006299 mbedtls_md5_context md5;
6300 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006301
Paul Bakker5121ce52009-01-03 21:22:43 +00006302 unsigned char padbuf[48];
6303 unsigned char md5sum[16];
6304 unsigned char sha1sum[20];
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006307 if( !session )
6308 session = ssl->session;
6309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006311
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006312 mbedtls_md5_init( &md5 );
6313 mbedtls_sha1_init( &sha1 );
6314
6315 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6316 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006317
6318 /*
6319 * SSLv3:
6320 * hash =
6321 * MD5( master + pad2 +
6322 * MD5( handshake + sender + master + pad1 ) )
6323 * + SHA1( master + pad2 +
6324 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006325 */
6326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006328 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6329 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006333 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6334 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006335#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006338 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
Paul Bakker1ef83d62012-04-11 12:09:53 +00006340 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006341
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006342 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6343 mbedtls_md5_update_ret( &md5, session->master, 48 );
6344 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6345 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006346
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006347 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6348 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6349 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6350 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006351
Paul Bakker1ef83d62012-04-11 12:09:53 +00006352 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006353
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006354 mbedtls_md5_starts_ret( &md5 );
6355 mbedtls_md5_update_ret( &md5, session->master, 48 );
6356 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6357 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6358 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006359
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006360 mbedtls_sha1_starts_ret( &sha1 );
6361 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6362 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6363 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6364 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006367
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006368 mbedtls_md5_free( &md5 );
6369 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006370
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006371 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6372 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6373 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006380static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006382{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006383 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006384 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006385 mbedtls_md5_context md5;
6386 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006387 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006390 if( !session )
6391 session = ssl->session;
6392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006394
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006395 mbedtls_md5_init( &md5 );
6396 mbedtls_sha1_init( &sha1 );
6397
6398 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6399 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006400
Paul Bakker1ef83d62012-04-11 12:09:53 +00006401 /*
6402 * TLSv1:
6403 * hash = PRF( master, finished_label,
6404 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6405 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006408 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6409 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006410#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006413 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6414 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006415#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006418 ? "client finished"
6419 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006420
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006421 mbedtls_md5_finish_ret( &md5, padbuf );
6422 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006423
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006424 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006425 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006428
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006429 mbedtls_md5_free( &md5 );
6430 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006431
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006432 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006436#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6439#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006440static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006442{
6443 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006444 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006445 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006446#if defined(MBEDTLS_USE_PSA_CRYPTO)
6447 size_t hash_size;
6448 psa_hash_operation_t sha256_psa;
6449 psa_status_t status;
6450#else
6451 mbedtls_sha256_context sha256;
6452#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006455 if( !session )
6456 session = ssl->session;
6457
Andrzej Kurekeb342242019-01-29 09:14:33 -05006458 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6459 ? "client finished"
6460 : "server finished";
6461
6462#if defined(MBEDTLS_USE_PSA_CRYPTO)
6463 sha256_psa = psa_hash_operation_init();
6464
6465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6466
6467 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6468 if( status != PSA_SUCCESS )
6469 {
6470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6471 return;
6472 }
6473
6474 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6475 if( status != PSA_SUCCESS )
6476 {
6477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6478 return;
6479 }
6480 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6481#else
6482
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006483 mbedtls_sha256_init( &sha256 );
6484
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006486
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006487 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006488
6489 /*
6490 * TLSv1.2:
6491 * hash = PRF( master, finished_label,
6492 * Hash( handshake ) )[0.11]
6493 */
6494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495#if !defined(MBEDTLS_SHA256_ALT)
6496 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006497 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006498#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006499
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006500 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006501 mbedtls_sha256_free( &sha256 );
6502#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006503
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006504 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006505 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006508
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006509 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006516static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006518{
6519 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006520 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006521 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006522#if defined(MBEDTLS_USE_PSA_CRYPTO)
6523 size_t hash_size;
Andrzej Kurek972fba52019-01-30 03:29:12 -05006524 psa_hash_operation_t sha384_psa;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006525 psa_status_t status;
6526#else
6527 mbedtls_sha512_context sha512;
6528#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006531 if( !session )
6532 session = ssl->session;
6533
Andrzej Kurekeb342242019-01-29 09:14:33 -05006534 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6535 ? "client finished"
6536 : "server finished";
6537
6538#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006539 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006540
6541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6542
Andrzej Kurek972fba52019-01-30 03:29:12 -05006543 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006544 if( status != PSA_SUCCESS )
6545 {
6546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6547 return;
6548 }
6549
Andrzej Kurek972fba52019-01-30 03:29:12 -05006550 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006551 if( status != PSA_SUCCESS )
6552 {
6553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6554 return;
6555 }
6556 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6557#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006558 mbedtls_sha512_init( &sha512 );
6559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006561
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006562 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006563
6564 /*
6565 * TLSv1.2:
6566 * hash = PRF( master, finished_label,
6567 * Hash( handshake ) )[0.11]
6568 */
6569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006571 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6572 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006573#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006574
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006575 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006576 mbedtls_sha512_free( &sha512 );
6577#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006578
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006579 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006580 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006583
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006584 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588#endif /* MBEDTLS_SHA512_C */
6589#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006592{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006594
6595 /*
6596 * Free our handshake params
6597 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006598 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006600 ssl->handshake = NULL;
6601
6602 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006603 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006604 */
6605 if( ssl->transform )
6606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 mbedtls_ssl_transform_free( ssl->transform );
6608 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006609 }
6610 ssl->transform = ssl->transform_negotiate;
6611 ssl->transform_negotiate = NULL;
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006614}
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006617{
6618 int resume = ssl->handshake->resume;
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622#if defined(MBEDTLS_SSL_RENEGOTIATION)
6623 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006626 ssl->renego_records_seen = 0;
6627 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006628#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006629
6630 /*
6631 * Free the previous session and switch in the current one
6632 */
Paul Bakker0a597072012-09-25 21:55:46 +00006633 if( ssl->session )
6634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006636 /* RFC 7366 3.1: keep the EtM state */
6637 ssl->session_negotiate->encrypt_then_mac =
6638 ssl->session->encrypt_then_mac;
6639#endif
6640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 mbedtls_ssl_session_free( ssl->session );
6642 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006643 }
6644 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006645 ssl->session_negotiate = NULL;
6646
Paul Bakker0a597072012-09-25 21:55:46 +00006647 /*
6648 * Add cache entry
6649 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006650 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006651 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006652 resume == 0 )
6653 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006654 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006656 }
Paul Bakker0a597072012-09-25 21:55:46 +00006657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006659 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006660 ssl->handshake->flight != NULL )
6661 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006662 /* Cancel handshake timer */
6663 ssl_set_timer( ssl, 0 );
6664
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006665 /* Keep last flight around in case we need to resend it:
6666 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006668 }
6669 else
6670#endif
6671 ssl_handshake_wrapup_free_hs_transform( ssl );
6672
Paul Bakker48916f92012-09-16 19:57:18 +00006673 ssl->state++;
6674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006676}
6677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006679{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006680 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006683
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006684 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006685
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006686 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006687
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006688 /*
6689 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6690 * may define some other value. Currently (early 2016), no defined
6691 * ciphersuite does this (and this is unlikely to change as activity has
6692 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006697 ssl->verify_data_len = hash_len;
6698 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006699#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006700
Paul Bakker5121ce52009-01-03 21:22:43 +00006701 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6703 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006704
6705 /*
6706 * In case of session resuming, invert the client and server
6707 * ChangeCipherSpec messages order.
6708 */
Paul Bakker0a597072012-09-25 21:55:46 +00006709 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006712 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006718#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006719 }
6720 else
6721 ssl->state++;
6722
Paul Bakker48916f92012-09-16 19:57:18 +00006723 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006724 * Switch to our negotiated transform and session parameters for outbound
6725 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006731 {
6732 unsigned char i;
6733
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006734 /* Remember current epoch settings for resending */
6735 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006736 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006737
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006738 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006739 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006740
6741 /* Increment epoch */
6742 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006743 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006744 break;
6745
6746 /* The loop goes to its end iff the counter is wrapping */
6747 if( i == 0 )
6748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6750 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006751 }
6752 }
6753 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006755 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006756
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006757 ssl->transform_out = ssl->transform_negotiate;
6758 ssl->session_out = ssl->session_negotiate;
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6761 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6766 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006767 }
6768 }
6769#endif
6770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006772 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006774#endif
6775
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006776 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006778 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 return( ret );
6780 }
6781
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006782#if defined(MBEDTLS_SSL_PROTO_DTLS)
6783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6784 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6785 {
6786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6787 return( ret );
6788 }
6789#endif
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006792
6793 return( 0 );
6794}
6795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006797#define SSL_MAX_HASH_LEN 36
6798#else
6799#define SSL_MAX_HASH_LEN 12
6800#endif
6801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006803{
Paul Bakker23986e52011-04-24 08:57:21 +00006804 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006805 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006806 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006809
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006810 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006811
Hanno Becker327c93b2018-08-15 13:56:18 +01006812 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 return( ret );
6816 }
6817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006821 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6822 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006824 }
6825
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006826 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#if defined(MBEDTLS_SSL_PROTO_SSL3)
6828 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006829 hash_len = 36;
6830 else
6831#endif
6832 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6835 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6839 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006841 }
6842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006844 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006847 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6848 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006850 }
6851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006853 ssl->verify_data_len = hash_len;
6854 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006855#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006856
Paul Bakker0a597072012-09-25 21:55:46 +00006857 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006860 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006864 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006866#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006867 }
6868 else
6869 ssl->state++;
6870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006872 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006874#endif
6875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006877
6878 return( 0 );
6879}
6880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006882{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6886 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6887 mbedtls_md5_init( &handshake->fin_md5 );
6888 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006889 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6890 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006891#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6893#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006894#if defined(MBEDTLS_USE_PSA_CRYPTO)
6895 handshake->fin_sha256_psa = psa_hash_operation_init();
6896 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6897#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006899 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006900#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006903#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006904 handshake->fin_sha384_psa = psa_hash_operation_init();
6905 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006906#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006908 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006909#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006912
6913 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006914
6915#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6916 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6917 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6918#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#if defined(MBEDTLS_DHM_C)
6921 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006922#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_ECDH_C)
6924 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006925#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006926#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006927 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006928#if defined(MBEDTLS_SSL_CLI_C)
6929 handshake->ecjpake_cache = NULL;
6930 handshake->ecjpake_cache_len = 0;
6931#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006932#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006933
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006934#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006935 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006936#endif
6937
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006938#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6939 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6940#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006941}
6942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006944{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6948 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 mbedtls_md_init( &transform->md_ctx_enc );
6951 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006952}
6953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006957}
6958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006960{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006961 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006962 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006964 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006966 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006967 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006968
6969 /*
6970 * Either the pointers are now NULL or cleared properly and can be freed.
6971 * Now allocate missing structures.
6972 */
6973 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006974 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006975 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006976 }
Paul Bakker48916f92012-09-16 19:57:18 +00006977
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006978 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006979 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006980 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006981 }
Paul Bakker48916f92012-09-16 19:57:18 +00006982
Paul Bakker82788fb2014-10-20 13:59:19 +02006983 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006984 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006985 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006986 }
Paul Bakker48916f92012-09-16 19:57:18 +00006987
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006988 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006989 if( ssl->handshake == NULL ||
6990 ssl->transform_negotiate == NULL ||
6991 ssl->session_negotiate == NULL )
6992 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 mbedtls_free( ssl->handshake );
6996 mbedtls_free( ssl->transform_negotiate );
6997 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006998
6999 ssl->handshake = NULL;
7000 ssl->transform_negotiate = NULL;
7001 ssl->session_negotiate = NULL;
7002
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007003 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007004 }
7005
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007006 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007008 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007009 ssl_handshake_params_init( ssl->handshake );
7010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007012 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7013 {
7014 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007015
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007016 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7017 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7018 else
7019 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007020
7021 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007022 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007023#endif
7024
Paul Bakker48916f92012-09-16 19:57:18 +00007025 return( 0 );
7026}
7027
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007028#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007029/* Dummy cookie callbacks for defaults */
7030static int ssl_cookie_write_dummy( void *ctx,
7031 unsigned char **p, unsigned char *end,
7032 const unsigned char *cli_id, size_t cli_id_len )
7033{
7034 ((void) ctx);
7035 ((void) p);
7036 ((void) end);
7037 ((void) cli_id);
7038 ((void) cli_id_len);
7039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007041}
7042
7043static int ssl_cookie_check_dummy( void *ctx,
7044 const unsigned char *cookie, size_t cookie_len,
7045 const unsigned char *cli_id, size_t cli_id_len )
7046{
7047 ((void) ctx);
7048 ((void) cookie);
7049 ((void) cookie_len);
7050 ((void) cli_id);
7051 ((void) cli_id_len);
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007054}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007055#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007056
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007057/* Once ssl->out_hdr as the address of the beginning of the
7058 * next outgoing record is set, deduce the other pointers.
7059 *
7060 * Note: For TLS, we save the implicit record sequence number
7061 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7062 * and the caller has to make sure there's space for this.
7063 */
7064
7065static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7066 mbedtls_ssl_transform *transform )
7067{
7068#if defined(MBEDTLS_SSL_PROTO_DTLS)
7069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7070 {
7071 ssl->out_ctr = ssl->out_hdr + 3;
7072 ssl->out_len = ssl->out_hdr + 11;
7073 ssl->out_iv = ssl->out_hdr + 13;
7074 }
7075 else
7076#endif
7077 {
7078 ssl->out_ctr = ssl->out_hdr - 8;
7079 ssl->out_len = ssl->out_hdr + 3;
7080 ssl->out_iv = ssl->out_hdr + 5;
7081 }
7082
7083 /* Adjust out_msg to make space for explicit IV, if used. */
7084 if( transform != NULL &&
7085 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7086 {
7087 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7088 }
7089 else
7090 ssl->out_msg = ssl->out_iv;
7091}
7092
7093/* Once ssl->in_hdr as the address of the beginning of the
7094 * next incoming record is set, deduce the other pointers.
7095 *
7096 * Note: For TLS, we save the implicit record sequence number
7097 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7098 * and the caller has to make sure there's space for this.
7099 */
7100
7101static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7102 mbedtls_ssl_transform *transform )
7103{
7104#if defined(MBEDTLS_SSL_PROTO_DTLS)
7105 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7106 {
7107 ssl->in_ctr = ssl->in_hdr + 3;
7108 ssl->in_len = ssl->in_hdr + 11;
7109 ssl->in_iv = ssl->in_hdr + 13;
7110 }
7111 else
7112#endif
7113 {
7114 ssl->in_ctr = ssl->in_hdr - 8;
7115 ssl->in_len = ssl->in_hdr + 3;
7116 ssl->in_iv = ssl->in_hdr + 5;
7117 }
7118
7119 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7120 if( transform != NULL &&
7121 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7122 {
7123 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7124 }
7125 else
7126 ssl->in_msg = ssl->in_iv;
7127}
7128
Paul Bakker5121ce52009-01-03 21:22:43 +00007129/*
7130 * Initialize an SSL context
7131 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007132void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7133{
7134 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7135}
7136
7137/*
7138 * Setup an SSL context
7139 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007140
7141static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7142{
7143 /* Set the incoming and outgoing record pointers. */
7144#if defined(MBEDTLS_SSL_PROTO_DTLS)
7145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7146 {
7147 ssl->out_hdr = ssl->out_buf;
7148 ssl->in_hdr = ssl->in_buf;
7149 }
7150 else
7151#endif /* MBEDTLS_SSL_PROTO_DTLS */
7152 {
7153 ssl->out_hdr = ssl->out_buf + 8;
7154 ssl->in_hdr = ssl->in_buf + 8;
7155 }
7156
7157 /* Derive other internal pointers. */
7158 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7159 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7160}
7161
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007162int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007163 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007164{
Paul Bakker48916f92012-09-16 19:57:18 +00007165 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007166
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007167 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007168
7169 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007170 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007171 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007172
7173 /* Set to NULL in case of an error condition */
7174 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007175
Angus Grattond8213d02016-05-25 20:56:48 +10007176 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7177 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 {
Angus Grattond8213d02016-05-25 20:56:48 +10007179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007180 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007181 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007182 }
7183
7184 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7185 if( ssl->out_buf == NULL )
7186 {
7187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007188 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007189 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007190 }
7191
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007192 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007193
Paul Bakker48916f92012-09-16 19:57:18 +00007194 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007195 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007196
7197 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007198
7199error:
7200 mbedtls_free( ssl->in_buf );
7201 mbedtls_free( ssl->out_buf );
7202
7203 ssl->conf = NULL;
7204
7205 ssl->in_buf = NULL;
7206 ssl->out_buf = NULL;
7207
7208 ssl->in_hdr = NULL;
7209 ssl->in_ctr = NULL;
7210 ssl->in_len = NULL;
7211 ssl->in_iv = NULL;
7212 ssl->in_msg = NULL;
7213
7214 ssl->out_hdr = NULL;
7215 ssl->out_ctr = NULL;
7216 ssl->out_len = NULL;
7217 ssl->out_iv = NULL;
7218 ssl->out_msg = NULL;
7219
k-stachowiak9f7798e2018-07-31 16:52:32 +02007220 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007221}
7222
7223/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007224 * Reset an initialized and used SSL context for re-use while retaining
7225 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007226 *
7227 * If partial is non-zero, keep data in the input buffer and client ID.
7228 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007229 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007230static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007231{
Paul Bakker48916f92012-09-16 19:57:18 +00007232 int ret;
7233
Hanno Becker7e772132018-08-10 12:38:21 +01007234#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7235 !defined(MBEDTLS_SSL_SRV_C)
7236 ((void) partial);
7237#endif
7238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007240
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007241 /* Cancel any possibly running timer */
7242 ssl_set_timer( ssl, 0 );
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SSL_RENEGOTIATION)
7245 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007246 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007247
7248 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7250 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007253
Paul Bakker7eb013f2011-10-06 12:37:39 +00007254 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007255 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007256
7257 ssl->in_msgtype = 0;
7258 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007260 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007261 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007264 ssl_dtls_replay_reset( ssl );
7265#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007266
7267 ssl->in_hslen = 0;
7268 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007269
7270 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007271
7272 ssl->out_msgtype = 0;
7273 ssl->out_msglen = 0;
7274 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7276 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007277 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007278#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007279
Hanno Becker19859472018-08-06 09:40:20 +01007280 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7281
Paul Bakker48916f92012-09-16 19:57:18 +00007282 ssl->transform_in = NULL;
7283 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007284
Hanno Becker78640902018-08-13 16:35:15 +01007285 ssl->session_in = NULL;
7286 ssl->session_out = NULL;
7287
Angus Grattond8213d02016-05-25 20:56:48 +10007288 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007289
7290#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007291 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007292#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7293 {
7294 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007295 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007296 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7299 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7302 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7305 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007306 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007307 }
7308#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007309
Paul Bakker48916f92012-09-16 19:57:18 +00007310 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 mbedtls_ssl_transform_free( ssl->transform );
7313 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007314 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007315 }
Paul Bakker48916f92012-09-16 19:57:18 +00007316
Paul Bakkerc0463502013-02-14 11:19:38 +01007317 if( ssl->session )
7318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 mbedtls_ssl_session_free( ssl->session );
7320 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007321 ssl->session = NULL;
7322 }
7323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007325 ssl->alpn_chosen = NULL;
7326#endif
7327
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007328#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007329#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007330 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007331#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007332 {
7333 mbedtls_free( ssl->cli_id );
7334 ssl->cli_id = NULL;
7335 ssl->cli_id_len = 0;
7336 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007337#endif
7338
Paul Bakker48916f92012-09-16 19:57:18 +00007339 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7340 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007341
7342 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007343}
7344
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007345/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007346 * Reset an initialized and used SSL context for re-use while retaining
7347 * all application-set variables, function pointers and data.
7348 */
7349int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7350{
7351 return( ssl_session_reset_int( ssl, 0 ) );
7352}
7353
7354/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007355 * SSL set accessors
7356 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007357void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007358{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007359 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007360}
7361
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007362void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007363{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007364 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007365}
7366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007368void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007369{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007370 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007371}
7372#endif
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007375void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007376{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007377 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007378}
7379#endif
7380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007382
Hanno Becker1841b0a2018-08-24 11:13:57 +01007383void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7384 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007385{
7386 ssl->disable_datagram_packing = !allow_packing;
7387}
7388
7389void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7390 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007392 conf->hs_timeout_min = min;
7393 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007394}
7395#endif
7396
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007397void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007398{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007399 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007400}
7401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007403void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007404 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007405 void *p_vrfy )
7406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007407 conf->f_vrfy = f_vrfy;
7408 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007411
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007412void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007413 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 void *p_rng )
7415{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007416 conf->f_rng = f_rng;
7417 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007418}
7419
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007420void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007421 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 void *p_dbg )
7423{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007424 conf->f_dbg = f_dbg;
7425 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007426}
7427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007429 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007430 mbedtls_ssl_send_t *f_send,
7431 mbedtls_ssl_recv_t *f_recv,
7432 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007433{
7434 ssl->p_bio = p_bio;
7435 ssl->f_send = f_send;
7436 ssl->f_recv = f_recv;
7437 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007438}
7439
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007440#if defined(MBEDTLS_SSL_PROTO_DTLS)
7441void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7442{
7443 ssl->mtu = mtu;
7444}
7445#endif
7446
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007447void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007448{
7449 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007450}
7451
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007452void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7453 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007454 mbedtls_ssl_set_timer_t *f_set_timer,
7455 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007456{
7457 ssl->p_timer = p_timer;
7458 ssl->f_set_timer = f_set_timer;
7459 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007460
7461 /* Make sure we start with no timer running */
7462 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007463}
7464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007466void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007467 void *p_cache,
7468 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7469 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007470{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007471 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007472 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007473 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477#if defined(MBEDTLS_SSL_CLI_C)
7478int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007479{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007480 int ret;
7481
7482 if( ssl == NULL ||
7483 session == NULL ||
7484 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007485 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007488 }
7489
7490 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7491 return( ret );
7492
Paul Bakker0a597072012-09-25 21:55:46 +00007493 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007494
7495 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007498
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007499void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007500 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007502 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7503 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7504 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7505 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007506}
7507
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007508void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007509 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007510 int major, int minor )
7511{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007513 return;
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007516 return;
7517
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007518 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007519}
7520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007522void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007523 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007524{
7525 conf->cert_profile = profile;
7526}
7527
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007528/* Append a new keycert entry to a (possibly empty) list */
7529static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7530 mbedtls_x509_crt *cert,
7531 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007532{
niisato8ee24222018-06-25 19:05:48 +09007533 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007534
niisato8ee24222018-06-25 19:05:48 +09007535 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7536 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007537 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007538
niisato8ee24222018-06-25 19:05:48 +09007539 new_cert->cert = cert;
7540 new_cert->key = key;
7541 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007542
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007543 /* Update head is the list was null, else add to the end */
7544 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007545 {
niisato8ee24222018-06-25 19:05:48 +09007546 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007547 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007548 else
7549 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007550 mbedtls_ssl_key_cert *cur = *head;
7551 while( cur->next != NULL )
7552 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007553 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007554 }
7555
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007556 return( 0 );
7557}
7558
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007559int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007560 mbedtls_x509_crt *own_cert,
7561 mbedtls_pk_context *pk_key )
7562{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007563 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007564}
7565
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007566void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007567 mbedtls_x509_crt *ca_chain,
7568 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007569{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007570 conf->ca_chain = ca_chain;
7571 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007574
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007575#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7576int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7577 mbedtls_x509_crt *own_cert,
7578 mbedtls_pk_context *pk_key )
7579{
7580 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7581 own_cert, pk_key ) );
7582}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007583
7584void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7585 mbedtls_x509_crt *ca_chain,
7586 mbedtls_x509_crl *ca_crl )
7587{
7588 ssl->handshake->sni_ca_chain = ca_chain;
7589 ssl->handshake->sni_ca_crl = ca_crl;
7590}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007591
7592void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7593 int authmode )
7594{
7595 ssl->handshake->sni_authmode = authmode;
7596}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007597#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7598
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007599#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007600/*
7601 * Set EC J-PAKE password for current handshake
7602 */
7603int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7604 const unsigned char *pw,
7605 size_t pw_len )
7606{
7607 mbedtls_ecjpake_role role;
7608
Janos Follath8eb64132016-06-03 15:40:57 +01007609 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7611
7612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7613 role = MBEDTLS_ECJPAKE_SERVER;
7614 else
7615 role = MBEDTLS_ECJPAKE_CLIENT;
7616
7617 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7618 role,
7619 MBEDTLS_MD_SHA256,
7620 MBEDTLS_ECP_DP_SECP256R1,
7621 pw, pw_len ) );
7622}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007623#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007626
7627static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7628{
7629 /* Remove reference to existing PSK, if any. */
7630#if defined(MBEDTLS_USE_PSA_CRYPTO)
7631 if( conf->psk_opaque != 0 )
7632 {
7633 /* The maintenance of the PSK key slot is the
7634 * user's responsibility. */
7635 conf->psk_opaque = 0;
7636 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007637 /* This and the following branch should never
7638 * be taken simultaenously as we maintain the
7639 * invariant that raw and opaque PSKs are never
7640 * configured simultaneously. As a safeguard,
7641 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007642#endif /* MBEDTLS_USE_PSA_CRYPTO */
7643 if( conf->psk != NULL )
7644 {
7645 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7646
7647 mbedtls_free( conf->psk );
7648 conf->psk = NULL;
7649 conf->psk_len = 0;
7650 }
7651
7652 /* Remove reference to PSK identity, if any. */
7653 if( conf->psk_identity != NULL )
7654 {
7655 mbedtls_free( conf->psk_identity );
7656 conf->psk_identity = NULL;
7657 conf->psk_identity_len = 0;
7658 }
7659}
7660
Hanno Becker7390c712018-11-15 13:33:04 +00007661/* This function assumes that PSK identity in the SSL config is unset.
7662 * It checks that the provided identity is well-formed and attempts
7663 * to make a copy of it in the SSL config.
7664 * On failure, the PSK identity in the config remains unset. */
7665static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7666 unsigned char const *psk_identity,
7667 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007668{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007669 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007670 if( psk_identity == NULL ||
7671 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007672 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007673 {
7674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7675 }
7676
Hanno Becker7390c712018-11-15 13:33:04 +00007677 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7678 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007679 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007680
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007681 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007682 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007683
7684 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007685}
7686
Hanno Becker7390c712018-11-15 13:33:04 +00007687int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7688 const unsigned char *psk, size_t psk_len,
7689 const unsigned char *psk_identity, size_t psk_identity_len )
7690{
7691 int ret;
7692 /* Remove opaque/raw PSK + PSK Identity */
7693 ssl_conf_remove_psk( conf );
7694
7695 /* Check and set raw PSK */
7696 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7698 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7699 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7700 conf->psk_len = psk_len;
7701 memcpy( conf->psk, psk, conf->psk_len );
7702
7703 /* Check and set PSK Identity */
7704 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7705 if( ret != 0 )
7706 ssl_conf_remove_psk( conf );
7707
7708 return( ret );
7709}
7710
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007711static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7712{
7713#if defined(MBEDTLS_USE_PSA_CRYPTO)
7714 if( ssl->handshake->psk_opaque != 0 )
7715 {
7716 ssl->handshake->psk_opaque = 0;
7717 }
7718 else
7719#endif /* MBEDTLS_USE_PSA_CRYPTO */
7720 if( ssl->handshake->psk != NULL )
7721 {
7722 mbedtls_platform_zeroize( ssl->handshake->psk,
7723 ssl->handshake->psk_len );
7724 mbedtls_free( ssl->handshake->psk );
7725 ssl->handshake->psk_len = 0;
7726 }
7727}
7728
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007729int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7730 const unsigned char *psk, size_t psk_len )
7731{
7732 if( psk == NULL || ssl->handshake == NULL )
7733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7734
7735 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7737
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007738 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007739
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007740 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007741 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007742
7743 ssl->handshake->psk_len = psk_len;
7744 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7745
7746 return( 0 );
7747}
7748
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007749#if defined(MBEDTLS_USE_PSA_CRYPTO)
7750int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007751 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007752 const unsigned char *psk_identity,
7753 size_t psk_identity_len )
7754{
Hanno Becker7390c712018-11-15 13:33:04 +00007755 int ret;
7756 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007757 ssl_conf_remove_psk( conf );
7758
Hanno Becker7390c712018-11-15 13:33:04 +00007759 /* Check and set opaque PSK */
7760 if( psk_slot == 0 )
7761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007762 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007763
7764 /* Check and set PSK Identity */
7765 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7766 psk_identity_len );
7767 if( ret != 0 )
7768 ssl_conf_remove_psk( conf );
7769
7770 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007771}
7772
7773int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007774 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007775{
7776 if( psk_slot == 0 || ssl->handshake == NULL )
7777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7778
7779 ssl_remove_psk( ssl );
7780 ssl->handshake->psk_opaque = psk_slot;
7781 return( 0 );
7782}
7783#endif /* MBEDTLS_USE_PSA_CRYPTO */
7784
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007785void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007787 size_t),
7788 void *p_psk )
7789{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007790 conf->f_psk = f_psk;
7791 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007794
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007795#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007796
7797#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007798int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007799{
7800 int ret;
7801
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007802 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7803 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7804 {
7805 mbedtls_mpi_free( &conf->dhm_P );
7806 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007807 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007809
7810 return( 0 );
7811}
Hanno Becker470a8c42017-10-04 15:28:46 +01007812#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007813
Hanno Beckera90658f2017-10-04 15:29:08 +01007814int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7815 const unsigned char *dhm_P, size_t P_len,
7816 const unsigned char *dhm_G, size_t G_len )
7817{
7818 int ret;
7819
7820 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7821 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7822 {
7823 mbedtls_mpi_free( &conf->dhm_P );
7824 mbedtls_mpi_free( &conf->dhm_G );
7825 return( ret );
7826 }
7827
7828 return( 0 );
7829}
Paul Bakker5121ce52009-01-03 21:22:43 +00007830
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007831int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007832{
7833 int ret;
7834
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007835 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7836 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7837 {
7838 mbedtls_mpi_free( &conf->dhm_P );
7839 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007840 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007841 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007842
7843 return( 0 );
7844}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007845#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007846
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007847#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7848/*
7849 * Set the minimum length for Diffie-Hellman parameters
7850 */
7851void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7852 unsigned int bitlen )
7853{
7854 conf->dhm_min_bitlen = bitlen;
7855}
7856#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7857
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007858#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007859/*
7860 * Set allowed/preferred hashes for handshake signatures
7861 */
7862void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7863 const int *hashes )
7864{
7865 conf->sig_hashes = hashes;
7866}
Hanno Becker947194e2017-04-07 13:25:49 +01007867#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007868
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007869#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007870/*
7871 * Set the allowed elliptic curves
7872 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007873void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007874 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007875{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007876 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007877}
Hanno Becker947194e2017-04-07 13:25:49 +01007878#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007879
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007880#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007882{
Hanno Becker947194e2017-04-07 13:25:49 +01007883 /* Initialize to suppress unnecessary compiler warning */
7884 size_t hostname_len = 0;
7885
7886 /* Check if new hostname is valid before
7887 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007888 if( hostname != NULL )
7889 {
7890 hostname_len = strlen( hostname );
7891
7892 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7894 }
7895
7896 /* Now it's clear that we will overwrite the old hostname,
7897 * so we can free it safely */
7898
7899 if( ssl->hostname != NULL )
7900 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007901 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007902 mbedtls_free( ssl->hostname );
7903 }
7904
7905 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007906
Paul Bakker5121ce52009-01-03 21:22:43 +00007907 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007908 {
7909 ssl->hostname = NULL;
7910 }
7911 else
7912 {
7913 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007914 if( ssl->hostname == NULL )
7915 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007916
Hanno Becker947194e2017-04-07 13:25:49 +01007917 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007918
Hanno Becker947194e2017-04-07 13:25:49 +01007919 ssl->hostname[hostname_len] = '\0';
7920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007921
7922 return( 0 );
7923}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007924#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007925
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007926#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007927void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007929 const unsigned char *, size_t),
7930 void *p_sni )
7931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007932 conf->f_sni = f_sni;
7933 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007938int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007939{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007940 size_t cur_len, tot_len;
7941 const char **p;
7942
7943 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007944 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7945 * MUST NOT be truncated."
7946 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007947 */
7948 tot_len = 0;
7949 for( p = protos; *p != NULL; p++ )
7950 {
7951 cur_len = strlen( *p );
7952 tot_len += cur_len;
7953
7954 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007956 }
7957
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007958 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007959
7960 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007961}
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007964{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007965 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007968
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007969void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007971 conf->max_major_ver = major;
7972 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007973}
7974
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007975void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007976{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007977 conf->min_major_ver = major;
7978 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007979}
7980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007982void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007983{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007984 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007985}
7986#endif
7987
Janos Follath088ce432017-04-10 12:42:31 +01007988#if defined(MBEDTLS_SSL_SRV_C)
7989void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7990 char cert_req_ca_list )
7991{
7992 conf->cert_req_ca_list = cert_req_ca_list;
7993}
7994#endif
7995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007997void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007998{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007999 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008000}
8001#endif
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008004void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008005{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008006 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008007}
8008#endif
8009
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008010#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008011void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008012{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008013 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008014}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008015#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008018int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008021 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008024 }
8025
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008026 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008027
8028 return( 0 );
8029}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008033void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008034{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008035 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008040void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008041{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008042 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008043}
8044#endif
8045
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008046void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008048 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008049}
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008052void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008054 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008055}
8056
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008057void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008059 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008060}
8061
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008062void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008063 const unsigned char period[8] )
8064{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008065 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008070#if defined(MBEDTLS_SSL_CLI_C)
8071void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008072{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008073 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008074}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008075#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008076
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008077#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008078void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8079 mbedtls_ssl_ticket_write_t *f_ticket_write,
8080 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8081 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008082{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008083 conf->f_ticket_write = f_ticket_write;
8084 conf->f_ticket_parse = f_ticket_parse;
8085 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008086}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008089
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008090#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8091void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8092 mbedtls_ssl_export_keys_t *f_export_keys,
8093 void *p_export_keys )
8094{
8095 conf->f_export_keys = f_export_keys;
8096 conf->p_export_keys = p_export_keys;
8097}
8098#endif
8099
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008100#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008101void mbedtls_ssl_conf_async_private_cb(
8102 mbedtls_ssl_config *conf,
8103 mbedtls_ssl_async_sign_t *f_async_sign,
8104 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8105 mbedtls_ssl_async_resume_t *f_async_resume,
8106 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008107 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008108{
8109 conf->f_async_sign_start = f_async_sign;
8110 conf->f_async_decrypt_start = f_async_decrypt;
8111 conf->f_async_resume = f_async_resume;
8112 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008113 conf->p_async_config_data = async_config_data;
8114}
8115
Gilles Peskine8f97af72018-04-26 11:46:10 +02008116void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8117{
8118 return( conf->p_async_config_data );
8119}
8120
Gilles Peskine1febfef2018-04-30 11:54:39 +02008121void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008122{
8123 if( ssl->handshake == NULL )
8124 return( NULL );
8125 else
8126 return( ssl->handshake->user_async_ctx );
8127}
8128
Gilles Peskine1febfef2018-04-30 11:54:39 +02008129void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008130 void *ctx )
8131{
8132 if( ssl->handshake != NULL )
8133 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008134}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008135#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008136
Paul Bakker5121ce52009-01-03 21:22:43 +00008137/*
8138 * SSL get accessors
8139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008141{
8142 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8143}
8144
Hanno Becker8b170a02017-10-10 11:51:19 +01008145int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8146{
8147 /*
8148 * Case A: We're currently holding back
8149 * a message for further processing.
8150 */
8151
8152 if( ssl->keep_current_message == 1 )
8153 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008154 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008155 return( 1 );
8156 }
8157
8158 /*
8159 * Case B: Further records are pending in the current datagram.
8160 */
8161
8162#if defined(MBEDTLS_SSL_PROTO_DTLS)
8163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8164 ssl->in_left > ssl->next_record_offset )
8165 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008167 return( 1 );
8168 }
8169#endif /* MBEDTLS_SSL_PROTO_DTLS */
8170
8171 /*
8172 * Case C: A handshake message is being processed.
8173 */
8174
Hanno Becker8b170a02017-10-10 11:51:19 +01008175 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8176 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008178 return( 1 );
8179 }
8180
8181 /*
8182 * Case D: An application data message is being processed
8183 */
8184 if( ssl->in_offt != NULL )
8185 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008187 return( 1 );
8188 }
8189
8190 /*
8191 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008192 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008193 * we implement support for multiple alerts in single records.
8194 */
8195
8196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8197 return( 0 );
8198}
8199
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008200uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008201{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008202 if( ssl->session != NULL )
8203 return( ssl->session->verify_result );
8204
8205 if( ssl->session_negotiate != NULL )
8206 return( ssl->session_negotiate->verify_result );
8207
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008208 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008209}
8210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008212{
Paul Bakker926c8e42013-03-06 10:23:34 +01008213 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008214 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008217}
8218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008223 {
8224 switch( ssl->minor_ver )
8225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008227 return( "DTLSv1.0" );
8228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008230 return( "DTLSv1.2" );
8231
8232 default:
8233 return( "unknown (DTLS)" );
8234 }
8235 }
8236#endif
8237
Paul Bakker43ca69c2011-01-15 17:35:19 +00008238 switch( ssl->minor_ver )
8239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008240 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008241 return( "SSLv3.0" );
8242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008244 return( "TLSv1.0" );
8245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008247 return( "TLSv1.1" );
8248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008250 return( "TLSv1.2" );
8251
Paul Bakker43ca69c2011-01-15 17:35:19 +00008252 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008253 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008254 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008255}
8256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008258{
Hanno Becker3136ede2018-08-17 15:28:19 +01008259 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008261 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008262
Hanno Becker78640902018-08-13 16:35:15 +01008263 if( transform == NULL )
8264 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266#if defined(MBEDTLS_ZLIB_SUPPORT)
8267 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8268 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008269#endif
8270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273 case MBEDTLS_MODE_GCM:
8274 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008275 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008277 transform_expansion = transform->minlen;
8278 break;
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008281
8282 block_size = mbedtls_cipher_get_block_size(
8283 &transform->cipher_ctx_enc );
8284
Hanno Becker3136ede2018-08-17 15:28:19 +01008285 /* Expansion due to the addition of the MAC. */
8286 transform_expansion += transform->maclen;
8287
8288 /* Expansion due to the addition of CBC padding;
8289 * Theoretically up to 256 bytes, but we never use
8290 * more than the block size of the underlying cipher. */
8291 transform_expansion += block_size;
8292
8293 /* For TLS 1.1 or higher, an explicit IV is added
8294 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008295#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8296 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008297 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008298#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008299
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008300 break;
8301
8302 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008305 }
8306
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008307 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008308}
8309
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008310#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8311size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8312{
8313 size_t max_len;
8314
8315 /*
8316 * Assume mfl_code is correct since it was checked when set
8317 */
Angus Grattond8213d02016-05-25 20:56:48 +10008318 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008319
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008320 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008321 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008322 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008323 {
Angus Grattond8213d02016-05-25 20:56:48 +10008324 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008325 }
8326
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008327 /* During a handshake, use the value being negotiated */
8328 if( ssl->session_negotiate != NULL &&
8329 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8330 {
8331 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8332 }
8333
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008334 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008335}
8336#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8337
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008338#if defined(MBEDTLS_SSL_PROTO_DTLS)
8339static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8340{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008341 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8342 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8343 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8344 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8345 return ( 0 );
8346
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008347 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8348 return( ssl->mtu );
8349
8350 if( ssl->mtu == 0 )
8351 return( ssl->handshake->mtu );
8352
8353 return( ssl->mtu < ssl->handshake->mtu ?
8354 ssl->mtu : ssl->handshake->mtu );
8355}
8356#endif /* MBEDTLS_SSL_PROTO_DTLS */
8357
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008358int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8359{
8360 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8361
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008362#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8363 !defined(MBEDTLS_SSL_PROTO_DTLS)
8364 (void) ssl;
8365#endif
8366
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008367#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8368 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8369
8370 if( max_len > mfl )
8371 max_len = mfl;
8372#endif
8373
8374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008375 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008376 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008377 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008378 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8379 const size_t overhead = (size_t) ret;
8380
8381 if( ret < 0 )
8382 return( ret );
8383
8384 if( mtu <= overhead )
8385 {
8386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8387 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8388 }
8389
8390 if( max_len > mtu - overhead )
8391 max_len = mtu - overhead;
8392 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008393#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008394
Hanno Becker0defedb2018-08-10 12:35:02 +01008395#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8396 !defined(MBEDTLS_SSL_PROTO_DTLS)
8397 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008398#endif
8399
8400 return( (int) max_len );
8401}
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403#if defined(MBEDTLS_X509_CRT_PARSE_C)
8404const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008405{
8406 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008407 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008408
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008409 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_SSL_CLI_C)
8414int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008415{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008416 if( ssl == NULL ||
8417 dst == NULL ||
8418 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008419 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008422 }
8423
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008424 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008427
Paul Bakker5121ce52009-01-03 21:22:43 +00008428/*
Paul Bakker1961b702013-01-25 14:49:24 +01008429 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008434
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008435 if( ssl == NULL || ssl->conf == NULL )
8436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008439 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008441#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008442#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008443 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008445#endif
8446
Paul Bakker1961b702013-01-25 14:49:24 +01008447 return( ret );
8448}
8449
8450/*
8451 * Perform the SSL handshake
8452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008454{
8455 int ret = 0;
8456
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008457 if( ssl == NULL || ssl->conf == NULL )
8458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008465
8466 if( ret != 0 )
8467 break;
8468 }
8469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008471
8472 return( ret );
8473}
8474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475#if defined(MBEDTLS_SSL_RENEGOTIATION)
8476#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008477/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008478 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008481{
8482 int ret;
8483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008485
8486 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8488 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008489
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008490 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008491 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008493 return( ret );
8494 }
8495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008497
8498 return( 0 );
8499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008501
8502/*
8503 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504 * - any side: calling mbedtls_ssl_renegotiate(),
8505 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8506 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008507 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008508 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008511static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008512{
8513 int ret;
8514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008516
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008517 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8518 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008519
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008520 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8521 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008523 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008525 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008526 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008527 ssl->handshake->out_msg_seq = 1;
8528 else
8529 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008530 }
8531#endif
8532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8534 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008539 return( ret );
8540 }
8541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008543
8544 return( 0 );
8545}
8546
8547/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008548 * Renegotiate current connection on client,
8549 * or request renegotiation on server
8550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008554
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008555 if( ssl == NULL || ssl->conf == NULL )
8556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008559 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008566
8567 /* Did we already try/start sending HelloRequest? */
8568 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008570
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008571 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008572 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008576 /*
8577 * On client, either start the renegotiation process or,
8578 * if already in progress, continue the handshake
8579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008584
8585 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008588 return( ret );
8589 }
8590 }
8591 else
8592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008596 return( ret );
8597 }
8598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008600
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008601 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008602}
8603
8604/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008605 * Check record counters and renegotiate if they're above the limit.
8606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008608{
Andres AG2196c7f2016-12-15 17:01:16 +00008609 size_t ep_len = ssl_ep_len( ssl );
8610 int in_ctr_cmp;
8611 int out_ctr_cmp;
8612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8614 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008615 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008616 {
8617 return( 0 );
8618 }
8619
Andres AG2196c7f2016-12-15 17:01:16 +00008620 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8621 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008622 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008623 ssl->conf->renego_period + ep_len, 8 - ep_len );
8624
8625 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008626 {
8627 return( 0 );
8628 }
8629
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008634
8635/*
8636 * Receive application data decrypted from the SSL layer
8637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008639{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008640 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008641 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008642
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008643 if( ssl == NULL || ssl->conf == NULL )
8644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008652 return( ret );
8653
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008654 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008656 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008657 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008658 return( ret );
8659 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008660 }
8661#endif
8662
Hanno Becker4a810fb2017-05-24 16:27:30 +01008663 /*
8664 * Check if renegotiation is necessary and/or handshake is
8665 * in process. If yes, perform/continue, and fall through
8666 * if an unexpected packet is received while the client
8667 * is waiting for the ServerHello.
8668 *
8669 * (There is no equivalent to the last condition on
8670 * the server-side as it is not treated as within
8671 * a handshake while waiting for the ClientHello
8672 * after a renegotiation request.)
8673 */
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008676 ret = ssl_check_ctr_renegotiate( ssl );
8677 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8678 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008681 return( ret );
8682 }
8683#endif
8684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008688 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8689 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008692 return( ret );
8693 }
8694 }
8695
Hanno Beckere41158b2017-10-23 13:30:32 +01008696 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008697 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008698 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008699 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008700 if( ssl->f_get_timer != NULL &&
8701 ssl->f_get_timer( ssl->p_timer ) == -1 )
8702 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008703 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008704 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008705
Hanno Becker327c93b2018-08-15 13:56:18 +01008706 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008707 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008708 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8709 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008710
Hanno Becker4a810fb2017-05-24 16:27:30 +01008711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8712 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008713 }
8714
8715 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008717 {
8718 /*
8719 * OpenSSL sends empty messages to randomize the IV
8720 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008721 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008724 return( 0 );
8725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008727 return( ret );
8728 }
8729 }
8730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008734
Hanno Becker4a810fb2017-05-24 16:27:30 +01008735 /*
8736 * - For client-side, expect SERVER_HELLO_REQUEST.
8737 * - For server-side, expect CLIENT_HELLO.
8738 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8739 */
8740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008742 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008744 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008747
8748 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008751 {
8752 continue;
8753 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008756 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008757#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008758
Hanno Becker4a810fb2017-05-24 16:27:30 +01008759#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008760 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008764
8765 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008768 {
8769 continue;
8770 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008773 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008774#endif /* MBEDTLS_SSL_SRV_C */
8775
Hanno Becker21df7f92017-10-17 11:03:26 +01008776#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008777 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008778 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8779 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8780 ssl->conf->allow_legacy_renegotiation ==
8781 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8782 {
8783 /*
8784 * Accept renegotiation request
8785 */
Paul Bakker48916f92012-09-16 19:57:18 +00008786
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008787 /* DTLS clients need to know renego is server-initiated */
8788#if defined(MBEDTLS_SSL_PROTO_DTLS)
8789 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8790 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8791 {
8792 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8793 }
8794#endif
8795 ret = ssl_start_renegotiation( ssl );
8796 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8797 ret != 0 )
8798 {
8799 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8800 return( ret );
8801 }
8802 }
8803 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008804#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008805 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008806 /*
8807 * Refuse renegotiation
8808 */
8809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812#if defined(MBEDTLS_SSL_PROTO_SSL3)
8813 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008814 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008815 /* SSLv3 does not have a "no_renegotiation" warning, so
8816 we send a fatal alert and abort the connection. */
8817 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8818 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8819 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008820 }
8821 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8823#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8824 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8825 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8828 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8829 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008830 {
8831 return( ret );
8832 }
Paul Bakker48916f92012-09-16 19:57:18 +00008833 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008834 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8836 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008840 }
Paul Bakker48916f92012-09-16 19:57:18 +00008841 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008842
Hanno Becker90333da2017-10-10 11:27:13 +01008843 /* At this point, we don't know whether the renegotiation has been
8844 * completed or not. The cases to consider are the following:
8845 * 1) The renegotiation is complete. In this case, no new record
8846 * has been read yet.
8847 * 2) The renegotiation is incomplete because the client received
8848 * an application data record while awaiting the ServerHello.
8849 * 3) The renegotiation is incomplete because the client received
8850 * a non-handshake, non-application data message while awaiting
8851 * the ServerHello.
8852 * In each of these case, looping will be the proper action:
8853 * - For 1), the next iteration will read a new record and check
8854 * if it's application data.
8855 * - For 2), the loop condition isn't satisfied as application data
8856 * is present, hence continue is the same as break
8857 * - For 3), the loop condition is satisfied and read_record
8858 * will re-deliver the message that was held back by the client
8859 * when expecting the ServerHello.
8860 */
8861 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008862 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008863#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008865 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008866 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008867 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008868 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008871 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008873 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008874 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008875 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8879 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008882 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008883 }
8884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8888 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008889 }
8890
8891 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008892
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008893 /* We're going to return something now, cancel timer,
8894 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008896 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008897
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008899 /* If we requested renego but received AppData, resend HelloRequest.
8900 * Do it now, after setting in_offt, to avoid taking this branch
8901 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008902#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008903 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008904 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008905 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008906 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008909 return( ret );
8910 }
8911 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008913#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008914 }
8915
8916 n = ( len < ssl->in_msglen )
8917 ? len : ssl->in_msglen;
8918
8919 memcpy( buf, ssl->in_offt, n );
8920 ssl->in_msglen -= n;
8921
8922 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008923 {
8924 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008925 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008926 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008927 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008928 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008929 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008930 /* more data available */
8931 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008932 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008935
Paul Bakker23986e52011-04-24 08:57:21 +00008936 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008937}
8938
8939/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008940 * Send application data to be encrypted by the SSL layer, taking care of max
8941 * fragment length and buffer size.
8942 *
8943 * According to RFC 5246 Section 6.2.1:
8944 *
8945 * Zero-length fragments of Application data MAY be sent as they are
8946 * potentially useful as a traffic analysis countermeasure.
8947 *
8948 * Therefore, it is possible that the input message length is 0 and the
8949 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008950 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008951static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008952 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008953{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008954 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8955 const size_t max_len = (size_t) ret;
8956
8957 if( ret < 0 )
8958 {
8959 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8960 return( ret );
8961 }
8962
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008963 if( len > max_len )
8964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008969 "maximum fragment length: %d > %d",
8970 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008972 }
8973 else
8974#endif
8975 len = max_len;
8976 }
Paul Bakker887bd502011-06-08 13:10:54 +00008977
Paul Bakker5121ce52009-01-03 21:22:43 +00008978 if( ssl->out_left != 0 )
8979 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008980 /*
8981 * The user has previously tried to send the data and
8982 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8983 * written. In this case, we expect the high-level write function
8984 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008989 return( ret );
8990 }
8991 }
Paul Bakker887bd502011-06-08 13:10:54 +00008992 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008993 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008994 /*
8995 * The user is trying to send a message the first time, so we need to
8996 * copy the data into the internal buffers and setup the data structure
8997 * to keep track of partial writes
8998 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008999 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009001 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009002
Hanno Becker67bc7c32018-08-06 11:33:50 +01009003 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009006 return( ret );
9007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009008 }
9009
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009010 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009011}
9012
9013/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009014 * Write application data, doing 1/n-1 splitting if necessary.
9015 *
9016 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009017 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009018 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009020#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009021static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009022 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009023{
9024 int ret;
9025
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009026 if( ssl->conf->cbc_record_splitting ==
9027 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009028 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9030 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9031 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009032 {
9033 return( ssl_write_real( ssl, buf, len ) );
9034 }
9035
9036 if( ssl->split_done == 0 )
9037 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009038 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009039 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009040 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009041 }
9042
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009043 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9044 return( ret );
9045 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009046
9047 return( ret + 1 );
9048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009050
9051/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009052 * Write application data (public-facing wrapper)
9053 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009054int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009055{
9056 int ret;
9057
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009059
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009060 if( ssl == NULL || ssl->conf == NULL )
9061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9062
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009063#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009064 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9065 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009066 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009067 return( ret );
9068 }
9069#endif
9070
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009071 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009072 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009073 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009074 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009076 return( ret );
9077 }
9078 }
9079
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009080#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009081 ret = ssl_write_split( ssl, buf, len );
9082#else
9083 ret = ssl_write_real( ssl, buf, len );
9084#endif
9085
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009087
9088 return( ret );
9089}
9090
9091/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009092 * Notify the peer that the connection is being closed
9093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009095{
9096 int ret;
9097
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009098 if( ssl == NULL || ssl->conf == NULL )
9099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009102
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009103 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9109 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9110 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009113 return( ret );
9114 }
9115 }
9116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009118
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009119 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009120}
9121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009123{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009124 if( transform == NULL )
9125 return;
9126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009128 deflateEnd( &transform->ctx_deflate );
9129 inflateEnd( &transform->ctx_inflate );
9130#endif
9131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009132 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9133 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135 mbedtls_md_free( &transform->md_ctx_enc );
9136 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009137
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009138 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009139}
9140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141#if defined(MBEDTLS_X509_CRT_PARSE_C)
9142static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009145
9146 while( cur != NULL )
9147 {
9148 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009150 cur = next;
9151 }
9152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009154
Hanno Becker0271f962018-08-16 13:23:47 +01009155#if defined(MBEDTLS_SSL_PROTO_DTLS)
9156
9157static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9158{
9159 unsigned offset;
9160 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9161
9162 if( hs == NULL )
9163 return;
9164
Hanno Becker283f5ef2018-08-24 09:34:47 +01009165 ssl_free_buffered_record( ssl );
9166
Hanno Becker0271f962018-08-16 13:23:47 +01009167 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009168 ssl_buffering_free_slot( ssl, offset );
9169}
9170
9171static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9172 uint8_t slot )
9173{
9174 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9175 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009176
9177 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9178 return;
9179
Hanno Beckere605b192018-08-21 15:59:07 +01009180 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009181 {
Hanno Beckere605b192018-08-21 15:59:07 +01009182 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009183 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009184 mbedtls_free( hs_buf->data );
9185 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009186 }
9187}
9188
9189#endif /* MBEDTLS_SSL_PROTO_DTLS */
9190
Gilles Peskine9b562d52018-04-25 20:32:43 +02009191void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009192{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009193 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9194
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009195 if( handshake == NULL )
9196 return;
9197
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009198#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9199 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9200 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009201 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009202 handshake->async_in_progress = 0;
9203 }
9204#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9205
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009206#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9207 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9208 mbedtls_md5_free( &handshake->fin_md5 );
9209 mbedtls_sha1_free( &handshake->fin_sha1 );
9210#endif
9211#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9212#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009213#if defined(MBEDTLS_USE_PSA_CRYPTO)
9214 psa_hash_abort( &handshake->fin_sha256_psa );
9215#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009216 mbedtls_sha256_free( &handshake->fin_sha256 );
9217#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009218#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009219#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009221 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009222#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009223 mbedtls_sha512_free( &handshake->fin_sha512 );
9224#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009225#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009226#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228#if defined(MBEDTLS_DHM_C)
9229 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231#if defined(MBEDTLS_ECDH_C)
9232 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009233#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009234#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009235 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009236#if defined(MBEDTLS_SSL_CLI_C)
9237 mbedtls_free( handshake->ecjpake_cache );
9238 handshake->ecjpake_cache = NULL;
9239 handshake->ecjpake_cache_len = 0;
9240#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009241#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009242
Janos Follath4ae5c292016-02-10 11:27:43 +00009243#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9244 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009245 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009247#endif
9248
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009249#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9250 if( handshake->psk != NULL )
9251 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009252 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009253 mbedtls_free( handshake->psk );
9254 }
9255#endif
9256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9258 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009259 /*
9260 * Free only the linked list wrapper, not the keys themselves
9261 * since the belong to the SNI callback
9262 */
9263 if( handshake->sni_key_cert != NULL )
9264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009266
9267 while( cur != NULL )
9268 {
9269 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009271 cur = next;
9272 }
9273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009275
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009276#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009277 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009278#endif
9279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280#if defined(MBEDTLS_SSL_PROTO_DTLS)
9281 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009282 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009283 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009284#endif
9285
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009286 mbedtls_platform_zeroize( handshake,
9287 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009288}
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009291{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009292 if( session == NULL )
9293 return;
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009296 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 mbedtls_x509_crt_free( session->peer_cert );
9299 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009300 }
Paul Bakkered27a042013-04-18 22:46:23 +02009301#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009302
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009303#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009305#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009306
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009307 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009308}
9309
Paul Bakker5121ce52009-01-03 21:22:43 +00009310/*
9311 * Free an SSL context
9312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009314{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009315 if( ssl == NULL )
9316 return;
9317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009319
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009320 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009321 {
Angus Grattond8213d02016-05-25 20:56:48 +10009322 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009324 }
9325
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009326 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009327 {
Angus Grattond8213d02016-05-25 20:56:48 +10009328 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009330 }
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009333 if( ssl->compress_buf != NULL )
9334 {
Angus Grattond8213d02016-05-25 20:56:48 +10009335 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009337 }
9338#endif
9339
Paul Bakker48916f92012-09-16 19:57:18 +00009340 if( ssl->transform )
9341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 mbedtls_ssl_transform_free( ssl->transform );
9343 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009344 }
9345
9346 if( ssl->handshake )
9347 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009348 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9350 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 mbedtls_free( ssl->handshake );
9353 mbedtls_free( ssl->transform_negotiate );
9354 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009355 }
9356
Paul Bakkerc0463502013-02-14 11:19:38 +01009357 if( ssl->session )
9358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 mbedtls_ssl_session_free( ssl->session );
9360 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009361 }
9362
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009363#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009364 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009365 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009366 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009368 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009369#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9372 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9375 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009376 }
9377#endif
9378
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009379#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009381#endif
9382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009384
Paul Bakker86f04f42013-02-14 11:20:09 +01009385 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009386 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009387}
9388
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009389/*
9390 * Initialze mbedtls_ssl_config
9391 */
9392void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9393{
9394 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9395}
9396
Simon Butcherc97b6972015-12-27 23:48:17 +00009397#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009398static int ssl_preset_default_hashes[] = {
9399#if defined(MBEDTLS_SHA512_C)
9400 MBEDTLS_MD_SHA512,
9401 MBEDTLS_MD_SHA384,
9402#endif
9403#if defined(MBEDTLS_SHA256_C)
9404 MBEDTLS_MD_SHA256,
9405 MBEDTLS_MD_SHA224,
9406#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009407#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009408 MBEDTLS_MD_SHA1,
9409#endif
9410 MBEDTLS_MD_NONE
9411};
Simon Butcherc97b6972015-12-27 23:48:17 +00009412#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009413
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009414static int ssl_preset_suiteb_ciphersuites[] = {
9415 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9416 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9417 0
9418};
9419
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009420#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009421static int ssl_preset_suiteb_hashes[] = {
9422 MBEDTLS_MD_SHA256,
9423 MBEDTLS_MD_SHA384,
9424 MBEDTLS_MD_NONE
9425};
9426#endif
9427
9428#if defined(MBEDTLS_ECP_C)
9429static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9430 MBEDTLS_ECP_DP_SECP256R1,
9431 MBEDTLS_ECP_DP_SECP384R1,
9432 MBEDTLS_ECP_DP_NONE
9433};
9434#endif
9435
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009436/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009437 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009438 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009439int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009440 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009441{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009442#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009443 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009444#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009445
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009446 /* Use the functions here so that they are covered in tests,
9447 * but otherwise access member directly for efficiency */
9448 mbedtls_ssl_conf_endpoint( conf, endpoint );
9449 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009450
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009451 /*
9452 * Things that are common to all presets
9453 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009454#if defined(MBEDTLS_SSL_CLI_C)
9455 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9456 {
9457 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9458#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9459 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9460#endif
9461 }
9462#endif
9463
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009464#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009465 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009466#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009467
9468#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9469 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9470#endif
9471
9472#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9473 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9474#endif
9475
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009476#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9477 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9478#endif
9479
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009480#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009481 conf->f_cookie_write = ssl_cookie_write_dummy;
9482 conf->f_cookie_check = ssl_cookie_check_dummy;
9483#endif
9484
9485#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9486 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9487#endif
9488
Janos Follath088ce432017-04-10 12:42:31 +01009489#if defined(MBEDTLS_SSL_SRV_C)
9490 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9491#endif
9492
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009493#if defined(MBEDTLS_SSL_PROTO_DTLS)
9494 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9495 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9496#endif
9497
9498#if defined(MBEDTLS_SSL_RENEGOTIATION)
9499 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009500 memset( conf->renego_period, 0x00, 2 );
9501 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009502#endif
9503
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009504#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9505 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9506 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009507 const unsigned char dhm_p[] =
9508 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9509 const unsigned char dhm_g[] =
9510 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9511
Hanno Beckera90658f2017-10-04 15:29:08 +01009512 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9513 dhm_p, sizeof( dhm_p ),
9514 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009515 {
9516 return( ret );
9517 }
9518 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009519#endif
9520
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009521 /*
9522 * Preset-specific defaults
9523 */
9524 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009525 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009526 /*
9527 * NSA Suite B
9528 */
9529 case MBEDTLS_SSL_PRESET_SUITEB:
9530 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9531 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9532 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9533 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9534
9535 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9536 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9537 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9538 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9539 ssl_preset_suiteb_ciphersuites;
9540
9541#if defined(MBEDTLS_X509_CRT_PARSE_C)
9542 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009543#endif
9544
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009545#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009546 conf->sig_hashes = ssl_preset_suiteb_hashes;
9547#endif
9548
9549#if defined(MBEDTLS_ECP_C)
9550 conf->curve_list = ssl_preset_suiteb_curves;
9551#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009552 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009553
9554 /*
9555 * Default
9556 */
9557 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009558 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9559 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9560 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9561 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9562 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9563 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9564 MBEDTLS_SSL_MIN_MINOR_VERSION :
9565 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009566 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9567 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9568
9569#if defined(MBEDTLS_SSL_PROTO_DTLS)
9570 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9571 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9572#endif
9573
9574 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9575 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9576 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9577 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9578 mbedtls_ssl_list_ciphersuites();
9579
9580#if defined(MBEDTLS_X509_CRT_PARSE_C)
9581 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9582#endif
9583
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009584#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009585 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009586#endif
9587
9588#if defined(MBEDTLS_ECP_C)
9589 conf->curve_list = mbedtls_ecp_grp_id_list();
9590#endif
9591
9592#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9593 conf->dhm_min_bitlen = 1024;
9594#endif
9595 }
9596
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009597 return( 0 );
9598}
9599
9600/*
9601 * Free mbedtls_ssl_config
9602 */
9603void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9604{
9605#if defined(MBEDTLS_DHM_C)
9606 mbedtls_mpi_free( &conf->dhm_P );
9607 mbedtls_mpi_free( &conf->dhm_G );
9608#endif
9609
9610#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9611 if( conf->psk != NULL )
9612 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009613 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009614 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009615 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009616 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009617 }
9618
9619 if( conf->psk_identity != NULL )
9620 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009621 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009622 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009623 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009624 conf->psk_identity_len = 0;
9625 }
9626#endif
9627
9628#if defined(MBEDTLS_X509_CRT_PARSE_C)
9629 ssl_key_cert_free( conf->key_cert );
9630#endif
9631
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009632 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009633}
9634
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009635#if defined(MBEDTLS_PK_C) && \
9636 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009637/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642#if defined(MBEDTLS_RSA_C)
9643 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9644 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646#if defined(MBEDTLS_ECDSA_C)
9647 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9648 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009649#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009651}
9652
Hanno Becker7e5437a2017-04-28 17:15:26 +01009653unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9654{
9655 switch( type ) {
9656 case MBEDTLS_PK_RSA:
9657 return( MBEDTLS_SSL_SIG_RSA );
9658 case MBEDTLS_PK_ECDSA:
9659 case MBEDTLS_PK_ECKEY:
9660 return( MBEDTLS_SSL_SIG_ECDSA );
9661 default:
9662 return( MBEDTLS_SSL_SIG_ANON );
9663 }
9664}
9665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009667{
9668 switch( sig )
9669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670#if defined(MBEDTLS_RSA_C)
9671 case MBEDTLS_SSL_SIG_RSA:
9672 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_ECDSA_C)
9675 case MBEDTLS_SSL_SIG_ECDSA:
9676 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009677#endif
9678 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009680 }
9681}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009682#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009683
Hanno Becker7e5437a2017-04-28 17:15:26 +01009684#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9685 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9686
9687/* Find an entry in a signature-hash set matching a given hash algorithm. */
9688mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9689 mbedtls_pk_type_t sig_alg )
9690{
9691 switch( sig_alg )
9692 {
9693 case MBEDTLS_PK_RSA:
9694 return( set->rsa );
9695 case MBEDTLS_PK_ECDSA:
9696 return( set->ecdsa );
9697 default:
9698 return( MBEDTLS_MD_NONE );
9699 }
9700}
9701
9702/* Add a signature-hash-pair to a signature-hash set */
9703void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9704 mbedtls_pk_type_t sig_alg,
9705 mbedtls_md_type_t md_alg )
9706{
9707 switch( sig_alg )
9708 {
9709 case MBEDTLS_PK_RSA:
9710 if( set->rsa == MBEDTLS_MD_NONE )
9711 set->rsa = md_alg;
9712 break;
9713
9714 case MBEDTLS_PK_ECDSA:
9715 if( set->ecdsa == MBEDTLS_MD_NONE )
9716 set->ecdsa = md_alg;
9717 break;
9718
9719 default:
9720 break;
9721 }
9722}
9723
9724/* Allow exactly one hash algorithm for each signature. */
9725void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9726 mbedtls_md_type_t md_alg )
9727{
9728 set->rsa = md_alg;
9729 set->ecdsa = md_alg;
9730}
9731
9732#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9733 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9734
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009735/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009736 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009739{
9740 switch( hash )
9741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742#if defined(MBEDTLS_MD5_C)
9743 case MBEDTLS_SSL_HASH_MD5:
9744 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746#if defined(MBEDTLS_SHA1_C)
9747 case MBEDTLS_SSL_HASH_SHA1:
9748 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009749#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750#if defined(MBEDTLS_SHA256_C)
9751 case MBEDTLS_SSL_HASH_SHA224:
9752 return( MBEDTLS_MD_SHA224 );
9753 case MBEDTLS_SSL_HASH_SHA256:
9754 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009755#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_SHA512_C)
9757 case MBEDTLS_SSL_HASH_SHA384:
9758 return( MBEDTLS_MD_SHA384 );
9759 case MBEDTLS_SSL_HASH_SHA512:
9760 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009761#endif
9762 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009764 }
9765}
9766
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009767/*
9768 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9769 */
9770unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9771{
9772 switch( md )
9773 {
9774#if defined(MBEDTLS_MD5_C)
9775 case MBEDTLS_MD_MD5:
9776 return( MBEDTLS_SSL_HASH_MD5 );
9777#endif
9778#if defined(MBEDTLS_SHA1_C)
9779 case MBEDTLS_MD_SHA1:
9780 return( MBEDTLS_SSL_HASH_SHA1 );
9781#endif
9782#if defined(MBEDTLS_SHA256_C)
9783 case MBEDTLS_MD_SHA224:
9784 return( MBEDTLS_SSL_HASH_SHA224 );
9785 case MBEDTLS_MD_SHA256:
9786 return( MBEDTLS_SSL_HASH_SHA256 );
9787#endif
9788#if defined(MBEDTLS_SHA512_C)
9789 case MBEDTLS_MD_SHA384:
9790 return( MBEDTLS_SSL_HASH_SHA384 );
9791 case MBEDTLS_MD_SHA512:
9792 return( MBEDTLS_SSL_HASH_SHA512 );
9793#endif
9794 default:
9795 return( MBEDTLS_SSL_HASH_NONE );
9796 }
9797}
9798
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009799#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009800/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009801 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009802 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009803 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009804int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009805{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009807
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009808 if( ssl->conf->curve_list == NULL )
9809 return( -1 );
9810
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009811 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009812 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009813 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009814
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009815 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009816}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009817#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009818
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009819#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009820/*
9821 * Check if a hash proposed by the peer is in our list.
9822 * Return 0 if we're willing to use it, -1 otherwise.
9823 */
9824int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9825 mbedtls_md_type_t md )
9826{
9827 const int *cur;
9828
9829 if( ssl->conf->sig_hashes == NULL )
9830 return( -1 );
9831
9832 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9833 if( *cur == (int) md )
9834 return( 0 );
9835
9836 return( -1 );
9837}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009838#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840#if defined(MBEDTLS_X509_CRT_PARSE_C)
9841int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9842 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009843 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009844 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009845{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009846 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009848 int usage = 0;
9849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009851 const char *ext_oid;
9852 size_t ext_len;
9853#endif
9854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9856 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009857 ((void) cert);
9858 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009859 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009860#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9863 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009864 {
9865 /* Server part of the key exchange */
9866 switch( ciphersuite->key_exchange )
9867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 case MBEDTLS_KEY_EXCHANGE_RSA:
9869 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009870 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009871 break;
9872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9874 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9875 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9876 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009877 break;
9878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9880 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009881 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009882 break;
9883
9884 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 case MBEDTLS_KEY_EXCHANGE_NONE:
9886 case MBEDTLS_KEY_EXCHANGE_PSK:
9887 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9888 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009889 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009890 usage = 0;
9891 }
9892 }
9893 else
9894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9896 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009897 }
9898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009900 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009901 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009902 ret = -1;
9903 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009904#else
9905 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9909 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9912 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009913 }
9914 else
9915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9917 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009918 }
9919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009921 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009922 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009923 ret = -1;
9924 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009926
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009927 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009930
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009931/*
9932 * Convert version numbers to/from wire format
9933 * and, for DTLS, to/from TLS equivalent.
9934 *
9935 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009936 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009937 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9938 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009941 unsigned char ver[2] )
9942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#if defined(MBEDTLS_SSL_PROTO_DTLS)
9944 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009947 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9948
9949 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9950 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9951 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009952 else
9953#else
9954 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009955#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009956 {
9957 ver[0] = (unsigned char) major;
9958 ver[1] = (unsigned char) minor;
9959 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009960}
9961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009963 const unsigned char ver[2] )
9964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965#if defined(MBEDTLS_SSL_PROTO_DTLS)
9966 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009967 {
9968 *major = 255 - ver[0] + 2;
9969 *minor = 255 - ver[1] + 1;
9970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009972 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9973 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009974 else
9975#else
9976 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009977#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009978 {
9979 *major = ver[0];
9980 *minor = ver[1];
9981 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009982}
9983
Simon Butcher99000142016-10-13 17:21:01 +01009984int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9985{
9986#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9987 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9988 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9989
9990 switch( md )
9991 {
9992#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9993#if defined(MBEDTLS_MD5_C)
9994 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009995 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009996#endif
9997#if defined(MBEDTLS_SHA1_C)
9998 case MBEDTLS_SSL_HASH_SHA1:
9999 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10000 break;
10001#endif
10002#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10003#if defined(MBEDTLS_SHA512_C)
10004 case MBEDTLS_SSL_HASH_SHA384:
10005 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10006 break;
10007#endif
10008#if defined(MBEDTLS_SHA256_C)
10009 case MBEDTLS_SSL_HASH_SHA256:
10010 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10011 break;
10012#endif
10013 default:
10014 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10015 }
10016
10017 return 0;
10018#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10019 (void) ssl;
10020 (void) md;
10021
10022 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10023#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10024}
10025
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010026#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10027 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10028int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10029 unsigned char *output,
10030 unsigned char *data, size_t data_len )
10031{
10032 int ret = 0;
10033 mbedtls_md5_context mbedtls_md5;
10034 mbedtls_sha1_context mbedtls_sha1;
10035
10036 mbedtls_md5_init( &mbedtls_md5 );
10037 mbedtls_sha1_init( &mbedtls_sha1 );
10038
10039 /*
10040 * digitally-signed struct {
10041 * opaque md5_hash[16];
10042 * opaque sha_hash[20];
10043 * };
10044 *
10045 * md5_hash
10046 * MD5(ClientHello.random + ServerHello.random
10047 * + ServerParams);
10048 * sha_hash
10049 * SHA(ClientHello.random + ServerHello.random
10050 * + ServerParams);
10051 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010052 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010053 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010055 goto exit;
10056 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010057 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010058 ssl->handshake->randbytes, 64 ) ) != 0 )
10059 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010061 goto exit;
10062 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010063 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010064 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010066 goto exit;
10067 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010068 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010069 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010071 goto exit;
10072 }
10073
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010074 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010075 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010077 goto exit;
10078 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010079 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010080 ssl->handshake->randbytes, 64 ) ) != 0 )
10081 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010083 goto exit;
10084 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010085 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010086 data_len ) ) != 0 )
10087 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010089 goto exit;
10090 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010091 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010092 output + 16 ) ) != 0 )
10093 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010095 goto exit;
10096 }
10097
10098exit:
10099 mbedtls_md5_free( &mbedtls_md5 );
10100 mbedtls_sha1_free( &mbedtls_sha1 );
10101
10102 if( ret != 0 )
10103 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10104 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10105
10106 return( ret );
10107
10108}
10109#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10110 MBEDTLS_SSL_PROTO_TLS1_1 */
10111
10112#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10113 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10114int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010115 unsigned char *hash, size_t *hashlen,
10116 unsigned char *data, size_t data_len,
10117 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010118{
10119 int ret = 0;
10120 mbedtls_md_context_t ctx;
10121 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010122 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010123
10124 mbedtls_md_init( &ctx );
10125
10126 /*
10127 * digitally-signed struct {
10128 * opaque client_random[32];
10129 * opaque server_random[32];
10130 * ServerDHParams params;
10131 * };
10132 */
10133 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10134 {
10135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10136 goto exit;
10137 }
10138 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10139 {
10140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10141 goto exit;
10142 }
10143 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10144 {
10145 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10146 goto exit;
10147 }
10148 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10149 {
10150 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10151 goto exit;
10152 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010153 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010154 {
10155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10156 goto exit;
10157 }
10158
10159exit:
10160 mbedtls_md_free( &ctx );
10161
10162 if( ret != 0 )
10163 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10164 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10165
10166 return( ret );
10167}
10168#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10169 MBEDTLS_SSL_PROTO_TLS1_2 */
10170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171#endif /* MBEDTLS_SSL_TLS_C */