blob: 8fe93141fd9df097764c33da6a7387b16dada1e8 [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
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001350 mbedtls_md5_finish_ret( &md5, hash );
1351 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{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001367 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001368
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001369 mbedtls_sha256_init( &sha256 );
1370
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001372
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001373 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001374 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001378
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001379 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001380
Paul Bakker380da532012-04-18 16:10:25 +00001381 return;
1382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_SHA512_C)
1386void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001387{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001388 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001389
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001390 mbedtls_sha512_init( &sha512 );
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001393
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001394 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001395 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001400 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001401
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 return;
1403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_SHA512_C */
1405#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1408int 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 +02001409{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001410 unsigned char *p = ssl->handshake->premaster;
1411 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001412 const unsigned char *psk = ssl->conf->psk;
1413 size_t psk_len = ssl->conf->psk_len;
1414
1415 /* If the psk callback was called, use its result */
1416 if( ssl->handshake->psk != NULL )
1417 {
1418 psk = ssl->handshake->psk;
1419 psk_len = ssl->handshake->psk_len;
1420 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001421
1422 /*
1423 * PMS = struct {
1424 * opaque other_secret<0..2^16-1>;
1425 * opaque psk<0..2^16-1>;
1426 * };
1427 * with "other_secret" depending on the particular key exchange
1428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1430 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001431 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001432 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001434
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001435 *(p++) = (unsigned char)( psk_len >> 8 );
1436 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001437
1438 if( end < p || (size_t)( end - p ) < psk_len )
1439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1440
1441 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001442 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001443 }
1444 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1446#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1447 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001448 {
1449 /*
1450 * other_secret already set by the ClientKeyExchange message,
1451 * and is 48 bytes long
1452 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001453 if( end - p < 2 )
1454 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1455
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001456 *p++ = 0;
1457 *p++ = 48;
1458 p += 48;
1459 }
1460 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1462#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1463 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001464 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001465 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001466 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001467
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001468 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001470 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001471 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001474 return( ret );
1475 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001476 *(p++) = (unsigned char)( len >> 8 );
1477 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001478 p += len;
1479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001481 }
1482 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1484#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1485 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001486 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001487 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001488 size_t zlen;
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001491 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001492 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001495 return( ret );
1496 }
1497
1498 *(p++) = (unsigned char)( zlen >> 8 );
1499 *(p++) = (unsigned char)( zlen );
1500 p += zlen;
1501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001503 }
1504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001509 }
1510
1511 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001512 if( end - p < 2 )
1513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001514
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001515 *(p++) = (unsigned char)( psk_len >> 8 );
1516 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001517
1518 if( end < p || (size_t)( end - p ) < psk_len )
1519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1520
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001521 memcpy( p, psk, psk_len );
1522 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001523
1524 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1525
1526 return( 0 );
1527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001531/*
1532 * SSLv3.0 MAC functions
1533 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001534#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001535static void ssl_mac( mbedtls_md_context_t *md_ctx,
1536 const unsigned char *secret,
1537 const unsigned char *buf, size_t len,
1538 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001539 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540{
1541 unsigned char header[11];
1542 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001543 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1545 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001546
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001547 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001549 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001550 else
Paul Bakker68884e32013-01-07 18:20:04 +01001551 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
1553 memcpy( header, ctr, 8 );
1554 header[ 8] = (unsigned char) type;
1555 header[ 9] = (unsigned char)( len >> 8 );
1556 header[10] = (unsigned char)( len );
1557
Paul Bakker68884e32013-01-07 18:20:04 +01001558 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 mbedtls_md_starts( md_ctx );
1560 mbedtls_md_update( md_ctx, secret, md_size );
1561 mbedtls_md_update( md_ctx, padding, padlen );
1562 mbedtls_md_update( md_ctx, header, 11 );
1563 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001564 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Paul Bakker68884e32013-01-07 18:20:04 +01001566 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 mbedtls_md_starts( md_ctx );
1568 mbedtls_md_update( md_ctx, secret, md_size );
1569 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001570 mbedtls_md_update( md_ctx, out, md_size );
1571 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1576 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001577 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001578#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001579#endif
1580
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001581/* The function below is only used in the Lucky 13 counter-measure in
1582 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1583#if defined(SSL_SOME_MODES_USE_MAC) && \
1584 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1585 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1586 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1587/* This function makes sure every byte in the memory region is accessed
1588 * (in ascending addresses order) */
1589static void ssl_read_memory( unsigned char *p, size_t len )
1590{
1591 unsigned char acc = 0;
1592 volatile unsigned char force;
1593
1594 for( ; len != 0; p++, len-- )
1595 acc ^= *p;
1596
1597 force = acc;
1598 (void) force;
1599}
1600#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1601
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001602/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001606{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001608 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001612 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001616 }
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001621 ssl->out_msg, ssl->out_msglen );
1622
Paul Bakker5121ce52009-01-03 21:22:43 +00001623 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001624 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001626#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 if( mode == MBEDTLS_MODE_STREAM ||
1628 ( mode == MBEDTLS_MODE_CBC
1629#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1630 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001631#endif
1632 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_SSL_PROTO_SSL3)
1635 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001636 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001637 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001638
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001639 ssl_mac( &ssl->transform_out->md_ctx_enc,
1640 ssl->transform_out->mac_enc,
1641 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001642 ssl->out_ctr, ssl->out_msgtype,
1643 mac );
1644
1645 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 }
1647 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1650 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1651 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 {
Hanno Becker992b6872017-11-09 18:57:39 +00001653 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1656 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1657 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1658 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001660 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001662
1663 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001664 }
1665 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001666#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001670 }
1671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001673 ssl->out_msg + ssl->out_msglen,
1674 ssl->transform_out->maclen );
1675
1676 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001677 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001678 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001679#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001681 /*
1682 * Encrypt
1683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1685 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001686 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001687 int ret;
1688 size_t olen = 0;
1689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 "including %d bytes of padding",
1692 ssl->out_msglen, 0 ) );
1693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001695 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001696 ssl->transform_out->ivlen,
1697 ssl->out_msg, ssl->out_msglen,
1698 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001701 return( ret );
1702 }
1703
1704 if( ssl->out_msglen != olen )
1705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001708 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 }
Paul Bakker68884e32013-01-07 18:20:04 +01001710 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001712#if defined(MBEDTLS_GCM_C) || \
1713 defined(MBEDTLS_CCM_C) || \
1714 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001716 mode == MBEDTLS_MODE_CCM ||
1717 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001718 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001719 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001720 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001721 unsigned char *enc_msg;
1722 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001723 unsigned char iv[12];
1724 mbedtls_ssl_transform *transform = ssl->transform_out;
1725 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001727 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001728
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001729 /*
1730 * Prepare additional authenticated data
1731 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001732 memcpy( add_data, ssl->out_ctr, 8 );
1733 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001735 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001736 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1737 add_data[12] = ssl->out_msglen & 0xFF;
1738
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001739 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001740
Paul Bakker68884e32013-01-07 18:20:04 +01001741 /*
1742 * Generate IV
1743 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001744 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1745 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001746 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001747 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1748 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1749 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1750
1751 }
1752 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1753 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001754 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001755 unsigned char i;
1756
1757 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1758
1759 for( i = 0; i < 8; i++ )
1760 iv[i+4] ^= ssl->out_ctr[i];
1761 }
1762 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001763 {
1764 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001767 }
1768
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001769 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1770 iv, transform->ivlen );
1771 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1772 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001773
Paul Bakker68884e32013-01-07 18:20:04 +01001774 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001775 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001776 */
1777 enc_msg = ssl->out_msg;
1778 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001779 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001782 "including 0 bytes of padding",
1783 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001784
Paul Bakker68884e32013-01-07 18:20:04 +01001785 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001786 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001787 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001788 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1789 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001790 add_data, 13,
1791 enc_msg, enc_msglen,
1792 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001793 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001796 return( ret );
1797 }
1798
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001799 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1802 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001803 }
1804
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001805 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001806 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001809 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1812#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001813 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001816 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001817 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001818 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001819
Paul Bakker48916f92012-09-16 19:57:18 +00001820 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1821 ssl->transform_out->ivlen;
1822 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 padlen = 0;
1824
1825 for( i = 0; i <= padlen; i++ )
1826 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1827
1828 ssl->out_msglen += padlen + 1;
1829
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001830 enc_msglen = ssl->out_msglen;
1831 enc_msg = ssl->out_msg;
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001834 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001835 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1836 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001839 {
1840 /*
1841 * Generate IV
1842 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001843 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001844 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001845 if( ret != 0 )
1846 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001847
Paul Bakker92be97b2013-01-02 17:30:03 +01001848 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001849 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001850
1851 /*
1852 * Fix pointer positions and message length with added IV
1853 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001854 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001855 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001856 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001857 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001861 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001862 ssl->out_msglen, ssl->transform_out->ivlen,
1863 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001866 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001867 ssl->transform_out->ivlen,
1868 enc_msg, enc_msglen,
1869 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001872 return( ret );
1873 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001874
Paul Bakkercca5b812013-08-31 17:40:26 +02001875 if( enc_msglen != olen )
1876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001879 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1882 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001883 {
1884 /*
1885 * Save IV in SSL3 and TLS1
1886 */
1887 memcpy( ssl->transform_out->iv_enc,
1888 ssl->transform_out->cipher_ctx_enc.iv,
1889 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001891#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001894 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001895 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001896 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1897
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001898 /*
1899 * MAC(MAC_write_key, seq_num +
1900 * TLSCipherText.type +
1901 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001902 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001903 * IV + // except for TLS 1.0
1904 * ENC(content + padding + padding_length));
1905 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001906 unsigned char pseudo_hdr[13];
1907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001909
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001910 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1911 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001912 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1913 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1918 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001919 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001920 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001922
Hanno Becker3d8c9072018-01-05 16:24:22 +00001923 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1924 ssl->transform_out->maclen );
1925
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001926 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001927 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001928 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001931 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001933 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001939 /* Make extra sure authentication was performed, exactly once */
1940 if( auth_done != 1 )
1941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1943 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001944 }
1945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
1948 return( 0 );
1949}
1950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001954 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001955#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001956 size_t padlen = 0, correct = 1;
1957#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001961 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001965 }
1966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001968
Paul Bakker48916f92012-09-16 19:57:18 +00001969 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001972 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 }
1975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1977 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001978 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001979 int ret;
1980 size_t olen = 0;
1981
Paul Bakker68884e32013-01-07 18:20:04 +01001982 padlen = 0;
1983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001985 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001986 ssl->transform_in->ivlen,
1987 ssl->in_msg, ssl->in_msglen,
1988 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001991 return( ret );
1992 }
1993
1994 if( ssl->in_msglen != olen )
1995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001998 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
Paul Bakker68884e32013-01-07 18:20:04 +01002000 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002002#if defined(MBEDTLS_GCM_C) || \
2003 defined(MBEDTLS_CCM_C) || \
2004 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002006 mode == MBEDTLS_MODE_CCM ||
2007 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002008 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002009 int ret;
2010 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002011 unsigned char *dec_msg;
2012 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002013 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002014 unsigned char iv[12];
2015 mbedtls_ssl_transform *transform = ssl->transform_in;
2016 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002018 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002019
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 /*
2021 * Compute and update sizes
2022 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002023 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002026 "+ taglen (%d)", ssl->in_msglen,
2027 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002029 }
2030 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2031
Paul Bakker68884e32013-01-07 18:20:04 +01002032 dec_msg = ssl->in_msg;
2033 dec_msg_result = ssl->in_msg;
2034 ssl->in_msglen = dec_msglen;
2035
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002036 /*
2037 * Prepare additional authenticated data
2038 */
Paul Bakker68884e32013-01-07 18:20:04 +01002039 memcpy( add_data, ssl->in_ctr, 8 );
2040 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002042 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002043 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2044 add_data[12] = ssl->in_msglen & 0xFF;
2045
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002046 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002047
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002048 /*
2049 * Prepare IV
2050 */
2051 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2052 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002053 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002054 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2055 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002056
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002057 }
2058 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2059 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002060 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002061 unsigned char i;
2062
2063 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2064
2065 for( i = 0; i < 8; i++ )
2066 iv[i+4] ^= ssl->in_ctr[i];
2067 }
2068 else
2069 {
2070 /* Reminder if we ever add an AEAD mode with a different size */
2071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2073 }
2074
2075 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002077
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002078 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002079 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002083 add_data, 13,
2084 dec_msg, dec_msglen,
2085 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002086 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2091 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002092
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002093 return( ret );
2094 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002095 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002096
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002097 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002101 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002102 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2105#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002106 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 {
Paul Bakker45829992013-01-03 14:52:21 +01002109 /*
2110 * Decrypt and check the padding
2111 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002112 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002113 unsigned char *dec_msg;
2114 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002115 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002116 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002117 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002118
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 /*
Paul Bakker45829992013-01-03 14:52:21 +01002120 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2123 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002124 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002125#endif
Paul Bakker45829992013-01-03 14:52:21 +01002126
2127 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2128 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002131 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2132 ssl->transform_in->ivlen,
2133 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002135 }
2136
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002137 dec_msglen = ssl->in_msglen;
2138 dec_msg = ssl->in_msg;
2139 dec_msg_result = ssl->in_msg;
2140
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002141 /*
2142 * Authenticate before decrypt if enabled
2143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2145 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002146 {
Hanno Becker992b6872017-11-09 18:57:39 +00002147 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002148 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002151
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002152 dec_msglen -= ssl->transform_in->maclen;
2153 ssl->in_msglen -= ssl->transform_in->maclen;
2154
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002155 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2156 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2157 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2158 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2163 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002164 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002165 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002169 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002170 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002171 ssl->transform_in->maclen );
2172
Hanno Becker992b6872017-11-09 18:57:39 +00002173 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2174 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002179 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002180 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002181 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002183
2184 /*
2185 * Check length sanity
2186 */
2187 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002190 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002192 }
2193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002195 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002196 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002199 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002200 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002201 dec_msglen -= ssl->transform_in->ivlen;
2202 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002203
Paul Bakker48916f92012-09-16 19:57:18 +00002204 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002205 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002206 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002210 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002211 ssl->transform_in->ivlen,
2212 dec_msg, dec_msglen,
2213 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002216 return( ret );
2217 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002218
Paul Bakkercca5b812013-08-31 17:40:26 +02002219 if( dec_msglen != olen )
2220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002223 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2226 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002227 {
2228 /*
2229 * Save IV in SSL3 and TLS1
2230 */
2231 memcpy( ssl->transform_in->iv_dec,
2232 ssl->transform_in->cipher_ctx_dec.iv,
2233 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002238
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002239 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002240 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#if defined(MBEDTLS_SSL_DEBUG_ALL)
2243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002244 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002245#endif
Paul Bakker45829992013-01-03 14:52:21 +01002246 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002247 correct = 0;
2248 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#if defined(MBEDTLS_SSL_PROTO_SSL3)
2251 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
Paul Bakker48916f92012-09-16 19:57:18 +00002253 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#if defined(MBEDTLS_SSL_DEBUG_ALL)
2256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002258 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002259#endif
Paul Bakker45829992013-01-03 14:52:21 +01002260 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262 }
2263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2265#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2266 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2267 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 {
2269 /*
Paul Bakker45829992013-01-03 14:52:21 +01002270 * TLSv1+: always check the padding up to the first failure
2271 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002273 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002274 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002275 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002276
Paul Bakker956c9e02013-12-19 14:42:28 +01002277 /*
2278 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002279 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002280 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002281 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002282 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002283 *
2284 * In both cases we reset padding_idx to a safe value (0) to
2285 * prevent out-of-buffer reads.
2286 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002287 correct &= ( padlen <= ssl->in_msglen );
2288 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002289 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002290
2291 padding_idx *= correct;
2292
Angus Grattonb512bc12018-06-19 15:57:50 +10002293 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002294 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002295 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002296 pad_count += real_count *
2297 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2298 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002299
2300 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002303 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002305#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002306 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002308 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2310 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002314 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315
2316 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002318 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002320 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002326#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002329#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
2331 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002332 * Authenticate if not done yet.
2333 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002335#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002336 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002337 {
Hanno Becker992b6872017-11-09 18:57:39 +00002338 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002339
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002340 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002342 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2343 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#if defined(MBEDTLS_SSL_PROTO_SSL3)
2346 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002347 {
2348 ssl_mac( &ssl->transform_in->md_ctx_dec,
2349 ssl->transform_in->mac_dec,
2350 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002351 ssl->in_ctr, ssl->in_msgtype,
2352 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002353 }
2354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2356#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2357 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2358 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002359 {
2360 /*
2361 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002362 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002363 *
2364 * Known timing attacks:
2365 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2366 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002367 * To compensate for different timings for the MAC calculation
2368 * depending on how much padding was removed (which is determined
2369 * by padlen), process extra_run more blocks through the hash
2370 * function.
2371 *
2372 * The formula in the paper is
2373 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2374 * where L1 is the size of the header plus the decrypted message
2375 * plus CBC padding and L2 is the size of the header plus the
2376 * decrypted message. This is for an underlying hash function
2377 * with 64-byte blocks.
2378 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2379 * correctly. We round down instead of up, so -56 is the correct
2380 * value for our calculations instead of -55.
2381 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002382 * Repeat the formula rather than defining a block_size variable.
2383 * This avoids requiring division by a variable at runtime
2384 * (which would be marginally less efficient and would require
2385 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002386 */
2387 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002388
2389 /*
2390 * The next two sizes are the minimum and maximum values of
2391 * in_msglen over all padlen values.
2392 *
2393 * They're independent of padlen, since we previously did
2394 * in_msglen -= padlen.
2395 *
2396 * Note that max_len + maclen is never more than the buffer
2397 * length, as we previously did in_msglen -= maclen too.
2398 */
2399 const size_t max_len = ssl->in_msglen + padlen;
2400 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2401
Gilles Peskine20b44082018-05-29 14:06:49 +02002402 switch( ssl->transform_in->ciphersuite_info->mac )
2403 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002404#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2405 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002406 case MBEDTLS_MD_MD5:
2407 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002408 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002409 /* 8 bytes of message size, 64-byte compression blocks */
2410 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2411 ( 13 + ssl->in_msglen + 8 ) / 64;
2412 break;
2413#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002414#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002415 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002416 /* 16 bytes of message size, 128-byte compression blocks */
2417 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2418 ( 13 + ssl->in_msglen + 16 ) / 128;
2419 break;
2420#endif
2421 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2424 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002425
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002426 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2429 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2430 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2431 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002432 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002433 /* Make sure we access everything even when padlen > 0. This
2434 * makes the synchronisation requirements for just-in-time
2435 * Prime+Probe attacks much tighter and hopefully impractical. */
2436 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002437 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002438
2439 /* Call mbedtls_md_process at least once due to cache attacks
2440 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002441 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002445
2446 /* Make sure we access all the memory that could contain the MAC,
2447 * before we check it in the next code block. This makes the
2448 * synchronisation requirements for just-in-time Prime+Probe
2449 * attacks much tighter and hopefully impractical. */
2450 ssl_read_memory( ssl->in_msg + min_len,
2451 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002452 }
2453 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2455 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002461#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002462 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2463 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2464 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002465#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Hanno Becker992b6872017-11-09 18:57:39 +00002467 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2468 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470#if defined(MBEDTLS_SSL_DEBUG_ALL)
2471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002472#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002473 correct = 0;
2474 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002477
2478 /*
2479 * Finally check the correct flag
2480 */
2481 if( correct == 0 )
2482 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002483#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002484
2485 /* Make extra sure authentication was performed, exactly once */
2486 if( auth_done != 1 )
2487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002490 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
2492 if( ssl->in_msglen == 0 )
2493 {
Angus Gratton34817922018-06-19 15:58:22 +10002494#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2495 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2496 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2497 {
2498 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2500 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2501 }
2502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2503
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 ssl->nb_zero++;
2505
2506 /*
2507 * Three or more empty messages may be a DoS attack
2508 * (excessive CPU consumption).
2509 */
2510 if( ssl->nb_zero > 3 )
2511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 }
2516 }
2517 else
2518 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002521 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002522 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002523 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002524 }
2525 else
2526#endif
2527 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002528 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002529 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2530 if( ++ssl->in_ctr[i - 1] != 0 )
2531 break;
2532
2533 /* The loop goes to its end iff the counter is wrapping */
2534 if( i == ssl_ep_len( ssl ) )
2535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2537 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002538 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002539 }
2540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 return( 0 );
2544}
2545
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002546#undef MAC_NONE
2547#undef MAC_PLAINTEXT
2548#undef MAC_CIPHERTEXT
2549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002551/*
2552 * Compression/decompression functions
2553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002555{
2556 int ret;
2557 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002558 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002559 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002560 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002563
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002564 if( len_pre == 0 )
2565 return( 0 );
2566
Paul Bakker2770fbd2012-07-03 13:30:23 +00002567 memcpy( msg_pre, ssl->out_msg, len_pre );
2568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002570 ssl->out_msglen ) );
2571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002573 ssl->out_msg, ssl->out_msglen );
2574
Paul Bakker48916f92012-09-16 19:57:18 +00002575 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2576 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2577 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002578 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002579
Paul Bakker48916f92012-09-16 19:57:18 +00002580 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002581 if( ret != Z_OK )
2582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2584 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002585 }
2586
Angus Grattond8213d02016-05-25 20:56:48 +10002587 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002588 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002591 ssl->out_msglen ) );
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002594 ssl->out_msg, ssl->out_msglen );
2595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002597
2598 return( 0 );
2599}
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002602{
2603 int ret;
2604 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002605 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002606 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002607 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002610
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002611 if( len_pre == 0 )
2612 return( 0 );
2613
Paul Bakker2770fbd2012-07-03 13:30:23 +00002614 memcpy( msg_pre, ssl->in_msg, len_pre );
2615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002617 ssl->in_msglen ) );
2618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002620 ssl->in_msg, ssl->in_msglen );
2621
Paul Bakker48916f92012-09-16 19:57:18 +00002622 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2623 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2624 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002625 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002626 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002627
Paul Bakker48916f92012-09-16 19:57:18 +00002628 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002629 if( ret != Z_OK )
2630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2632 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002633 }
2634
Angus Grattond8213d02016-05-25 20:56:48 +10002635 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002636 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002639 ssl->in_msglen ) );
2640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002642 ssl->in_msg, ssl->in_msglen );
2643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002645
2646 return( 0 );
2647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2651static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#if defined(MBEDTLS_SSL_PROTO_DTLS)
2654static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002655{
2656 /* If renegotiation is not enforced, retransmit until we would reach max
2657 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002658 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002659 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002660 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002661 unsigned char doublings = 1;
2662
2663 while( ratio != 0 )
2664 {
2665 ++doublings;
2666 ratio >>= 1;
2667 }
2668
2669 if( ++ssl->renego_records_seen > doublings )
2670 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002672 return( 0 );
2673 }
2674 }
2675
2676 return( ssl_write_hello_request( ssl ) );
2677}
2678#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002680
Paul Bakker5121ce52009-01-03 21:22:43 +00002681/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002682 * Fill the input message buffer by appending data to it.
2683 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002684 *
2685 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2686 * available (from this read and/or a previous one). Otherwise, an error code
2687 * is returned (possibly EOF or WANT_READ).
2688 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002689 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2690 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2691 * since we always read a whole datagram at once.
2692 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002693 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002694 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002697{
Paul Bakker23986e52011-04-24 08:57:21 +00002698 int ret;
2699 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002703 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002706 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002708 }
2709
Angus Grattond8213d02016-05-25 20:56:48 +10002710 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2713 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002714 }
2715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002719 uint32_t timeout;
2720
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002721 /* Just to be sure */
2722 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2723 {
2724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2725 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2726 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2727 }
2728
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002729 /*
2730 * The point is, we need to always read a full datagram at once, so we
2731 * sometimes read more then requested, and handle the additional data.
2732 * It could be the rest of the current record (while fetching the
2733 * header) and/or some other records in the same datagram.
2734 */
2735
2736 /*
2737 * Move to the next record in the already read datagram if applicable
2738 */
2739 if( ssl->next_record_offset != 0 )
2740 {
2741 if( ssl->in_left < ssl->next_record_offset )
2742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002745 }
2746
2747 ssl->in_left -= ssl->next_record_offset;
2748
2749 if( ssl->in_left != 0 )
2750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002752 ssl->next_record_offset ) );
2753 memmove( ssl->in_hdr,
2754 ssl->in_hdr + ssl->next_record_offset,
2755 ssl->in_left );
2756 }
2757
2758 ssl->next_record_offset = 0;
2759 }
2760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002763
2764 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002765 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002766 */
2767 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002770 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002771 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002772
2773 /*
2774 * A record can't be split accross datagrams. If we need to read but
2775 * are not at the beginning of a new record, the caller did something
2776 * wrong.
2777 */
2778 if( ssl->in_left != 0 )
2779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002782 }
2783
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002784 /*
2785 * Don't even try to read if time's out already.
2786 * This avoids by-passing the timer when repeatedly receiving messages
2787 * that will end up being dropped.
2788 */
2789 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002790 {
2791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002792 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002793 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002794 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002795 {
Angus Grattond8213d02016-05-25 20:56:48 +10002796 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002799 timeout = ssl->handshake->retransmit_timeout;
2800 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002801 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002804
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002805 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002806 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2807 timeout );
2808 else
2809 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002812
2813 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002815 }
2816
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002817 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002820 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002823 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002824 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002827 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002828 }
2829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002833 return( ret );
2834 }
2835
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002836 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002837 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002839 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002841 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002842 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002845 return( ret );
2846 }
2847
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002848 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002851 }
2852
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 if( ret < 0 )
2854 return( ret );
2855
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002856 ssl->in_left = ret;
2857 }
2858 else
2859#endif
2860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002862 ssl->in_left, nb_want ) );
2863
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002864 while( ssl->in_left < nb_want )
2865 {
2866 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002867
2868 if( ssl_check_timer( ssl ) != 0 )
2869 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2870 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002871 {
2872 if( ssl->f_recv_timeout != NULL )
2873 {
2874 ret = ssl->f_recv_timeout( ssl->p_bio,
2875 ssl->in_hdr + ssl->in_left, len,
2876 ssl->conf->read_timeout );
2877 }
2878 else
2879 {
2880 ret = ssl->f_recv( ssl->p_bio,
2881 ssl->in_hdr + ssl->in_left, len );
2882 }
2883 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002886 ssl->in_left, nb_want ) );
2887 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002888
2889 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002891
2892 if( ret < 0 )
2893 return( ret );
2894
mohammad160352aecb92018-03-28 23:41:40 -07002895 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002896 {
Darryl Green11999bb2018-03-13 15:22:58 +00002897 MBEDTLS_SSL_DEBUG_MSG( 1,
2898 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002899 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2901 }
2902
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002903 ssl->in_left += ret;
2904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 }
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
2909 return( 0 );
2910}
2911
2912/*
2913 * Flush any data not yet written
2914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002916{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002917 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002918 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002922 if( ssl->f_send == NULL )
2923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002925 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002927 }
2928
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002929 /* Avoid incrementing counter if data is flushed */
2930 if( ssl->out_left == 0 )
2931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002933 return( 0 );
2934 }
2935
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 while( ssl->out_left > 0 )
2937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2939 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Hanno Becker2b1e3542018-08-06 11:19:13 +01002941 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002942 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
2946 if( ret <= 0 )
2947 return( ret );
2948
mohammad160352aecb92018-03-28 23:41:40 -07002949 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002950 {
Darryl Green11999bb2018-03-13 15:22:58 +00002951 MBEDTLS_SSL_DEBUG_MSG( 1,
2952 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002953 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2955 }
2956
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 ssl->out_left -= ret;
2958 }
2959
Hanno Becker2b1e3542018-08-06 11:19:13 +01002960#if defined(MBEDTLS_SSL_PROTO_DTLS)
2961 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002962 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002963 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002964 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002965 else
2966#endif
2967 {
2968 ssl->out_hdr = ssl->out_buf + 8;
2969 }
2970 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
2974 return( 0 );
2975}
2976
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002977/*
2978 * Functions to handle the DTLS retransmission state machine
2979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002981/*
2982 * Append current handshake message to current outgoing flight
2983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2988 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2989 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002990
2991 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002992 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002993 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002996 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002997 }
2998
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002999 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003000 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003003 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003004 }
3005
3006 /* Copy current handshake message with headers */
3007 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3008 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003009 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003010 msg->next = NULL;
3011
3012 /* Append to the current flight */
3013 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003014 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003015 else
3016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003018 while( cur->next != NULL )
3019 cur = cur->next;
3020 cur->next = msg;
3021 }
3022
Hanno Becker3b235902018-08-06 09:54:53 +01003023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003024 return( 0 );
3025}
3026
3027/*
3028 * Free the current flight of handshake messages
3029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 mbedtls_ssl_flight_item *cur = flight;
3033 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003034
3035 while( cur != NULL )
3036 {
3037 next = cur->next;
3038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 mbedtls_free( cur->p );
3040 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003041
3042 cur = next;
3043 }
3044}
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3047static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003048#endif
3049
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003050/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003051 * Swap transform_out and out_ctr with the alternative ones
3052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003056 unsigned char tmp_out_ctr[8];
3057
3058 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003061 return;
3062 }
3063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003065
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003066 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003067 tmp_transform = ssl->transform_out;
3068 ssl->transform_out = ssl->handshake->alt_transform_out;
3069 ssl->handshake->alt_transform_out = tmp_transform;
3070
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003071 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003072 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3073 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003074 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003075
3076 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003077 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3080 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3085 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003086 }
3087 }
3088#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003089}
3090
3091/*
3092 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003093 */
3094int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3095{
3096 int ret = 0;
3097
3098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3099
3100 ret = mbedtls_ssl_flight_transmit( ssl );
3101
3102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3103
3104 return( ret );
3105}
3106
3107/*
3108 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003109 *
3110 * Need to remember the current message in case flush_output returns
3111 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003112 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003113 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003114int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003115{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003116 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003120 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003122
3123 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003124 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003125 ssl_swap_epochs( ssl );
3126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003128 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003129
3130 while( ssl->handshake->cur_msg != NULL )
3131 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003132 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003133 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003134
Hanno Beckere1dcb032018-08-17 16:47:58 +01003135 int const is_finished =
3136 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3137 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3138
Hanno Becker04da1892018-08-14 13:22:10 +01003139 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3140 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3141
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003142 /* Swap epochs before sending Finished: we can't do it after
3143 * sending ChangeCipherSpec, in case write returns WANT_READ.
3144 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003145 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003146 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003148 ssl_swap_epochs( ssl );
3149 }
3150
Hanno Becker67bc7c32018-08-06 11:33:50 +01003151 ret = ssl_get_remaining_payload_in_datagram( ssl );
3152 if( ret < 0 )
3153 return( ret );
3154 max_frag_len = (size_t) ret;
3155
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003156 /* CCS is copied as is, while HS messages may need fragmentation */
3157 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3158 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003159 if( max_frag_len == 0 )
3160 {
3161 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3162 return( ret );
3163
3164 continue;
3165 }
3166
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003167 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003168 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003169 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003171 /* Update position inside current message */
3172 ssl->handshake->cur_msg_p += cur->len;
3173 }
3174 else
3175 {
3176 const unsigned char * const p = ssl->handshake->cur_msg_p;
3177 const size_t hs_len = cur->len - 12;
3178 const size_t frag_off = p - ( cur->p + 12 );
3179 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003180 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003181
Hanno Beckere1dcb032018-08-17 16:47:58 +01003182 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003183 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003184 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003185 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003186
Hanno Becker67bc7c32018-08-06 11:33:50 +01003187 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3188 return( ret );
3189
3190 continue;
3191 }
3192 max_hs_frag_len = max_frag_len - 12;
3193
3194 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3195 max_hs_frag_len : rem_len;
3196
3197 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003198 {
3199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003200 (unsigned) cur_hs_frag_len,
3201 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003202 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003203
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003204 /* Messages are stored with handshake headers as if not fragmented,
3205 * copy beginning of headers then fill fragmentation fields.
3206 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3207 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003208
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003209 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3210 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3211 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3212
Hanno Becker67bc7c32018-08-06 11:33:50 +01003213 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3214 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3215 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003216
3217 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3218
Hanno Becker3f7b9732018-08-28 09:53:25 +01003219 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003220 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3221 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003222 ssl->out_msgtype = cur->type;
3223
3224 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003225 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003226 }
3227
3228 /* If done with the current message move to the next one if any */
3229 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3230 {
3231 if( cur->next != NULL )
3232 {
3233 ssl->handshake->cur_msg = cur->next;
3234 ssl->handshake->cur_msg_p = cur->next->p + 12;
3235 }
3236 else
3237 {
3238 ssl->handshake->cur_msg = NULL;
3239 ssl->handshake->cur_msg_p = NULL;
3240 }
3241 }
3242
3243 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003244 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003247 return( ret );
3248 }
3249 }
3250
Hanno Becker67bc7c32018-08-06 11:33:50 +01003251 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3252 return( ret );
3253
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003254 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3256 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003257 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003260 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3261 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003262
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003264
3265 return( 0 );
3266}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003267
3268/*
3269 * To be called when the last message of an incoming flight is received.
3270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003272{
3273 /* We won't need to resend that one any more */
3274 ssl_flight_free( ssl->handshake->flight );
3275 ssl->handshake->flight = NULL;
3276 ssl->handshake->cur_msg = NULL;
3277
3278 /* The next incoming flight will start with this msg_seq */
3279 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3280
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003281 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003282 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003283
Hanno Becker0271f962018-08-16 13:23:47 +01003284 /* Clear future message buffering structure. */
3285 ssl_buffering_free( ssl );
3286
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003287 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003288 ssl_set_timer( ssl, 0 );
3289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3291 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003294 }
3295 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003297}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003298
3299/*
3300 * To be called when the last message of an outgoing flight is send.
3301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003303{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003304 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003305 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3308 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003311 }
3312 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003316
Paul Bakker5121ce52009-01-03 21:22:43 +00003317/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003318 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320
3321/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003322 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003323 *
3324 * - fill in handshake headers
3325 * - update handshake checksum
3326 * - DTLS: save message for resending
3327 * - then pass to the record layer
3328 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003329 * DTLS: except for HelloRequest, messages are only queued, and will only be
3330 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003331 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003332 * Inputs:
3333 * - ssl->out_msglen: 4 + actual handshake message len
3334 * (4 is the size of handshake headers for TLS)
3335 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3336 * - ssl->out_msg + 4: the handshake message body
3337 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003338 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003339 * - ssl->out_msglen: the length of the record contents
3340 * (including handshake headers but excluding record headers)
3341 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003343int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003344{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003345 int ret;
3346 const size_t hs_len = ssl->out_msglen - 4;
3347 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003348
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3350
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003351 /*
3352 * Sanity checks
3353 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003354 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003355 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3356 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003357 /* In SSLv3, the client might send a NoCertificate alert. */
3358#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3359 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3360 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3361 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3362#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3363 {
3364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3365 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3366 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003369 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3370 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3371 ssl->handshake == NULL )
3372 {
3373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3374 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003379 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003381 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3383 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003385#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003386
Hanno Beckerb50a2532018-08-06 11:52:54 +01003387 /* Double-check that we did not exceed the bounds
3388 * of the outgoing record buffer.
3389 * This should never fail as the various message
3390 * writing functions must obey the bounds of the
3391 * outgoing record buffer, but better be safe.
3392 *
3393 * Note: We deliberately do not check for the MTU or MFL here.
3394 */
3395 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3396 {
3397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3398 "size %u, maximum %u",
3399 (unsigned) ssl->out_msglen,
3400 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3402 }
3403
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003404 /*
3405 * Fill handshake headers
3406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003409 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3410 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3411 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003413 /*
3414 * DTLS has additional fields in the Handshake layer,
3415 * between the length field and the actual payload:
3416 * uint16 message_seq;
3417 * uint24 fragment_offset;
3418 * uint24 fragment_length;
3419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003422 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003423 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003424 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003425 {
3426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3427 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003428 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003429 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3431 }
3432
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003433 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003434 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003435
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003436 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003437 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003438 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003439 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3440 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3441 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003442 }
3443 else
3444 {
3445 ssl->out_msg[4] = 0;
3446 ssl->out_msg[5] = 0;
3447 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003448
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003449 /* Handshake hashes are computed without fragmentation,
3450 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003451 memset( ssl->out_msg + 6, 0x00, 3 );
3452 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003455
Hanno Becker0207e532018-08-28 10:28:28 +01003456 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003457 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3458 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003461 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003463 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003464 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3465 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003466 {
3467 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003470 return( ret );
3471 }
3472 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003473 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003474#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003475 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003476 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003477 {
3478 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3479 return( ret );
3480 }
3481 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003482
3483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3484
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003485 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003486}
3487
3488/*
3489 * Record layer functions
3490 */
3491
3492/*
3493 * Write current record.
3494 *
3495 * Uses:
3496 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3497 * - ssl->out_msglen: length of the record content (excl headers)
3498 * - ssl->out_msg: record content
3499 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003500int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003501{
3502 int ret, done = 0;
3503 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003504 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003505
3506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003509 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511 {
3512 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003515 return( ret );
3516 }
3517
3518 len = ssl->out_msglen;
3519 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3523 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 ret = mbedtls_ssl_hw_record_write( ssl );
3528 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3531 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003532 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003533
3534 if( ret == 0 )
3535 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003538 if( !done )
3539 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003540 unsigned i;
3541 size_t protected_record_size;
3542
Paul Bakker05ef8352012-05-08 09:17:57 +00003543 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003545 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003546
Hanno Becker19859472018-08-06 09:40:20 +01003547 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003548 ssl->out_len[0] = (unsigned char)( len >> 8 );
3549 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003550
Paul Bakker48916f92012-09-16 19:57:18 +00003551 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003552 {
3553 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003556 return( ret );
3557 }
3558
3559 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003560 ssl->out_len[0] = (unsigned char)( len >> 8 );
3561 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003562 }
3563
Hanno Becker2b1e3542018-08-06 11:19:13 +01003564 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3565
3566#if defined(MBEDTLS_SSL_PROTO_DTLS)
3567 /* In case of DTLS, double-check that we don't exceed
3568 * the remaining space in the datagram. */
3569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3570 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003571 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003572 if( ret < 0 )
3573 return( ret );
3574
3575 if( protected_record_size > (size_t) ret )
3576 {
3577 /* Should never happen */
3578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3579 }
3580 }
3581#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003584 "version = [%d:%d], msglen = %d",
3585 ssl->out_hdr[0], ssl->out_hdr[1],
3586 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003589 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003590
3591 ssl->out_left += protected_record_size;
3592 ssl->out_hdr += protected_record_size;
3593 ssl_update_out_pointers( ssl, ssl->transform_out );
3594
Hanno Becker04484622018-08-06 09:49:38 +01003595 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3596 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3597 break;
3598
3599 /* The loop goes to its end iff the counter is wrapping */
3600 if( i == ssl_ep_len( ssl ) )
3601 {
3602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3603 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 }
3606
Hanno Becker67bc7c32018-08-06 11:33:50 +01003607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003608 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3609 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003610 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003611 size_t remaining;
3612 ret = ssl_get_remaining_payload_in_datagram( ssl );
3613 if( ret < 0 )
3614 {
3615 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3616 ret );
3617 return( ret );
3618 }
3619
3620 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003621 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003622 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003623 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003624 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003625 else
3626 {
Hanno Becker513815a2018-08-20 11:56:09 +01003627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003628 }
3629 }
3630#endif /* MBEDTLS_SSL_PROTO_DTLS */
3631
3632 if( ( flush == SSL_FORCE_FLUSH ) &&
3633 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 return( ret );
3637 }
3638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003640
3641 return( 0 );
3642}
3643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003645
3646static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3647{
3648 if( ssl->in_msglen < ssl->in_hslen ||
3649 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3650 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3651 {
3652 return( 1 );
3653 }
3654 return( 0 );
3655}
Hanno Becker44650b72018-08-16 12:51:11 +01003656
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003657static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003658{
3659 return( ( ssl->in_msg[9] << 16 ) |
3660 ( ssl->in_msg[10] << 8 ) |
3661 ssl->in_msg[11] );
3662}
3663
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003664static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003665{
3666 return( ( ssl->in_msg[6] << 16 ) |
3667 ( ssl->in_msg[7] << 8 ) |
3668 ssl->in_msg[8] );
3669}
3670
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003671static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003672{
3673 uint32_t msg_len, frag_off, frag_len;
3674
3675 msg_len = ssl_get_hs_total_len( ssl );
3676 frag_off = ssl_get_hs_frag_off( ssl );
3677 frag_len = ssl_get_hs_frag_len( ssl );
3678
3679 if( frag_off > msg_len )
3680 return( -1 );
3681
3682 if( frag_len > msg_len - frag_off )
3683 return( -1 );
3684
3685 if( frag_len + 12 > ssl->in_msglen )
3686 return( -1 );
3687
3688 return( 0 );
3689}
3690
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003691/*
3692 * Mark bits in bitmask (used for DTLS HS reassembly)
3693 */
3694static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3695{
3696 unsigned int start_bits, end_bits;
3697
3698 start_bits = 8 - ( offset % 8 );
3699 if( start_bits != 8 )
3700 {
3701 size_t first_byte_idx = offset / 8;
3702
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003703 /* Special case */
3704 if( len <= start_bits )
3705 {
3706 for( ; len != 0; len-- )
3707 mask[first_byte_idx] |= 1 << ( start_bits - len );
3708
3709 /* Avoid potential issues with offset or len becoming invalid */
3710 return;
3711 }
3712
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003713 offset += start_bits; /* Now offset % 8 == 0 */
3714 len -= start_bits;
3715
3716 for( ; start_bits != 0; start_bits-- )
3717 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3718 }
3719
3720 end_bits = len % 8;
3721 if( end_bits != 0 )
3722 {
3723 size_t last_byte_idx = ( offset + len ) / 8;
3724
3725 len -= end_bits; /* Now len % 8 == 0 */
3726
3727 for( ; end_bits != 0; end_bits-- )
3728 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3729 }
3730
3731 memset( mask + offset / 8, 0xFF, len / 8 );
3732}
3733
3734/*
3735 * Check that bitmask is full
3736 */
3737static int ssl_bitmask_check( unsigned char *mask, size_t len )
3738{
3739 size_t i;
3740
3741 for( i = 0; i < len / 8; i++ )
3742 if( mask[i] != 0xFF )
3743 return( -1 );
3744
3745 for( i = 0; i < len % 8; i++ )
3746 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3747 return( -1 );
3748
3749 return( 0 );
3750}
3751
Hanno Becker56e205e2018-08-16 09:06:12 +01003752/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003753static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003754 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003755{
Hanno Becker56e205e2018-08-16 09:06:12 +01003756 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003757
Hanno Becker56e205e2018-08-16 09:06:12 +01003758 alloc_len = 12; /* Handshake header */
3759 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003760
Hanno Beckerd07df862018-08-16 09:14:58 +01003761 if( add_bitmap )
3762 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003763
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003764 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003765}
Hanno Becker56e205e2018-08-16 09:06:12 +01003766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003768
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003769static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003770{
3771 return( ( ssl->in_msg[1] << 16 ) |
3772 ( ssl->in_msg[2] << 8 ) |
3773 ssl->in_msg[3] );
3774}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003775
Simon Butcher99000142016-10-13 17:21:01 +01003776int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003781 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003783 }
3784
Hanno Becker12555c62018-08-16 12:47:53 +01003785 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003788 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003789 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003793 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003794 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003795 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003796
Hanno Becker44650b72018-08-16 12:51:11 +01003797 if( ssl_check_hs_header( ssl ) != 0 )
3798 {
3799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3800 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3801 }
3802
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003803 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003804 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3805 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3806 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3807 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003808 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003809 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3810 {
3811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3812 recv_msg_seq,
3813 ssl->handshake->in_msg_seq ) );
3814 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3815 }
3816
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003817 /* Retransmit only on last message from previous flight, to avoid
3818 * too many retransmissions.
3819 * Besides, No sane server ever retransmits HelloVerifyRequest */
3820 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003824 "message_seq = %d, start_of_flight = %d",
3825 recv_msg_seq,
3826 ssl->handshake->in_flight_start_seq ) );
3827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003831 return( ret );
3832 }
3833 }
3834 else
3835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003837 "message_seq = %d, expected = %d",
3838 recv_msg_seq,
3839 ssl->handshake->in_msg_seq ) );
3840 }
3841
Hanno Becker90333da2017-10-10 11:27:13 +01003842 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003843 }
3844 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003845
Hanno Becker6d97ef52018-08-16 13:09:04 +01003846 /* Message reassembly is handled alongside buffering of future
3847 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003848 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003849 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003850 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003853 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003854 }
3855 }
3856 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003858 /* With TLS we don't handle fragmentation (for now) */
3859 if( ssl->in_msglen < ssl->in_hslen )
3860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3862 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003863 }
3864
Simon Butcher99000142016-10-13 17:21:01 +01003865 return( 0 );
3866}
3867
3868void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3869{
Hanno Becker0271f962018-08-16 13:23:47 +01003870 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003871
Hanno Becker0271f962018-08-16 13:23:47 +01003872 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003873 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003874 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003875 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003876
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003877 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003880 ssl->handshake != NULL )
3881 {
Hanno Becker0271f962018-08-16 13:23:47 +01003882 unsigned offset;
3883 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003884
Hanno Becker0271f962018-08-16 13:23:47 +01003885 /* Increment handshake sequence number */
3886 hs->in_msg_seq++;
3887
3888 /*
3889 * Clear up handshake buffering and reassembly structure.
3890 */
3891
3892 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003893 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003894
3895 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003896 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3897 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003898 offset++, hs_buf++ )
3899 {
3900 *hs_buf = *(hs_buf + 1);
3901 }
3902
3903 /* Create a fresh last entry */
3904 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003905 }
3906#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003907}
3908
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003909/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003910 * DTLS anti-replay: RFC 6347 4.1.2.6
3911 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003912 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3913 * Bit n is set iff record number in_window_top - n has been seen.
3914 *
3915 * Usually, in_window_top is the last record number seen and the lsb of
3916 * in_window is set. The only exception is the initial state (record number 0
3917 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3920static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003921{
3922 ssl->in_window_top = 0;
3923 ssl->in_window = 0;
3924}
3925
3926static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3927{
3928 return( ( (uint64_t) buf[0] << 40 ) |
3929 ( (uint64_t) buf[1] << 32 ) |
3930 ( (uint64_t) buf[2] << 24 ) |
3931 ( (uint64_t) buf[3] << 16 ) |
3932 ( (uint64_t) buf[4] << 8 ) |
3933 ( (uint64_t) buf[5] ) );
3934}
3935
3936/*
3937 * Return 0 if sequence number is acceptable, -1 otherwise
3938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003940{
3941 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3942 uint64_t bit;
3943
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003944 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003945 return( 0 );
3946
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003947 if( rec_seqnum > ssl->in_window_top )
3948 return( 0 );
3949
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003950 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003951
3952 if( bit >= 64 )
3953 return( -1 );
3954
3955 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3956 return( -1 );
3957
3958 return( 0 );
3959}
3960
3961/*
3962 * Update replay window on new validated record
3963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003965{
3966 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3967
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003968 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003969 return;
3970
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003971 if( rec_seqnum > ssl->in_window_top )
3972 {
3973 /* Update window_top and the contents of the window */
3974 uint64_t shift = rec_seqnum - ssl->in_window_top;
3975
3976 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003977 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003978 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003979 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003980 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003981 ssl->in_window |= 1;
3982 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003983
3984 ssl->in_window_top = rec_seqnum;
3985 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003986 else
3987 {
3988 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003989 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003990
3991 if( bit < 64 ) /* Always true, but be extra sure */
3992 ssl->in_window |= (uint64_t) 1 << bit;
3993 }
3994}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003996
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003997#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003998/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003999static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4000
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004001/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004002 * Without any SSL context, check if a datagram looks like a ClientHello with
4003 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004004 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004005 *
4006 * - if cookie is valid, return 0
4007 * - if ClientHello looks superficially valid but cookie is not,
4008 * fill obuf and set olen, then
4009 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4010 * - otherwise return a specific error code
4011 */
4012static int ssl_check_dtls_clihlo_cookie(
4013 mbedtls_ssl_cookie_write_t *f_cookie_write,
4014 mbedtls_ssl_cookie_check_t *f_cookie_check,
4015 void *p_cookie,
4016 const unsigned char *cli_id, size_t cli_id_len,
4017 const unsigned char *in, size_t in_len,
4018 unsigned char *obuf, size_t buf_len, size_t *olen )
4019{
4020 size_t sid_len, cookie_len;
4021 unsigned char *p;
4022
4023 if( f_cookie_write == NULL || f_cookie_check == NULL )
4024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4025
4026 /*
4027 * Structure of ClientHello with record and handshake headers,
4028 * and expected values. We don't need to check a lot, more checks will be
4029 * done when actually parsing the ClientHello - skipping those checks
4030 * avoids code duplication and does not make cookie forging any easier.
4031 *
4032 * 0-0 ContentType type; copied, must be handshake
4033 * 1-2 ProtocolVersion version; copied
4034 * 3-4 uint16 epoch; copied, must be 0
4035 * 5-10 uint48 sequence_number; copied
4036 * 11-12 uint16 length; (ignored)
4037 *
4038 * 13-13 HandshakeType msg_type; (ignored)
4039 * 14-16 uint24 length; (ignored)
4040 * 17-18 uint16 message_seq; copied
4041 * 19-21 uint24 fragment_offset; copied, must be 0
4042 * 22-24 uint24 fragment_length; (ignored)
4043 *
4044 * 25-26 ProtocolVersion client_version; (ignored)
4045 * 27-58 Random random; (ignored)
4046 * 59-xx SessionID session_id; 1 byte len + sid_len content
4047 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4048 * ...
4049 *
4050 * Minimum length is 61 bytes.
4051 */
4052 if( in_len < 61 ||
4053 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4054 in[3] != 0 || in[4] != 0 ||
4055 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4056 {
4057 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4058 }
4059
4060 sid_len = in[59];
4061 if( sid_len > in_len - 61 )
4062 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4063
4064 cookie_len = in[60 + sid_len];
4065 if( cookie_len > in_len - 60 )
4066 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4067
4068 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4069 cli_id, cli_id_len ) == 0 )
4070 {
4071 /* Valid cookie */
4072 return( 0 );
4073 }
4074
4075 /*
4076 * If we get here, we've got an invalid cookie, let's prepare HVR.
4077 *
4078 * 0-0 ContentType type; copied
4079 * 1-2 ProtocolVersion version; copied
4080 * 3-4 uint16 epoch; copied
4081 * 5-10 uint48 sequence_number; copied
4082 * 11-12 uint16 length; olen - 13
4083 *
4084 * 13-13 HandshakeType msg_type; hello_verify_request
4085 * 14-16 uint24 length; olen - 25
4086 * 17-18 uint16 message_seq; copied
4087 * 19-21 uint24 fragment_offset; copied
4088 * 22-24 uint24 fragment_length; olen - 25
4089 *
4090 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4091 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4092 *
4093 * Minimum length is 28.
4094 */
4095 if( buf_len < 28 )
4096 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4097
4098 /* Copy most fields and adapt others */
4099 memcpy( obuf, in, 25 );
4100 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4101 obuf[25] = 0xfe;
4102 obuf[26] = 0xff;
4103
4104 /* Generate and write actual cookie */
4105 p = obuf + 28;
4106 if( f_cookie_write( p_cookie,
4107 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4108 {
4109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4110 }
4111
4112 *olen = p - obuf;
4113
4114 /* Go back and fill length fields */
4115 obuf[27] = (unsigned char)( *olen - 28 );
4116
4117 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4118 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4119 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4120
4121 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4122 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4123
4124 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4125}
4126
4127/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004128 * Handle possible client reconnect with the same UDP quadruplet
4129 * (RFC 6347 Section 4.2.8).
4130 *
4131 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4132 * that looks like a ClientHello.
4133 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004134 * - if the input looks like a ClientHello without cookies,
4135 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004136 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004137 * - if the input looks like a ClientHello with a valid cookie,
4138 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004139 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004140 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004141 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004142 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004143 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4144 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004145 */
4146static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4147{
4148 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004149 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004150
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004151 ret = ssl_check_dtls_clihlo_cookie(
4152 ssl->conf->f_cookie_write,
4153 ssl->conf->f_cookie_check,
4154 ssl->conf->p_cookie,
4155 ssl->cli_id, ssl->cli_id_len,
4156 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004157 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004158
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004159 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4160
4161 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004162 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004163 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004164 * If the error is permanent we'll catch it later,
4165 * if it's not, then hopefully it'll work next time. */
4166 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4167
4168 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004169 }
4170
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004171 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004172 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004173 /* Got a valid cookie, partially reset context */
4174 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4175 {
4176 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4177 return( ret );
4178 }
4179
4180 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004181 }
4182
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004183 return( ret );
4184}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004185#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004186
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004187/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004188 * ContentType type;
4189 * ProtocolVersion version;
4190 * uint16 epoch; // DTLS only
4191 * uint48 sequence_number; // DTLS only
4192 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004193 *
4194 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004195 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004196 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4197 *
4198 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004199 * 1. proceed with the record if this function returns 0
4200 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4201 * 3. return CLIENT_RECONNECT if this function return that value
4202 * 4. drop the whole datagram if this function returns anything else.
4203 * Point 2 is needed when the peer is resending, and we have already received
4204 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004207{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004208 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 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 +02004211
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004213 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004214 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004217 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004218 ssl->in_msgtype,
4219 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004220
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004221 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4223 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4224 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4225 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004228
4229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004230 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4231 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004232 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4233#endif /* MBEDTLS_SSL_PROTO_DTLS */
4234 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4235 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004238 }
4239
4240 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004241 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4244 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004245 }
4246
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004247 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4250 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004251 }
4252
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004253 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004254 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004255 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4258 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004259 }
4260
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004261 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004262 * DTLS-related tests.
4263 * Check epoch before checking length constraint because
4264 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4265 * message gets duplicated before the corresponding Finished message,
4266 * the second ChangeCipherSpec should be discarded because it belongs
4267 * to an old epoch, but not because its length is shorter than
4268 * the minimum record length for packets using the new record transform.
4269 * Note that these two kinds of failures are handled differently,
4270 * as an unexpected record is silently skipped but an invalid
4271 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004272 */
4273#if defined(MBEDTLS_SSL_PROTO_DTLS)
4274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4275 {
4276 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4277
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004278 /* Check epoch (and sequence number) with DTLS */
4279 if( rec_epoch != ssl->in_epoch )
4280 {
4281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4282 "expected %d, received %d",
4283 ssl->in_epoch, rec_epoch ) );
4284
4285#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4286 /*
4287 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4288 * access the first byte of record content (handshake type), as we
4289 * have an active transform (possibly iv_len != 0), so use the
4290 * fact that the record header len is 13 instead.
4291 */
4292 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4293 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4294 rec_epoch == 0 &&
4295 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4296 ssl->in_left > 13 &&
4297 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4298 {
4299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4300 "from the same port" ) );
4301 return( ssl_handle_possible_reconnect( ssl ) );
4302 }
4303 else
4304#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004305 {
4306 /* Consider buffering the record. */
4307 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4308 {
4309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4310 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4311 }
4312
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004313 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004314 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004315 }
4316
4317#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4318 /* Replay detection only works for the current epoch */
4319 if( rec_epoch == ssl->in_epoch &&
4320 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4321 {
4322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4323 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4324 }
4325#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004326
Hanno Becker52c6dc62017-05-26 16:07:36 +01004327 /* Drop unexpected ApplicationData records,
4328 * except at the beginning of renegotiations */
4329 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4330 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4331#if defined(MBEDTLS_SSL_RENEGOTIATION)
4332 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4333 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4334#endif
4335 )
4336 {
4337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4338 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4339 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004340 }
4341#endif /* MBEDTLS_SSL_PROTO_DTLS */
4342
Hanno Becker52c6dc62017-05-26 16:07:36 +01004343
4344 /* Check length against bounds of the current transform and version */
4345 if( ssl->transform_in == NULL )
4346 {
4347 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004348 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004349 {
4350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4351 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4352 }
4353 }
4354 else
4355 {
4356 if( ssl->in_msglen < ssl->transform_in->minlen )
4357 {
4358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4359 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4360 }
4361
4362#if defined(MBEDTLS_SSL_PROTO_SSL3)
4363 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004364 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004365 {
4366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4367 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4368 }
4369#endif
4370#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4371 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4372 /*
4373 * TLS encrypted messages can have up to 256 bytes of padding
4374 */
4375 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4376 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004377 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004378 {
4379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4380 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4381 }
4382#endif
4383 }
4384
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004385 return( 0 );
4386}
Paul Bakker5121ce52009-01-03 21:22:43 +00004387
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004388/*
4389 * If applicable, decrypt (and decompress) record content
4390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004392{
4393 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4396 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4399 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 ret = mbedtls_ssl_hw_record_read( ssl );
4404 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4407 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004408 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004409
4410 if( ret == 0 )
4411 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004412 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004414 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 {
4416 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004419 return( ret );
4420 }
4421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 ssl->in_msg, ssl->in_msglen );
4424
Angus Grattond8213d02016-05-25 20:56:48 +10004425 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4428 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 }
4430 }
4431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004433 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004435 {
4436 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004439 return( ret );
4440 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004448 }
4449#endif
4450
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004451 return( 0 );
4452}
4453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004455
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004456/*
4457 * Read a record.
4458 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004459 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4460 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4461 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004462 */
Hanno Becker1097b342018-08-15 14:09:41 +01004463
4464/* Helper functions for mbedtls_ssl_read_record(). */
4465static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004466static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4467static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004468
Hanno Becker327c93b2018-08-15 13:56:18 +01004469int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004470 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004471{
4472 int ret;
4473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004475
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004476 if( ssl->keep_current_message == 0 )
4477 {
4478 do {
Simon Butcher99000142016-10-13 17:21:01 +01004479
Hanno Becker26994592018-08-15 14:14:59 +01004480 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004481 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004482 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004483
Hanno Beckere74d5562018-08-15 14:26:08 +01004484 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004485 {
Hanno Becker40f50842018-08-15 14:48:01 +01004486#if defined(MBEDTLS_SSL_PROTO_DTLS)
4487 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004488
Hanno Becker40f50842018-08-15 14:48:01 +01004489 /* We only check for buffered messages if the
4490 * current datagram is fully consumed. */
4491 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004492 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004493 {
Hanno Becker40f50842018-08-15 14:48:01 +01004494 if( ssl_load_buffered_message( ssl ) == 0 )
4495 have_buffered = 1;
4496 }
4497
4498 if( have_buffered == 0 )
4499#endif /* MBEDTLS_SSL_PROTO_DTLS */
4500 {
4501 ret = ssl_get_next_record( ssl );
4502 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4503 continue;
4504
4505 if( ret != 0 )
4506 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004507 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004508 return( ret );
4509 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004510 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004511 }
4512
4513 ret = mbedtls_ssl_handle_message_type( ssl );
4514
Hanno Becker40f50842018-08-15 14:48:01 +01004515#if defined(MBEDTLS_SSL_PROTO_DTLS)
4516 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4517 {
4518 /* Buffer future message */
4519 ret = ssl_buffer_message( ssl );
4520 if( ret != 0 )
4521 return( ret );
4522
4523 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4524 }
4525#endif /* MBEDTLS_SSL_PROTO_DTLS */
4526
Hanno Becker90333da2017-10-10 11:27:13 +01004527 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4528 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004529
4530 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004531 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004532 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004533 return( ret );
4534 }
4535
Hanno Becker327c93b2018-08-15 13:56:18 +01004536 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004537 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004538 {
4539 mbedtls_ssl_update_handshake_status( ssl );
4540 }
Simon Butcher99000142016-10-13 17:21:01 +01004541 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004542 else
Simon Butcher99000142016-10-13 17:21:01 +01004543 {
Hanno Becker02f59072018-08-15 14:00:24 +01004544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004545 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004546 }
4547
4548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4549
4550 return( 0 );
4551}
4552
Hanno Becker40f50842018-08-15 14:48:01 +01004553#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004554static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004555{
Hanno Becker40f50842018-08-15 14:48:01 +01004556 if( ssl->in_left > ssl->next_record_offset )
4557 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004558
Hanno Becker40f50842018-08-15 14:48:01 +01004559 return( 0 );
4560}
4561
4562static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4563{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004564 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004565 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004566 int ret = 0;
4567
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004568 if( hs == NULL )
4569 return( -1 );
4570
Hanno Beckere00ae372018-08-20 09:39:42 +01004571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4572
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004573 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4574 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4575 {
4576 /* Check if we have seen a ChangeCipherSpec before.
4577 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004578 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004579 {
4580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4581 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004582 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004583 }
4584
Hanno Becker39b8bc92018-08-28 17:17:13 +01004585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004586 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4587 ssl->in_msglen = 1;
4588 ssl->in_msg[0] = 1;
4589
4590 /* As long as they are equal, the exact value doesn't matter. */
4591 ssl->in_left = 0;
4592 ssl->next_record_offset = 0;
4593
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004594 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004595 goto exit;
4596 }
Hanno Becker37f95322018-08-16 13:55:32 +01004597
Hanno Beckerb8f50142018-08-28 10:01:34 +01004598#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004599 /* Debug only */
4600 {
4601 unsigned offset;
4602 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4603 {
4604 hs_buf = &hs->buffering.hs[offset];
4605 if( hs_buf->is_valid == 1 )
4606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4608 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004609 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004610 }
4611 }
4612 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004613#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004614
4615 /* Check if we have buffered and/or fully reassembled the
4616 * next handshake message. */
4617 hs_buf = &hs->buffering.hs[0];
4618 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4619 {
4620 /* Synthesize a record containing the buffered HS message. */
4621 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4622 ( hs_buf->data[2] << 8 ) |
4623 hs_buf->data[3];
4624
4625 /* Double-check that we haven't accidentally buffered
4626 * a message that doesn't fit into the input buffer. */
4627 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4628 {
4629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4631 }
4632
4633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4634 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4635 hs_buf->data, msg_len + 12 );
4636
4637 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4638 ssl->in_hslen = msg_len + 12;
4639 ssl->in_msglen = msg_len + 12;
4640 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4641
4642 ret = 0;
4643 goto exit;
4644 }
4645 else
4646 {
4647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4648 hs->in_msg_seq ) );
4649 }
4650
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004651 ret = -1;
4652
4653exit:
4654
4655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4656 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004657}
4658
Hanno Beckera02b0b42018-08-21 17:20:27 +01004659static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4660 size_t desired )
4661{
4662 int offset;
4663 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4665 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004666
Hanno Becker01315ea2018-08-21 17:22:17 +01004667 /* Get rid of future records epoch first, if such exist. */
4668 ssl_free_buffered_record( ssl );
4669
4670 /* Check if we have enough space available now. */
4671 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4672 hs->buffering.total_bytes_buffered ) )
4673 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004675 return( 0 );
4676 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004677
Hanno Becker4f432ad2018-08-28 10:02:32 +01004678 /* We don't have enough space to buffer the next expected handshake
4679 * message. Remove buffers used for future messages to gain space,
4680 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004681 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4682 offset >= 0; offset-- )
4683 {
4684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4685 offset ) );
4686
Hanno Beckerb309b922018-08-23 13:18:05 +01004687 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004688
4689 /* Check if we have enough space available now. */
4690 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4691 hs->buffering.total_bytes_buffered ) )
4692 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004694 return( 0 );
4695 }
4696 }
4697
4698 return( -1 );
4699}
4700
Hanno Becker40f50842018-08-15 14:48:01 +01004701static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4702{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004703 int ret = 0;
4704 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4705
4706 if( hs == NULL )
4707 return( 0 );
4708
4709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4710
4711 switch( ssl->in_msgtype )
4712 {
4713 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004715
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004716 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004717 break;
4718
4719 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004720 {
4721 unsigned recv_msg_seq_offset;
4722 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4723 mbedtls_ssl_hs_buffer *hs_buf;
4724 size_t msg_len = ssl->in_hslen - 12;
4725
4726 /* We should never receive an old handshake
4727 * message - double-check nonetheless. */
4728 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4729 {
4730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4731 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4732 }
4733
4734 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4735 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4736 {
4737 /* Silently ignore -- message too far in the future */
4738 MBEDTLS_SSL_DEBUG_MSG( 2,
4739 ( "Ignore future HS message with sequence number %u, "
4740 "buffering window %u - %u",
4741 recv_msg_seq, ssl->handshake->in_msg_seq,
4742 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4743
4744 goto exit;
4745 }
4746
4747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4748 recv_msg_seq, recv_msg_seq_offset ) );
4749
4750 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4751
4752 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004753 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004754 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004755 size_t reassembly_buf_sz;
4756
Hanno Becker37f95322018-08-16 13:55:32 +01004757 hs_buf->is_fragmented =
4758 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4759
4760 /* We copy the message back into the input buffer
4761 * after reassembly, so check that it's not too large.
4762 * This is an implementation-specific limitation
4763 * and not one from the standard, hence it is not
4764 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004765 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004766 {
4767 /* Ignore message */
4768 goto exit;
4769 }
4770
Hanno Beckere0b150f2018-08-21 15:51:03 +01004771 /* Check if we have enough space to buffer the message. */
4772 if( hs->buffering.total_bytes_buffered >
4773 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4774 {
4775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4776 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4777 }
4778
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004779 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4780 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004781
4782 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4783 hs->buffering.total_bytes_buffered ) )
4784 {
4785 if( recv_msg_seq_offset > 0 )
4786 {
4787 /* If we can't buffer a future message because
4788 * of space limitations -- ignore. */
4789 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",
4790 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4791 (unsigned) hs->buffering.total_bytes_buffered ) );
4792 goto exit;
4793 }
Hanno Beckere1801392018-08-21 16:51:05 +01004794 else
4795 {
4796 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",
4797 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4798 (unsigned) hs->buffering.total_bytes_buffered ) );
4799 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004800
Hanno Beckera02b0b42018-08-21 17:20:27 +01004801 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004802 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004803 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",
4804 (unsigned) msg_len,
4805 (unsigned) reassembly_buf_sz,
4806 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004807 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004808 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4809 goto exit;
4810 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004811 }
4812
4813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4814 msg_len ) );
4815
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004816 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4817 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004818 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004819 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004820 goto exit;
4821 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004822 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004823
4824 /* Prepare final header: copy msg_type, length and message_seq,
4825 * then add standardised fragment_offset and fragment_length */
4826 memcpy( hs_buf->data, ssl->in_msg, 6 );
4827 memset( hs_buf->data + 6, 0, 3 );
4828 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4829
4830 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004831
4832 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004833 }
4834 else
4835 {
4836 /* Make sure msg_type and length are consistent */
4837 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4838 {
4839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4840 /* Ignore */
4841 goto exit;
4842 }
4843 }
4844
Hanno Becker4422bbb2018-08-20 09:40:19 +01004845 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004846 {
4847 size_t frag_len, frag_off;
4848 unsigned char * const msg = hs_buf->data + 12;
4849
4850 /*
4851 * Check and copy current fragment
4852 */
4853
4854 /* Validation of header fields already done in
4855 * mbedtls_ssl_prepare_handshake_record(). */
4856 frag_off = ssl_get_hs_frag_off( ssl );
4857 frag_len = ssl_get_hs_frag_len( ssl );
4858
4859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4860 frag_off, frag_len ) );
4861 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4862
4863 if( hs_buf->is_fragmented )
4864 {
4865 unsigned char * const bitmask = msg + msg_len;
4866 ssl_bitmask_set( bitmask, frag_off, frag_len );
4867 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4868 msg_len ) == 0 );
4869 }
4870 else
4871 {
4872 hs_buf->is_complete = 1;
4873 }
4874
4875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4876 hs_buf->is_complete ? "" : "not yet " ) );
4877 }
4878
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004879 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004880 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004881
4882 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004883 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004884 break;
4885 }
4886
4887exit:
4888
4889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4890 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004891}
4892#endif /* MBEDTLS_SSL_PROTO_DTLS */
4893
Hanno Becker1097b342018-08-15 14:09:41 +01004894static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004895{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004896 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004897 * Consume last content-layer message and potentially
4898 * update in_msglen which keeps track of the contents'
4899 * consumption state.
4900 *
4901 * (1) Handshake messages:
4902 * Remove last handshake message, move content
4903 * and adapt in_msglen.
4904 *
4905 * (2) Alert messages:
4906 * Consume whole record content, in_msglen = 0.
4907 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004908 * (3) Change cipher spec:
4909 * Consume whole record content, in_msglen = 0.
4910 *
4911 * (4) Application data:
4912 * Don't do anything - the record layer provides
4913 * the application data as a stream transport
4914 * and consumes through mbedtls_ssl_read only.
4915 *
4916 */
4917
4918 /* Case (1): Handshake messages */
4919 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004920 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004921 /* Hard assertion to be sure that no application data
4922 * is in flight, as corrupting ssl->in_msglen during
4923 * ssl->in_offt != NULL is fatal. */
4924 if( ssl->in_offt != NULL )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4928 }
4929
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004930 /*
4931 * Get next Handshake message in the current record
4932 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004933
Hanno Becker4a810fb2017-05-24 16:27:30 +01004934 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004935 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004936 * current handshake content: If DTLS handshake
4937 * fragmentation is used, that's the fragment
4938 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004939 * size here is faulty and should be changed at
4940 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004941 * (2) While it doesn't seem to cause problems, one
4942 * has to be very careful not to assume that in_hslen
4943 * is always <= in_msglen in a sensible communication.
4944 * Again, it's wrong for DTLS handshake fragmentation.
4945 * The following check is therefore mandatory, and
4946 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004947 * Additionally, ssl->in_hslen might be arbitrarily out of
4948 * bounds after handling a DTLS message with an unexpected
4949 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004950 */
4951 if( ssl->in_hslen < ssl->in_msglen )
4952 {
4953 ssl->in_msglen -= ssl->in_hslen;
4954 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4955 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004956
Hanno Becker4a810fb2017-05-24 16:27:30 +01004957 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4958 ssl->in_msg, ssl->in_msglen );
4959 }
4960 else
4961 {
4962 ssl->in_msglen = 0;
4963 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004964
Hanno Becker4a810fb2017-05-24 16:27:30 +01004965 ssl->in_hslen = 0;
4966 }
4967 /* Case (4): Application data */
4968 else if( ssl->in_offt != NULL )
4969 {
4970 return( 0 );
4971 }
4972 /* Everything else (CCS & Alerts) */
4973 else
4974 {
4975 ssl->in_msglen = 0;
4976 }
4977
Hanno Becker1097b342018-08-15 14:09:41 +01004978 return( 0 );
4979}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004980
Hanno Beckere74d5562018-08-15 14:26:08 +01004981static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4982{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004983 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004984 return( 1 );
4985
4986 return( 0 );
4987}
4988
Hanno Becker5f066e72018-08-16 14:56:31 +01004989#if defined(MBEDTLS_SSL_PROTO_DTLS)
4990
4991static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4992{
4993 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4994 if( hs == NULL )
4995 return;
4996
Hanno Becker01315ea2018-08-21 17:22:17 +01004997 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004998 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004999 hs->buffering.total_bytes_buffered -=
5000 hs->buffering.future_record.len;
5001
5002 mbedtls_free( hs->buffering.future_record.data );
5003 hs->buffering.future_record.data = NULL;
5004 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005005}
5006
5007static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5008{
5009 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5010 unsigned char * rec;
5011 size_t rec_len;
5012 unsigned rec_epoch;
5013
5014 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5015 return( 0 );
5016
5017 if( hs == NULL )
5018 return( 0 );
5019
Hanno Becker5f066e72018-08-16 14:56:31 +01005020 rec = hs->buffering.future_record.data;
5021 rec_len = hs->buffering.future_record.len;
5022 rec_epoch = hs->buffering.future_record.epoch;
5023
5024 if( rec == NULL )
5025 return( 0 );
5026
Hanno Becker4cb782d2018-08-20 11:19:05 +01005027 /* Only consider loading future records if the
5028 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005029 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005030 return( 0 );
5031
Hanno Becker5f066e72018-08-16 14:56:31 +01005032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5033
5034 if( rec_epoch != ssl->in_epoch )
5035 {
5036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5037 goto exit;
5038 }
5039
5040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5041
5042 /* Double-check that the record is not too large */
5043 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5044 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5045 {
5046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5048 }
5049
5050 memcpy( ssl->in_hdr, rec, rec_len );
5051 ssl->in_left = rec_len;
5052 ssl->next_record_offset = 0;
5053
5054 ssl_free_buffered_record( ssl );
5055
5056exit:
5057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5058 return( 0 );
5059}
5060
5061static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5062{
5063 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5064 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005065 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005066
5067 /* Don't buffer future records outside handshakes. */
5068 if( hs == NULL )
5069 return( 0 );
5070
5071 /* Only buffer handshake records (we are only interested
5072 * in Finished messages). */
5073 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5074 return( 0 );
5075
5076 /* Don't buffer more than one future epoch record. */
5077 if( hs->buffering.future_record.data != NULL )
5078 return( 0 );
5079
Hanno Becker01315ea2018-08-21 17:22:17 +01005080 /* Don't buffer record if there's not enough buffering space remaining. */
5081 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5082 hs->buffering.total_bytes_buffered ) )
5083 {
5084 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",
5085 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5086 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005087 return( 0 );
5088 }
5089
Hanno Becker5f066e72018-08-16 14:56:31 +01005090 /* Buffer record */
5091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5092 ssl->in_epoch + 1 ) );
5093 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5094 rec_hdr_len + ssl->in_msglen );
5095
5096 /* ssl_parse_record_header() only considers records
5097 * of the next epoch as candidates for buffering. */
5098 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005099 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005100
5101 hs->buffering.future_record.data =
5102 mbedtls_calloc( 1, hs->buffering.future_record.len );
5103 if( hs->buffering.future_record.data == NULL )
5104 {
5105 /* If we run out of RAM trying to buffer a
5106 * record from the next epoch, just ignore. */
5107 return( 0 );
5108 }
5109
Hanno Becker01315ea2018-08-21 17:22:17 +01005110 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005111
Hanno Becker01315ea2018-08-21 17:22:17 +01005112 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005113 return( 0 );
5114}
5115
5116#endif /* MBEDTLS_SSL_PROTO_DTLS */
5117
Hanno Beckere74d5562018-08-15 14:26:08 +01005118static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005119{
5120 int ret;
5121
Hanno Becker5f066e72018-08-16 14:56:31 +01005122#if defined(MBEDTLS_SSL_PROTO_DTLS)
5123 /* We might have buffered a future record; if so,
5124 * and if the epoch matches now, load it.
5125 * On success, this call will set ssl->in_left to
5126 * the length of the buffered record, so that
5127 * the calls to ssl_fetch_input() below will
5128 * essentially be no-ops. */
5129 ret = ssl_load_buffered_record( ssl );
5130 if( ret != 0 )
5131 return( ret );
5132#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005137 return( ret );
5138 }
5139
5140 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5144 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005145 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005146 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5147 {
5148 ret = ssl_buffer_future_record( ssl );
5149 if( ret != 0 )
5150 return( ret );
5151
5152 /* Fall through to handling of unexpected records */
5153 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5154 }
5155
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005156 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5157 {
5158 /* Skip unexpected record (but not whole datagram) */
5159 ssl->next_record_offset = ssl->in_msglen
5160 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005161
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5163 "(header)" ) );
5164 }
5165 else
5166 {
5167 /* Skip invalid record and the rest of the datagram */
5168 ssl->next_record_offset = 0;
5169 ssl->in_left = 0;
5170
5171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5172 "(header)" ) );
5173 }
5174
5175 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005176 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005177 }
5178#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005179 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005180 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005181
5182 /*
5183 * Read and optionally decrypt the message contents
5184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5186 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005189 return( ret );
5190 }
5191
5192 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005194 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005196 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005197 if( ssl->next_record_offset < ssl->in_left )
5198 {
5199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5200 }
5201 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005202 else
5203#endif
5204 ssl->in_left = 0;
5205
5206 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005210 {
5211 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5213 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005214 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005215 /* Except when waiting for Finished as a bad mac here
5216 * probably means something went wrong in the handshake
5217 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5218 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5219 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5220 {
5221#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5222 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5223 {
5224 mbedtls_ssl_send_alert_message( ssl,
5225 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5226 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5227 }
5228#endif
5229 return( ret );
5230 }
5231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005233 if( ssl->conf->badmac_limit != 0 &&
5234 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5237 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005238 }
5239#endif
5240
Hanno Becker4a810fb2017-05-24 16:27:30 +01005241 /* As above, invalid records cause
5242 * dismissal of the whole datagram. */
5243
5244 ssl->next_record_offset = 0;
5245 ssl->in_left = 0;
5246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005248 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005249 }
5250
5251 return( ret );
5252 }
5253 else
5254#endif
5255 {
5256 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5258 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 mbedtls_ssl_send_alert_message( ssl,
5261 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5262 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005263 }
5264#endif
5265 return( ret );
5266 }
5267 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005268
Simon Butcher99000142016-10-13 17:21:01 +01005269 return( 0 );
5270}
5271
5272int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5273{
5274 int ret;
5275
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005276 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005277 * Handle particular types of records
5278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005280 {
Simon Butcher99000142016-10-13 17:21:01 +01005281 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5282 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005283 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005285 }
5286
Hanno Beckere678eaa2018-08-21 14:57:46 +01005287 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005288 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005289 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005290 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5292 ssl->in_msglen ) );
5293 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005294 }
5295
Hanno Beckere678eaa2018-08-21 14:57:46 +01005296 if( ssl->in_msg[0] != 1 )
5297 {
5298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5299 ssl->in_msg[0] ) );
5300 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5301 }
5302
5303#if defined(MBEDTLS_SSL_PROTO_DTLS)
5304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5305 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5306 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5307 {
5308 if( ssl->handshake == NULL )
5309 {
5310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5311 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5312 }
5313
5314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5315 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5316 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005317#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005318 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005322 if( ssl->in_msglen != 2 )
5323 {
5324 /* Note: Standard allows for more than one 2 byte alert
5325 to be packed in a single message, but Mbed TLS doesn't
5326 currently support this. */
5327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5328 ssl->in_msglen ) );
5329 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5330 }
5331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 ssl->in_msg[0], ssl->in_msg[1] ) );
5334
5335 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005336 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005341 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005343 }
5344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5346 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5349 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005350 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005351
5352#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5353 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5354 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5355 {
Hanno Becker90333da2017-10-10 11:27:13 +01005356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005357 /* Will be handled when trying to parse ServerHello */
5358 return( 0 );
5359 }
5360#endif
5361
5362#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5363 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5364 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5365 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5366 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5367 {
5368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5369 /* Will be handled in mbedtls_ssl_parse_certificate() */
5370 return( 0 );
5371 }
5372#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5373
5374 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005375 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005376 }
5377
Hanno Beckerc76c6192017-06-06 10:03:17 +01005378#if defined(MBEDTLS_SSL_PROTO_DTLS)
5379 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5380 ssl->handshake != NULL &&
5381 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5382 {
5383 ssl_handshake_wrapup_free_hs_transform( ssl );
5384 }
5385#endif
5386
Paul Bakker5121ce52009-01-03 21:22:43 +00005387 return( 0 );
5388}
5389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005391{
5392 int ret;
5393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5395 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5396 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005397 {
5398 return( ret );
5399 }
5400
5401 return( 0 );
5402}
5403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005405 unsigned char level,
5406 unsigned char message )
5407{
5408 int ret;
5409
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005410 if( ssl == NULL || ssl->conf == NULL )
5411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005417 ssl->out_msglen = 2;
5418 ssl->out_msg[0] = level;
5419 ssl->out_msg[1] = message;
5420
Hanno Becker67bc7c32018-08-06 11:33:50 +01005421 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005424 return( ret );
5425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005427
5428 return( 0 );
5429}
5430
Paul Bakker5121ce52009-01-03 21:22:43 +00005431/*
5432 * Handshake functions
5433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5435 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5436 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5437 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5438 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5439 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5440 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005441/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5449 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005450 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5451 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005454 ssl->state++;
5455 return( 0 );
5456 }
5457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005460}
5461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5469 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005470 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5471 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005474 ssl->state++;
5475 return( 0 );
5476 }
5477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5479 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005480}
Gilles Peskinef9828522017-05-03 12:28:43 +02005481
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005482#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005483/* Some certificate support -> implement write and parse */
5484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005488 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 const mbedtls_x509_crt *crt;
5490 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5495 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005496 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5497 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005500 ssl->state++;
5501 return( 0 );
5502 }
5503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005505 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
5507 if( ssl->client_auth == 0 )
5508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 ssl->state++;
5511 return( 0 );
5512 }
5513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 /*
5516 * If using SSLv3 and got no cert, send an Alert message
5517 * (otherwise an empty Certificate message will be sent).
5518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5520 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005521 {
5522 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5524 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5525 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 goto write_msg;
5529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532#endif /* MBEDTLS_SSL_CLI_C */
5533#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005534 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005536 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5539 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005540 }
5541 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005542#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005544 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005545
5546 /*
5547 * 0 . 0 handshake type
5548 * 1 . 3 handshake length
5549 * 4 . 6 length of all certs
5550 * 7 . 9 length of cert. 1
5551 * 10 . n-1 peer certificate
5552 * n . n+2 length of cert. 2
5553 * n+3 . ... upper level cert, etc.
5554 */
5555 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005557
Paul Bakker29087132010-03-21 21:03:34 +00005558 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005559 {
5560 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005561 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005564 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005566 }
5567
5568 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5569 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5570 ssl->out_msg[i + 2] = (unsigned char)( n );
5571
5572 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5573 i += n; crt = crt->next;
5574 }
5575
5576 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5577 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5578 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5579
5580 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5582 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005583
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005584#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005585write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005586#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005587
5588 ssl->state++;
5589
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005590 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005591 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005593 return( ret );
5594 }
5595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005597
Paul Bakkered27a042013-04-18 22:46:23 +02005598 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005599}
5600
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005601/*
5602 * Once the certificate message is read, parse it into a cert chain and
5603 * perform basic checks, but leave actual verification to the caller
5604 */
5605static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005606{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005607 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005608 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005609 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611#if defined(MBEDTLS_SSL_SRV_C)
5612#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 /*
5614 * Check if the client sent an empty certificate
5615 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005616 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005618 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005619 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5621 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5622 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005625
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005626 /* The client was asked for a certificate but didn't send
5627 one. The client should know what's going on, so we
5628 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005629 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005630 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005631 }
5632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5636 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005637 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5641 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5642 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5643 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005646
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005647 /* The client was asked for a certificate but didn't send
5648 one. The client should know what's going on, so we
5649 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005650 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005651 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005652 }
5653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5655 MBEDTLS_SSL_PROTO_TLS1_2 */
5656#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005661 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5662 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005664 }
5665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5667 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005670 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5671 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005673 }
5674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005676
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005680 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005681
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005682 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005686 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5687 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 }
5690
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005691 /* In case we tried to reuse a session but it failed */
5692 if( ssl->session_negotiate->peer_cert != NULL )
5693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5695 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005696 }
5697
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005698 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005700 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005703 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5704 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005705 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005706 }
5707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005709
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005710 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005711
5712 while( i < ssl->in_hslen )
5713 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005714 if ( i + 3 > ssl->in_hslen ) {
5715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5716 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5717 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5718 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5719 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 if( ssl->in_msg[i] != 0 )
5721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005723 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5724 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005726 }
5727
5728 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5729 | (unsigned int) ssl->in_msg[i + 2];
5730 i += 3;
5731
5732 if( n < 128 || i + n > ssl->in_hslen )
5733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005735 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5736 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 }
5739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005741 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005742 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005743 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005744 case 0: /*ok*/
5745 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5746 /* Ignore certificate with an unknown algorithm: maybe a
5747 prior certificate was already trusted. */
5748 break;
5749
5750 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5751 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5752 goto crt_parse_der_failed;
5753
5754 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5755 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5756 goto crt_parse_der_failed;
5757
5758 default:
5759 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5760 crt_parse_der_failed:
5761 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005762 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005763 return( ret );
5764 }
5765
5766 i += n;
5767 }
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005771 /*
5772 * On client, make sure the server cert doesn't change during renego to
5773 * avoid "triple handshake" attack: https://secure-resumption.com/
5774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005776 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005778 {
5779 if( ssl->session->peer_cert == NULL )
5780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005782 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5783 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005785 }
5786
5787 if( ssl->session->peer_cert->raw.len !=
5788 ssl->session_negotiate->peer_cert->raw.len ||
5789 memcmp( ssl->session->peer_cert->raw.p,
5790 ssl->session_negotiate->peer_cert->raw.p,
5791 ssl->session->peer_cert->raw.len ) != 0 )
5792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005794 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5795 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005797 }
5798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005800
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005801 return( 0 );
5802}
5803
5804int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5805{
5806 int ret;
5807 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5808 ssl->transform_negotiate->ciphersuite_info;
5809#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5810 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5811 ? ssl->handshake->sni_authmode
5812 : ssl->conf->authmode;
5813#else
5814 const int authmode = ssl->conf->authmode;
5815#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005816 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005817
5818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5819
5820 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5821 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5822 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5823 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5824 {
5825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5826 ssl->state++;
5827 return( 0 );
5828 }
5829
5830#if defined(MBEDTLS_SSL_SRV_C)
5831 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5832 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5833 {
5834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5835 ssl->state++;
5836 return( 0 );
5837 }
5838
5839 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5840 authmode == MBEDTLS_SSL_VERIFY_NONE )
5841 {
5842 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005844
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005845 ssl->state++;
5846 return( 0 );
5847 }
5848#endif
5849
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005850#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5851 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005852 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005853 {
5854 goto crt_verify;
5855 }
5856#endif
5857
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005858 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005859 {
5860 /* mbedtls_ssl_read_record may have sent an alert already. We
5861 let it decide whether to alert. */
5862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5863 return( ret );
5864 }
5865
5866 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5867 {
5868#if defined(MBEDTLS_SSL_SRV_C)
5869 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5870 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5871 {
5872 ret = 0;
5873 }
5874#endif
5875
5876 ssl->state++;
5877 return( ret );
5878 }
5879
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005880#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5881 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005882 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005883
5884crt_verify:
5885 if( ssl->handshake->ecrs_enabled)
5886 rs_ctx = &ssl->handshake->ecrs_ctx;
5887#endif
5888
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005889 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005891 mbedtls_x509_crt *ca_chain;
5892 mbedtls_x509_crl *ca_crl;
5893
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005894#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005895 if( ssl->handshake->sni_ca_chain != NULL )
5896 {
5897 ca_chain = ssl->handshake->sni_ca_chain;
5898 ca_crl = ssl->handshake->sni_ca_crl;
5899 }
5900 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005901#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005902 {
5903 ca_chain = ssl->conf->ca_chain;
5904 ca_crl = ssl->conf->ca_crl;
5905 }
5906
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005907 /*
5908 * Main check: verify certificate
5909 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005910 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005911 ssl->session_negotiate->peer_cert,
5912 ca_chain, ca_crl,
5913 ssl->conf->cert_profile,
5914 ssl->hostname,
5915 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005916 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005917
5918 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005921 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005922
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005923#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5924 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005925 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005926#endif
5927
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005928 /*
5929 * Secondary checks: always done, but change 'ret' only if it was 0
5930 */
5931
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005932#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005935
5936 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005938 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005939 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005940 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005943 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005945 }
5946 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005947#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005950 ciphersuite_info,
5951 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005952 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005955 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005957 }
5958
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005959 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5960 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5961 * with details encoded in the verification flags. All other kinds
5962 * of error codes, including those from the user provided f_vrfy
5963 * functions, are treated as fatal and lead to a failure of
5964 * ssl_parse_certificate even if verification was optional. */
5965 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5966 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5967 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5968 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005969 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005970 }
5971
5972 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5973 {
5974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5975 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5976 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005977
5978 if( ret != 0 )
5979 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005980 uint8_t alert;
5981
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005982 /* The certificate may have been rejected for several reasons.
5983 Pick one and send the corresponding alert. Which alert to send
5984 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005985 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5986 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5987 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5988 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5989 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5990 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5991 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5992 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5993 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5994 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5995 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5996 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5997 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5998 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5999 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6000 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6001 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6002 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6003 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6004 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006005 else
6006 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006007 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6008 alert );
6009 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006010
Hanno Beckere6706e62017-05-15 16:05:15 +01006011#if defined(MBEDTLS_DEBUG_C)
6012 if( ssl->session_negotiate->verify_result != 0 )
6013 {
6014 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6015 ssl->session_negotiate->verify_result ) );
6016 }
6017 else
6018 {
6019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6020 }
6021#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006022 }
6023
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006024 ssl->state++;
6025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
6028 return( ret );
6029}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6031 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6032 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6033 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6034 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6035 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6036 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006039{
6040 int ret;
6041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006045 ssl->out_msglen = 1;
6046 ssl->out_msg[0] = 1;
6047
Paul Bakker5121ce52009-01-03 21:22:43 +00006048 ssl->state++;
6049
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006050 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006051 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 return( ret );
6054 }
6055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006057
6058 return( 0 );
6059}
6060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006062{
6063 int ret;
6064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Hanno Becker327c93b2018-08-15 13:56:18 +01006067 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 return( ret );
6071 }
6072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006076 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6077 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 }
6080
Hanno Beckere678eaa2018-08-21 14:57:46 +01006081 /* CCS records are only accepted if they have length 1 and content '1',
6082 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006084 /*
6085 * Switch to our negotiated transform and session parameters for inbound
6086 * data.
6087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006089 ssl->transform_in = ssl->transform_negotiate;
6090 ssl->session_in = ssl->session_negotiate;
6091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006096 ssl_dtls_replay_reset( ssl );
6097#endif
6098
6099 /* Increment epoch */
6100 if( ++ssl->in_epoch == 0 )
6101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006103 /* This is highly unlikely to happen for legitimate reasons, so
6104 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006106 }
6107 }
6108 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006110 memset( ssl->in_ctr, 0, 8 );
6111
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006112 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6115 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6121 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006123 }
6124 }
6125#endif
6126
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 ssl->state++;
6128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006130
6131 return( 0 );
6132}
6133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6135 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006136{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006137 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6140 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6141 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006142 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006143 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6146#if defined(MBEDTLS_SHA512_C)
6147 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006148 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6149 else
6150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006151#if defined(MBEDTLS_SHA256_C)
6152 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006153 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006154 else
6155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006159 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006160 }
Paul Bakker380da532012-04-18 16:10:25 +00006161}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6166 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006167 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6168 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6171#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006172 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006175 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006178}
6179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006181 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006182{
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)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006185 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6186 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6189#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006190 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006191#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006193 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006196}
6197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6199 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6200static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006201 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006202{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006203 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6204 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006205}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006206#endif
Paul Bakker380da532012-04-18 16:10:25 +00006207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6209#if defined(MBEDTLS_SHA256_C)
6210static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006211 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006212{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006213 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006214}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006215#endif
Paul Bakker380da532012-04-18 16:10:25 +00006216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217#if defined(MBEDTLS_SHA512_C)
6218static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006219 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006220{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006221 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006222}
Paul Bakker769075d2012-11-24 11:26:46 +01006223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006227static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006229{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006230 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006231 mbedtls_md5_context md5;
6232 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006233
Paul Bakker5121ce52009-01-03 21:22:43 +00006234 unsigned char padbuf[48];
6235 unsigned char md5sum[16];
6236 unsigned char sha1sum[20];
6237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006239 if( !session )
6240 session = ssl->session;
6241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006243
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006244 mbedtls_md5_init( &md5 );
6245 mbedtls_sha1_init( &sha1 );
6246
6247 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6248 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249
6250 /*
6251 * SSLv3:
6252 * hash =
6253 * MD5( master + pad2 +
6254 * MD5( handshake + sender + master + pad1 ) )
6255 * + SHA1( master + pad2 +
6256 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006257 */
6258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006260 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6261 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006265 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6266 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006270 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006271
Paul Bakker1ef83d62012-04-11 12:09:53 +00006272 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006273
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006274 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6275 mbedtls_md5_update_ret( &md5, session->master, 48 );
6276 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6277 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006278
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006279 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6280 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6281 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6282 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006283
Paul Bakker1ef83d62012-04-11 12:09:53 +00006284 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006285
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006286 mbedtls_md5_starts_ret( &md5 );
6287 mbedtls_md5_update_ret( &md5, session->master, 48 );
6288 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6289 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6290 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006291
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006292 mbedtls_sha1_starts_ret( &sha1 );
6293 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6294 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6295 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6296 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006299
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006300 mbedtls_md5_free( &md5 );
6301 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006302
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006303 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6304 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6305 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006308}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006312static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006314{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006315 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006316 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006317 mbedtls_md5_context md5;
6318 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006319 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006322 if( !session )
6323 session = ssl->session;
6324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006326
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006327 mbedtls_md5_init( &md5 );
6328 mbedtls_sha1_init( &sha1 );
6329
6330 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6331 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006332
Paul Bakker1ef83d62012-04-11 12:09:53 +00006333 /*
6334 * TLSv1:
6335 * hash = PRF( master, finished_label,
6336 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6337 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006340 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6341 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006342#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006345 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6346 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006347#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006350 ? "client finished"
6351 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006353 mbedtls_md5_finish_ret( &md5, padbuf );
6354 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006355
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006356 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006357 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006360
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006361 mbedtls_md5_free( &md5 );
6362 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006363
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006364 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6371#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006372static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006374{
6375 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006376 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006377 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006378 unsigned char padbuf[32];
6379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006381 if( !session )
6382 session = ssl->session;
6383
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006384 mbedtls_sha256_init( &sha256 );
6385
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006387
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006388 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006389
6390 /*
6391 * TLSv1.2:
6392 * hash = PRF( master, finished_label,
6393 * Hash( handshake ) )[0.11]
6394 */
6395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396#if !defined(MBEDTLS_SHA256_ALT)
6397 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006398 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006399#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006402 ? "client finished"
6403 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006405 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006406
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006407 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006408 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006411
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006412 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006413
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006414 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006421static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006423{
6424 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006425 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006426 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006427 unsigned char padbuf[48];
6428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006430 if( !session )
6431 session = ssl->session;
6432
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006433 mbedtls_sha512_init( &sha512 );
6434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006436
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006437 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006438
6439 /*
6440 * TLSv1.2:
6441 * hash = PRF( master, finished_label,
6442 * Hash( handshake ) )[0.11]
6443 */
6444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006446 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6447 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006448#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006451 ? "client finished"
6452 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006454 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006455
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006456 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006457 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006461 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006462
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006463 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467#endif /* MBEDTLS_SHA512_C */
6468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006473
6474 /*
6475 * Free our handshake params
6476 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006477 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006479 ssl->handshake = NULL;
6480
6481 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006482 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006483 */
6484 if( ssl->transform )
6485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 mbedtls_ssl_transform_free( ssl->transform );
6487 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006488 }
6489 ssl->transform = ssl->transform_negotiate;
6490 ssl->transform_negotiate = NULL;
6491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006493}
6494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006496{
6497 int resume = ssl->handshake->resume;
6498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501#if defined(MBEDTLS_SSL_RENEGOTIATION)
6502 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006505 ssl->renego_records_seen = 0;
6506 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006507#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006508
6509 /*
6510 * Free the previous session and switch in the current one
6511 */
Paul Bakker0a597072012-09-25 21:55:46 +00006512 if( ssl->session )
6513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006515 /* RFC 7366 3.1: keep the EtM state */
6516 ssl->session_negotiate->encrypt_then_mac =
6517 ssl->session->encrypt_then_mac;
6518#endif
6519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 mbedtls_ssl_session_free( ssl->session );
6521 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006522 }
6523 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006524 ssl->session_negotiate = NULL;
6525
Paul Bakker0a597072012-09-25 21:55:46 +00006526 /*
6527 * Add cache entry
6528 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006529 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006530 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006531 resume == 0 )
6532 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006533 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006535 }
Paul Bakker0a597072012-09-25 21:55:46 +00006536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006539 ssl->handshake->flight != NULL )
6540 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006541 /* Cancel handshake timer */
6542 ssl_set_timer( ssl, 0 );
6543
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006544 /* Keep last flight around in case we need to resend it:
6545 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006547 }
6548 else
6549#endif
6550 ssl_handshake_wrapup_free_hs_transform( ssl );
6551
Paul Bakker48916f92012-09-16 19:57:18 +00006552 ssl->state++;
6553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006555}
6556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006558{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006559 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006562
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006563 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006564
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006565 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006566
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006567 /*
6568 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6569 * may define some other value. Currently (early 2016), no defined
6570 * ciphersuite does this (and this is unlikely to change as activity has
6571 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006576 ssl->verify_data_len = hash_len;
6577 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006578#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006579
Paul Bakker5121ce52009-01-03 21:22:43 +00006580 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6582 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006583
6584 /*
6585 * In case of session resuming, invert the client and server
6586 * ChangeCipherSpec messages order.
6587 */
Paul Bakker0a597072012-09-25 21:55:46 +00006588 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 }
6599 else
6600 ssl->state++;
6601
Paul Bakker48916f92012-09-16 19:57:18 +00006602 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006603 * Switch to our negotiated transform and session parameters for outbound
6604 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006609 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006610 {
6611 unsigned char i;
6612
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006613 /* Remember current epoch settings for resending */
6614 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006615 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006616
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006617 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006618 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006619
6620 /* Increment epoch */
6621 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006622 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006623 break;
6624
6625 /* The loop goes to its end iff the counter is wrapping */
6626 if( i == 0 )
6627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6629 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006630 }
6631 }
6632 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006634 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006636 ssl->transform_out = ssl->transform_negotiate;
6637 ssl->session_out = ssl->session_negotiate;
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6640 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6645 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006646 }
6647 }
6648#endif
6649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006651 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006653#endif
6654
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006655 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006658 return( ret );
6659 }
6660
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006661#if defined(MBEDTLS_SSL_PROTO_DTLS)
6662 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6663 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6664 {
6665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6666 return( ret );
6667 }
6668#endif
6669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006671
6672 return( 0 );
6673}
6674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006676#define SSL_MAX_HASH_LEN 36
6677#else
6678#define SSL_MAX_HASH_LEN 12
6679#endif
6680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006682{
Paul Bakker23986e52011-04-24 08:57:21 +00006683 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006684 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006685 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006688
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006689 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Hanno Becker327c93b2018-08-15 13:56:18 +01006691 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006694 return( ret );
6695 }
6696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006700 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6701 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006703 }
6704
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006705 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706#if defined(MBEDTLS_SSL_PROTO_SSL3)
6707 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006708 hash_len = 36;
6709 else
6710#endif
6711 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6714 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006717 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6718 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 }
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006723 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006726 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6727 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006729 }
6730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006732 ssl->verify_data_len = hash_len;
6733 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006734#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006735
Paul Bakker0a597072012-09-25 21:55:46 +00006736 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006743 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 }
6747 else
6748 ssl->state++;
6749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006751 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006753#endif
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006756
6757 return( 0 );
6758}
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6765 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6766 mbedtls_md5_init( &handshake->fin_md5 );
6767 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006768 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6769 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6772#if defined(MBEDTLS_SHA256_C)
6773 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006774 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SHA512_C)
6777 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006778 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006781
6782 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006783
6784#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6785 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6786 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6787#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#if defined(MBEDTLS_DHM_C)
6790 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792#if defined(MBEDTLS_ECDH_C)
6793 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006794#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006795#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006796 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006797#if defined(MBEDTLS_SSL_CLI_C)
6798 handshake->ecjpake_cache = NULL;
6799 handshake->ecjpake_cache_len = 0;
6800#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006801#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006802
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006803#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006804 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006805#endif
6806
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006807#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6808 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6809#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006810}
6811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006813{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6817 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 mbedtls_md_init( &transform->md_ctx_enc );
6820 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006821}
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006824{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006826}
6827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006829{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006830 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006831 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006833 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006835 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006836 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006837
6838 /*
6839 * Either the pointers are now NULL or cleared properly and can be freed.
6840 * Now allocate missing structures.
6841 */
6842 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006843 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006844 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006845 }
Paul Bakker48916f92012-09-16 19:57:18 +00006846
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006847 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006848 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006849 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006850 }
Paul Bakker48916f92012-09-16 19:57:18 +00006851
Paul Bakker82788fb2014-10-20 13:59:19 +02006852 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006853 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006854 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006855 }
Paul Bakker48916f92012-09-16 19:57:18 +00006856
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006857 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006858 if( ssl->handshake == NULL ||
6859 ssl->transform_negotiate == NULL ||
6860 ssl->session_negotiate == NULL )
6861 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 mbedtls_free( ssl->handshake );
6865 mbedtls_free( ssl->transform_negotiate );
6866 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006867
6868 ssl->handshake = NULL;
6869 ssl->transform_negotiate = NULL;
6870 ssl->session_negotiate = NULL;
6871
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006872 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006873 }
6874
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006875 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006877 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006878 ssl_handshake_params_init( ssl->handshake );
6879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6882 {
6883 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006884
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006885 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6886 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6887 else
6888 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006889
6890 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006891 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006892#endif
6893
Paul Bakker48916f92012-09-16 19:57:18 +00006894 return( 0 );
6895}
6896
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006897#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006898/* Dummy cookie callbacks for defaults */
6899static int ssl_cookie_write_dummy( void *ctx,
6900 unsigned char **p, unsigned char *end,
6901 const unsigned char *cli_id, size_t cli_id_len )
6902{
6903 ((void) ctx);
6904 ((void) p);
6905 ((void) end);
6906 ((void) cli_id);
6907 ((void) cli_id_len);
6908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006910}
6911
6912static int ssl_cookie_check_dummy( void *ctx,
6913 const unsigned char *cookie, size_t cookie_len,
6914 const unsigned char *cli_id, size_t cli_id_len )
6915{
6916 ((void) ctx);
6917 ((void) cookie);
6918 ((void) cookie_len);
6919 ((void) cli_id);
6920 ((void) cli_id_len);
6921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006923}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006924#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006925
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006926/* Once ssl->out_hdr as the address of the beginning of the
6927 * next outgoing record is set, deduce the other pointers.
6928 *
6929 * Note: For TLS, we save the implicit record sequence number
6930 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6931 * and the caller has to make sure there's space for this.
6932 */
6933
6934static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6935 mbedtls_ssl_transform *transform )
6936{
6937#if defined(MBEDTLS_SSL_PROTO_DTLS)
6938 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6939 {
6940 ssl->out_ctr = ssl->out_hdr + 3;
6941 ssl->out_len = ssl->out_hdr + 11;
6942 ssl->out_iv = ssl->out_hdr + 13;
6943 }
6944 else
6945#endif
6946 {
6947 ssl->out_ctr = ssl->out_hdr - 8;
6948 ssl->out_len = ssl->out_hdr + 3;
6949 ssl->out_iv = ssl->out_hdr + 5;
6950 }
6951
6952 /* Adjust out_msg to make space for explicit IV, if used. */
6953 if( transform != NULL &&
6954 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6955 {
6956 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6957 }
6958 else
6959 ssl->out_msg = ssl->out_iv;
6960}
6961
6962/* Once ssl->in_hdr as the address of the beginning of the
6963 * next incoming record is set, deduce the other pointers.
6964 *
6965 * Note: For TLS, we save the implicit record sequence number
6966 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6967 * and the caller has to make sure there's space for this.
6968 */
6969
6970static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6971 mbedtls_ssl_transform *transform )
6972{
6973#if defined(MBEDTLS_SSL_PROTO_DTLS)
6974 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6975 {
6976 ssl->in_ctr = ssl->in_hdr + 3;
6977 ssl->in_len = ssl->in_hdr + 11;
6978 ssl->in_iv = ssl->in_hdr + 13;
6979 }
6980 else
6981#endif
6982 {
6983 ssl->in_ctr = ssl->in_hdr - 8;
6984 ssl->in_len = ssl->in_hdr + 3;
6985 ssl->in_iv = ssl->in_hdr + 5;
6986 }
6987
6988 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6989 if( transform != NULL &&
6990 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6991 {
6992 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6993 }
6994 else
6995 ssl->in_msg = ssl->in_iv;
6996}
6997
Paul Bakker5121ce52009-01-03 21:22:43 +00006998/*
6999 * Initialize an SSL context
7000 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007001void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7002{
7003 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7004}
7005
7006/*
7007 * Setup an SSL context
7008 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007009
7010static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7011{
7012 /* Set the incoming and outgoing record pointers. */
7013#if defined(MBEDTLS_SSL_PROTO_DTLS)
7014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7015 {
7016 ssl->out_hdr = ssl->out_buf;
7017 ssl->in_hdr = ssl->in_buf;
7018 }
7019 else
7020#endif /* MBEDTLS_SSL_PROTO_DTLS */
7021 {
7022 ssl->out_hdr = ssl->out_buf + 8;
7023 ssl->in_hdr = ssl->in_buf + 8;
7024 }
7025
7026 /* Derive other internal pointers. */
7027 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7028 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7029}
7030
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007031int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007032 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007033{
Paul Bakker48916f92012-09-16 19:57:18 +00007034 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007035
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007036 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007037
7038 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007039 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007040 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007041
7042 /* Set to NULL in case of an error condition */
7043 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007044
Angus Grattond8213d02016-05-25 20:56:48 +10007045 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7046 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007047 {
Angus Grattond8213d02016-05-25 20:56:48 +10007048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007049 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007050 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007051 }
7052
7053 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7054 if( ssl->out_buf == NULL )
7055 {
7056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007057 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007058 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007059 }
7060
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007061 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007062
Paul Bakker48916f92012-09-16 19:57:18 +00007063 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007064 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007065
7066 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007067
7068error:
7069 mbedtls_free( ssl->in_buf );
7070 mbedtls_free( ssl->out_buf );
7071
7072 ssl->conf = NULL;
7073
7074 ssl->in_buf = NULL;
7075 ssl->out_buf = NULL;
7076
7077 ssl->in_hdr = NULL;
7078 ssl->in_ctr = NULL;
7079 ssl->in_len = NULL;
7080 ssl->in_iv = NULL;
7081 ssl->in_msg = NULL;
7082
7083 ssl->out_hdr = NULL;
7084 ssl->out_ctr = NULL;
7085 ssl->out_len = NULL;
7086 ssl->out_iv = NULL;
7087 ssl->out_msg = NULL;
7088
k-stachowiak9f7798e2018-07-31 16:52:32 +02007089 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007090}
7091
7092/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007093 * Reset an initialized and used SSL context for re-use while retaining
7094 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007095 *
7096 * If partial is non-zero, keep data in the input buffer and client ID.
7097 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007098 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007099static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007100{
Paul Bakker48916f92012-09-16 19:57:18 +00007101 int ret;
7102
Hanno Becker7e772132018-08-10 12:38:21 +01007103#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7104 !defined(MBEDTLS_SSL_SRV_C)
7105 ((void) partial);
7106#endif
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007110 /* Cancel any possibly running timer */
7111 ssl_set_timer( ssl, 0 );
7112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#if defined(MBEDTLS_SSL_RENEGOTIATION)
7114 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007115 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007116
7117 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7119 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007122
Paul Bakker7eb013f2011-10-06 12:37:39 +00007123 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007124 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007125
7126 ssl->in_msgtype = 0;
7127 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007129 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007130 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007133 ssl_dtls_replay_reset( ssl );
7134#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007135
7136 ssl->in_hslen = 0;
7137 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007138
7139 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007140
7141 ssl->out_msgtype = 0;
7142 ssl->out_msglen = 0;
7143 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7145 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007146 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007147#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007148
Hanno Becker19859472018-08-06 09:40:20 +01007149 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7150
Paul Bakker48916f92012-09-16 19:57:18 +00007151 ssl->transform_in = NULL;
7152 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007153
Hanno Becker78640902018-08-13 16:35:15 +01007154 ssl->session_in = NULL;
7155 ssl->session_out = NULL;
7156
Angus Grattond8213d02016-05-25 20:56:48 +10007157 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007158
7159#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007160 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007161#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7162 {
7163 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007164 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007165 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7168 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7171 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7174 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007175 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007176 }
7177#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007178
Paul Bakker48916f92012-09-16 19:57:18 +00007179 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 mbedtls_ssl_transform_free( ssl->transform );
7182 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007183 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007184 }
Paul Bakker48916f92012-09-16 19:57:18 +00007185
Paul Bakkerc0463502013-02-14 11:19:38 +01007186 if( ssl->session )
7187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 mbedtls_ssl_session_free( ssl->session );
7189 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007190 ssl->session = NULL;
7191 }
7192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007194 ssl->alpn_chosen = NULL;
7195#endif
7196
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007197#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007198#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007199 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007200#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007201 {
7202 mbedtls_free( ssl->cli_id );
7203 ssl->cli_id = NULL;
7204 ssl->cli_id_len = 0;
7205 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007206#endif
7207
Paul Bakker48916f92012-09-16 19:57:18 +00007208 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7209 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007210
7211 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007212}
7213
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007214/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007215 * Reset an initialized and used SSL context for re-use while retaining
7216 * all application-set variables, function pointers and data.
7217 */
7218int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7219{
7220 return( ssl_session_reset_int( ssl, 0 ) );
7221}
7222
7223/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007224 * SSL set accessors
7225 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007226void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007227{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007228 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007229}
7230
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007231void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007232{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007233 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007234}
7235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007237void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007239 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007240}
7241#endif
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007244void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007246 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007247}
7248#endif
7249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007251
Hanno Becker1841b0a2018-08-24 11:13:57 +01007252void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7253 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007254{
7255 ssl->disable_datagram_packing = !allow_packing;
7256}
7257
7258void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7259 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007261 conf->hs_timeout_min = min;
7262 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007263}
7264#endif
7265
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007266void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007268 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007269}
7270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007272void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007273 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007274 void *p_vrfy )
7275{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007276 conf->f_vrfy = f_vrfy;
7277 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007280
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007281void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007282 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007283 void *p_rng )
7284{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007285 conf->f_rng = f_rng;
7286 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007287}
7288
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007289void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007290 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007291 void *p_dbg )
7292{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007293 conf->f_dbg = f_dbg;
7294 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007295}
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007298 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007299 mbedtls_ssl_send_t *f_send,
7300 mbedtls_ssl_recv_t *f_recv,
7301 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007302{
7303 ssl->p_bio = p_bio;
7304 ssl->f_send = f_send;
7305 ssl->f_recv = f_recv;
7306 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007307}
7308
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007309#if defined(MBEDTLS_SSL_PROTO_DTLS)
7310void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7311{
7312 ssl->mtu = mtu;
7313}
7314#endif
7315
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007316void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007317{
7318 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007319}
7320
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007321void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7322 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007323 mbedtls_ssl_set_timer_t *f_set_timer,
7324 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007325{
7326 ssl->p_timer = p_timer;
7327 ssl->f_set_timer = f_set_timer;
7328 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007329
7330 /* Make sure we start with no timer running */
7331 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007332}
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007335void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007336 void *p_cache,
7337 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7338 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007339{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007340 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007341 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007342 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346#if defined(MBEDTLS_SSL_CLI_C)
7347int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007348{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007349 int ret;
7350
7351 if( ssl == NULL ||
7352 session == NULL ||
7353 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007354 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007357 }
7358
7359 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7360 return( ret );
7361
Paul Bakker0a597072012-09-25 21:55:46 +00007362 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007363
7364 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007367
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007368void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007369 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007371 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7372 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7373 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7374 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007375}
7376
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007377void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007378 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007379 int major, int minor )
7380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007382 return;
7383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007385 return;
7386
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007387 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007388}
7389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007391void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007392 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007393{
7394 conf->cert_profile = profile;
7395}
7396
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007397/* Append a new keycert entry to a (possibly empty) list */
7398static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7399 mbedtls_x509_crt *cert,
7400 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007401{
niisato8ee24222018-06-25 19:05:48 +09007402 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007403
niisato8ee24222018-06-25 19:05:48 +09007404 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7405 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007406 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007407
niisato8ee24222018-06-25 19:05:48 +09007408 new_cert->cert = cert;
7409 new_cert->key = key;
7410 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007411
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007412 /* Update head is the list was null, else add to the end */
7413 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007414 {
niisato8ee24222018-06-25 19:05:48 +09007415 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007416 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007417 else
7418 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007419 mbedtls_ssl_key_cert *cur = *head;
7420 while( cur->next != NULL )
7421 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007422 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007423 }
7424
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007425 return( 0 );
7426}
7427
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007428int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007429 mbedtls_x509_crt *own_cert,
7430 mbedtls_pk_context *pk_key )
7431{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007432 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007433}
7434
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007435void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007436 mbedtls_x509_crt *ca_chain,
7437 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007438{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007439 conf->ca_chain = ca_chain;
7440 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007443
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007444#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7445int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7446 mbedtls_x509_crt *own_cert,
7447 mbedtls_pk_context *pk_key )
7448{
7449 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7450 own_cert, pk_key ) );
7451}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007452
7453void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7454 mbedtls_x509_crt *ca_chain,
7455 mbedtls_x509_crl *ca_crl )
7456{
7457 ssl->handshake->sni_ca_chain = ca_chain;
7458 ssl->handshake->sni_ca_crl = ca_crl;
7459}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007460
7461void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7462 int authmode )
7463{
7464 ssl->handshake->sni_authmode = authmode;
7465}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007466#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7467
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007468#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007469/*
7470 * Set EC J-PAKE password for current handshake
7471 */
7472int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7473 const unsigned char *pw,
7474 size_t pw_len )
7475{
7476 mbedtls_ecjpake_role role;
7477
Janos Follath8eb64132016-06-03 15:40:57 +01007478 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7480
7481 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7482 role = MBEDTLS_ECJPAKE_SERVER;
7483 else
7484 role = MBEDTLS_ECJPAKE_CLIENT;
7485
7486 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7487 role,
7488 MBEDTLS_MD_SHA256,
7489 MBEDTLS_ECP_DP_SECP256R1,
7490 pw, pw_len ) );
7491}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007492#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007495
7496static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7497{
7498 /* Remove reference to existing PSK, if any. */
7499#if defined(MBEDTLS_USE_PSA_CRYPTO)
7500 if( conf->psk_opaque != 0 )
7501 {
7502 /* The maintenance of the PSK key slot is the
7503 * user's responsibility. */
7504 conf->psk_opaque = 0;
7505 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007506 /* This and the following branch should never
7507 * be taken simultaenously as we maintain the
7508 * invariant that raw and opaque PSKs are never
7509 * configured simultaneously. As a safeguard,
7510 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007511#endif /* MBEDTLS_USE_PSA_CRYPTO */
7512 if( conf->psk != NULL )
7513 {
7514 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7515
7516 mbedtls_free( conf->psk );
7517 conf->psk = NULL;
7518 conf->psk_len = 0;
7519 }
7520
7521 /* Remove reference to PSK identity, if any. */
7522 if( conf->psk_identity != NULL )
7523 {
7524 mbedtls_free( conf->psk_identity );
7525 conf->psk_identity = NULL;
7526 conf->psk_identity_len = 0;
7527 }
7528}
7529
Hanno Becker7390c712018-11-15 13:33:04 +00007530/* This function assumes that PSK identity in the SSL config is unset.
7531 * It checks that the provided identity is well-formed and attempts
7532 * to make a copy of it in the SSL config.
7533 * On failure, the PSK identity in the config remains unset. */
7534static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7535 unsigned char const *psk_identity,
7536 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007537{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007538 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007539 if( psk_identity == NULL ||
7540 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007541 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007542 {
7543 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7544 }
7545
Hanno Becker7390c712018-11-15 13:33:04 +00007546 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7547 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007549
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007550 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007551 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007552
7553 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007554}
7555
Hanno Becker7390c712018-11-15 13:33:04 +00007556int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7557 const unsigned char *psk, size_t psk_len,
7558 const unsigned char *psk_identity, size_t psk_identity_len )
7559{
7560 int ret;
7561 /* Remove opaque/raw PSK + PSK Identity */
7562 ssl_conf_remove_psk( conf );
7563
7564 /* Check and set raw PSK */
7565 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7567 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7568 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7569 conf->psk_len = psk_len;
7570 memcpy( conf->psk, psk, conf->psk_len );
7571
7572 /* Check and set PSK Identity */
7573 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7574 if( ret != 0 )
7575 ssl_conf_remove_psk( conf );
7576
7577 return( ret );
7578}
7579
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007580static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7581{
7582#if defined(MBEDTLS_USE_PSA_CRYPTO)
7583 if( ssl->handshake->psk_opaque != 0 )
7584 {
7585 ssl->handshake->psk_opaque = 0;
7586 }
7587 else
7588#endif /* MBEDTLS_USE_PSA_CRYPTO */
7589 if( ssl->handshake->psk != NULL )
7590 {
7591 mbedtls_platform_zeroize( ssl->handshake->psk,
7592 ssl->handshake->psk_len );
7593 mbedtls_free( ssl->handshake->psk );
7594 ssl->handshake->psk_len = 0;
7595 }
7596}
7597
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007598int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7599 const unsigned char *psk, size_t psk_len )
7600{
7601 if( psk == NULL || ssl->handshake == NULL )
7602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7603
7604 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7606
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007607 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007608
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007609 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007610 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007611
7612 ssl->handshake->psk_len = psk_len;
7613 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7614
7615 return( 0 );
7616}
7617
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007618#if defined(MBEDTLS_USE_PSA_CRYPTO)
7619int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007620 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007621 const unsigned char *psk_identity,
7622 size_t psk_identity_len )
7623{
Hanno Becker7390c712018-11-15 13:33:04 +00007624 int ret;
7625 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007626 ssl_conf_remove_psk( conf );
7627
Hanno Becker7390c712018-11-15 13:33:04 +00007628 /* Check and set opaque PSK */
7629 if( psk_slot == 0 )
7630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007631 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007632
7633 /* Check and set PSK Identity */
7634 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7635 psk_identity_len );
7636 if( ret != 0 )
7637 ssl_conf_remove_psk( conf );
7638
7639 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007640}
7641
7642int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007643 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007644{
7645 if( psk_slot == 0 || ssl->handshake == NULL )
7646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7647
7648 ssl_remove_psk( ssl );
7649 ssl->handshake->psk_opaque = psk_slot;
7650 return( 0 );
7651}
7652#endif /* MBEDTLS_USE_PSA_CRYPTO */
7653
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007654void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007656 size_t),
7657 void *p_psk )
7658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007659 conf->f_psk = f_psk;
7660 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007663
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007664#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007665
7666#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007667int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007668{
7669 int ret;
7670
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007671 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7672 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7673 {
7674 mbedtls_mpi_free( &conf->dhm_P );
7675 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007676 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007678
7679 return( 0 );
7680}
Hanno Becker470a8c42017-10-04 15:28:46 +01007681#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007682
Hanno Beckera90658f2017-10-04 15:29:08 +01007683int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7684 const unsigned char *dhm_P, size_t P_len,
7685 const unsigned char *dhm_G, size_t G_len )
7686{
7687 int ret;
7688
7689 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7690 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7691 {
7692 mbedtls_mpi_free( &conf->dhm_P );
7693 mbedtls_mpi_free( &conf->dhm_G );
7694 return( ret );
7695 }
7696
7697 return( 0 );
7698}
Paul Bakker5121ce52009-01-03 21:22:43 +00007699
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007700int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007701{
7702 int ret;
7703
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007704 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7705 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7706 {
7707 mbedtls_mpi_free( &conf->dhm_P );
7708 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007709 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007710 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007711
7712 return( 0 );
7713}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007714#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007715
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007716#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7717/*
7718 * Set the minimum length for Diffie-Hellman parameters
7719 */
7720void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7721 unsigned int bitlen )
7722{
7723 conf->dhm_min_bitlen = bitlen;
7724}
7725#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7726
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007727#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007728/*
7729 * Set allowed/preferred hashes for handshake signatures
7730 */
7731void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7732 const int *hashes )
7733{
7734 conf->sig_hashes = hashes;
7735}
Hanno Becker947194e2017-04-07 13:25:49 +01007736#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007737
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007738#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007739/*
7740 * Set the allowed elliptic curves
7741 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007742void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007743 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007744{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007745 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007746}
Hanno Becker947194e2017-04-07 13:25:49 +01007747#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007748
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007749#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007751{
Hanno Becker947194e2017-04-07 13:25:49 +01007752 /* Initialize to suppress unnecessary compiler warning */
7753 size_t hostname_len = 0;
7754
7755 /* Check if new hostname is valid before
7756 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007757 if( hostname != NULL )
7758 {
7759 hostname_len = strlen( hostname );
7760
7761 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7763 }
7764
7765 /* Now it's clear that we will overwrite the old hostname,
7766 * so we can free it safely */
7767
7768 if( ssl->hostname != NULL )
7769 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007770 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007771 mbedtls_free( ssl->hostname );
7772 }
7773
7774 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007775
Paul Bakker5121ce52009-01-03 21:22:43 +00007776 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007777 {
7778 ssl->hostname = NULL;
7779 }
7780 else
7781 {
7782 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007783 if( ssl->hostname == NULL )
7784 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007785
Hanno Becker947194e2017-04-07 13:25:49 +01007786 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007787
Hanno Becker947194e2017-04-07 13:25:49 +01007788 ssl->hostname[hostname_len] = '\0';
7789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007790
7791 return( 0 );
7792}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007793#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007794
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007795#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007796void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007798 const unsigned char *, size_t),
7799 void *p_sni )
7800{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007801 conf->f_sni = f_sni;
7802 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007803}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007807int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007808{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007809 size_t cur_len, tot_len;
7810 const char **p;
7811
7812 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007813 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7814 * MUST NOT be truncated."
7815 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007816 */
7817 tot_len = 0;
7818 for( p = protos; *p != NULL; p++ )
7819 {
7820 cur_len = strlen( *p );
7821 tot_len += cur_len;
7822
7823 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007825 }
7826
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007827 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007828
7829 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007830}
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007833{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007834 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007837
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007838void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007839{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007840 conf->max_major_ver = major;
7841 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007842}
7843
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007844void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007845{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007846 conf->min_major_ver = major;
7847 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007848}
7849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007851void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007852{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007853 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007854}
7855#endif
7856
Janos Follath088ce432017-04-10 12:42:31 +01007857#if defined(MBEDTLS_SSL_SRV_C)
7858void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7859 char cert_req_ca_list )
7860{
7861 conf->cert_req_ca_list = cert_req_ca_list;
7862}
7863#endif
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007866void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007868 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007869}
7870#endif
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007873void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007874{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007875 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007876}
7877#endif
7878
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007879#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007880void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007881{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007882 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007883}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007884#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007887int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007890 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007893 }
7894
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007895 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007896
7897 return( 0 );
7898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007902void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007903{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007904 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007905}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007909void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007910{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007911 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007912}
7913#endif
7914
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007915void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007916{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007917 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007918}
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007921void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007922{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007923 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007924}
7925
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007926void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007928 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007929}
7930
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007931void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007932 const unsigned char period[8] )
7933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007934 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007939#if defined(MBEDTLS_SSL_CLI_C)
7940void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007941{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007942 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007943}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007944#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007945
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007946#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007947void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7948 mbedtls_ssl_ticket_write_t *f_ticket_write,
7949 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7950 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007951{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007952 conf->f_ticket_write = f_ticket_write;
7953 conf->f_ticket_parse = f_ticket_parse;
7954 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007955}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007958
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007959#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7960void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7961 mbedtls_ssl_export_keys_t *f_export_keys,
7962 void *p_export_keys )
7963{
7964 conf->f_export_keys = f_export_keys;
7965 conf->p_export_keys = p_export_keys;
7966}
7967#endif
7968
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007969#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007970void mbedtls_ssl_conf_async_private_cb(
7971 mbedtls_ssl_config *conf,
7972 mbedtls_ssl_async_sign_t *f_async_sign,
7973 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7974 mbedtls_ssl_async_resume_t *f_async_resume,
7975 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007976 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007977{
7978 conf->f_async_sign_start = f_async_sign;
7979 conf->f_async_decrypt_start = f_async_decrypt;
7980 conf->f_async_resume = f_async_resume;
7981 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007982 conf->p_async_config_data = async_config_data;
7983}
7984
Gilles Peskine8f97af72018-04-26 11:46:10 +02007985void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7986{
7987 return( conf->p_async_config_data );
7988}
7989
Gilles Peskine1febfef2018-04-30 11:54:39 +02007990void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007991{
7992 if( ssl->handshake == NULL )
7993 return( NULL );
7994 else
7995 return( ssl->handshake->user_async_ctx );
7996}
7997
Gilles Peskine1febfef2018-04-30 11:54:39 +02007998void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007999 void *ctx )
8000{
8001 if( ssl->handshake != NULL )
8002 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008003}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008004#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008005
Paul Bakker5121ce52009-01-03 21:22:43 +00008006/*
8007 * SSL get accessors
8008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008010{
8011 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8012}
8013
Hanno Becker8b170a02017-10-10 11:51:19 +01008014int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8015{
8016 /*
8017 * Case A: We're currently holding back
8018 * a message for further processing.
8019 */
8020
8021 if( ssl->keep_current_message == 1 )
8022 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008024 return( 1 );
8025 }
8026
8027 /*
8028 * Case B: Further records are pending in the current datagram.
8029 */
8030
8031#if defined(MBEDTLS_SSL_PROTO_DTLS)
8032 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8033 ssl->in_left > ssl->next_record_offset )
8034 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008035 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008036 return( 1 );
8037 }
8038#endif /* MBEDTLS_SSL_PROTO_DTLS */
8039
8040 /*
8041 * Case C: A handshake message is being processed.
8042 */
8043
Hanno Becker8b170a02017-10-10 11:51:19 +01008044 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8045 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008047 return( 1 );
8048 }
8049
8050 /*
8051 * Case D: An application data message is being processed
8052 */
8053 if( ssl->in_offt != NULL )
8054 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008056 return( 1 );
8057 }
8058
8059 /*
8060 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008061 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008062 * we implement support for multiple alerts in single records.
8063 */
8064
8065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8066 return( 0 );
8067}
8068
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008069uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008070{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008071 if( ssl->session != NULL )
8072 return( ssl->session->verify_result );
8073
8074 if( ssl->session_negotiate != NULL )
8075 return( ssl->session_negotiate->verify_result );
8076
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008077 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008078}
8079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008081{
Paul Bakker926c8e42013-03-06 10:23:34 +01008082 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008083 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008086}
8087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008091 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008092 {
8093 switch( ssl->minor_ver )
8094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008096 return( "DTLSv1.0" );
8097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008099 return( "DTLSv1.2" );
8100
8101 default:
8102 return( "unknown (DTLS)" );
8103 }
8104 }
8105#endif
8106
Paul Bakker43ca69c2011-01-15 17:35:19 +00008107 switch( ssl->minor_ver )
8108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008110 return( "SSLv3.0" );
8111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008113 return( "TLSv1.0" );
8114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008116 return( "TLSv1.1" );
8117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008119 return( "TLSv1.2" );
8120
Paul Bakker43ca69c2011-01-15 17:35:19 +00008121 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008122 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008123 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008124}
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008127{
Hanno Becker3136ede2018-08-17 15:28:19 +01008128 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008130 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008131
Hanno Becker78640902018-08-13 16:35:15 +01008132 if( transform == NULL )
8133 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_ZLIB_SUPPORT)
8136 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8137 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008138#endif
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 case MBEDTLS_MODE_GCM:
8143 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008144 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008146 transform_expansion = transform->minlen;
8147 break;
8148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008150
8151 block_size = mbedtls_cipher_get_block_size(
8152 &transform->cipher_ctx_enc );
8153
Hanno Becker3136ede2018-08-17 15:28:19 +01008154 /* Expansion due to the addition of the MAC. */
8155 transform_expansion += transform->maclen;
8156
8157 /* Expansion due to the addition of CBC padding;
8158 * Theoretically up to 256 bytes, but we never use
8159 * more than the block size of the underlying cipher. */
8160 transform_expansion += block_size;
8161
8162 /* For TLS 1.1 or higher, an explicit IV is added
8163 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008164#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8165 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008166 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008167#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008168
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008169 break;
8170
8171 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008174 }
8175
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008176 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008177}
8178
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008179#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8180size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8181{
8182 size_t max_len;
8183
8184 /*
8185 * Assume mfl_code is correct since it was checked when set
8186 */
Angus Grattond8213d02016-05-25 20:56:48 +10008187 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008188
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008189 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008190 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008191 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008192 {
Angus Grattond8213d02016-05-25 20:56:48 +10008193 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008194 }
8195
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008196 /* During a handshake, use the value being negotiated */
8197 if( ssl->session_negotiate != NULL &&
8198 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8199 {
8200 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8201 }
8202
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008203 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008204}
8205#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8206
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008207#if defined(MBEDTLS_SSL_PROTO_DTLS)
8208static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8209{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008210 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8211 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8212 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8213 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8214 return ( 0 );
8215
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008216 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8217 return( ssl->mtu );
8218
8219 if( ssl->mtu == 0 )
8220 return( ssl->handshake->mtu );
8221
8222 return( ssl->mtu < ssl->handshake->mtu ?
8223 ssl->mtu : ssl->handshake->mtu );
8224}
8225#endif /* MBEDTLS_SSL_PROTO_DTLS */
8226
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008227int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8228{
8229 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8230
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008231#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8232 !defined(MBEDTLS_SSL_PROTO_DTLS)
8233 (void) ssl;
8234#endif
8235
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008236#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8237 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8238
8239 if( max_len > mfl )
8240 max_len = mfl;
8241#endif
8242
8243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008244 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008245 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008246 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008247 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8248 const size_t overhead = (size_t) ret;
8249
8250 if( ret < 0 )
8251 return( ret );
8252
8253 if( mtu <= overhead )
8254 {
8255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8256 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8257 }
8258
8259 if( max_len > mtu - overhead )
8260 max_len = mtu - overhead;
8261 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008262#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008263
Hanno Becker0defedb2018-08-10 12:35:02 +01008264#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8265 !defined(MBEDTLS_SSL_PROTO_DTLS)
8266 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008267#endif
8268
8269 return( (int) max_len );
8270}
8271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272#if defined(MBEDTLS_X509_CRT_PARSE_C)
8273const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008274{
8275 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008276 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008277
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008278 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282#if defined(MBEDTLS_SSL_CLI_C)
8283int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008284{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008285 if( ssl == NULL ||
8286 dst == NULL ||
8287 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008288 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008291 }
8292
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008293 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008296
Paul Bakker5121ce52009-01-03 21:22:43 +00008297/*
Paul Bakker1961b702013-01-25 14:49:24 +01008298 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008303
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008304 if( ssl == NULL || ssl->conf == NULL )
8305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008308 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008312 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008314#endif
8315
Paul Bakker1961b702013-01-25 14:49:24 +01008316 return( ret );
8317}
8318
8319/*
8320 * Perform the SSL handshake
8321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008323{
8324 int ret = 0;
8325
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008326 if( ssl == NULL || ssl->conf == NULL )
8327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008334
8335 if( ret != 0 )
8336 break;
8337 }
8338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008340
8341 return( ret );
8342}
8343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_RENEGOTIATION)
8345#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008346/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008347 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008350{
8351 int ret;
8352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008354
8355 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8357 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008358
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008359 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008360 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008362 return( ret );
8363 }
8364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008366
8367 return( 0 );
8368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008370
8371/*
8372 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373 * - any side: calling mbedtls_ssl_renegotiate(),
8374 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8375 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008376 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008377 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008381{
8382 int ret;
8383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008385
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008386 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8387 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008388
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008389 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8390 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008392 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008394 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008395 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008396 ssl->handshake->out_msg_seq = 1;
8397 else
8398 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008399 }
8400#endif
8401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8403 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008408 return( ret );
8409 }
8410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008412
8413 return( 0 );
8414}
8415
8416/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008417 * Renegotiate current connection on client,
8418 * or request renegotiation on server
8419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008423
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008424 if( ssl == NULL || ssl->conf == NULL )
8425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008428 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008429 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008435
8436 /* Did we already try/start sending HelloRequest? */
8437 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008439
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008440 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008442#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008445 /*
8446 * On client, either start the renegotiation process or,
8447 * if already in progress, continue the handshake
8448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008453
8454 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008457 return( ret );
8458 }
8459 }
8460 else
8461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008465 return( ret );
8466 }
8467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008469
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008470 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008471}
8472
8473/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008474 * Check record counters and renegotiate if they're above the limit.
8475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008477{
Andres AG2196c7f2016-12-15 17:01:16 +00008478 size_t ep_len = ssl_ep_len( ssl );
8479 int in_ctr_cmp;
8480 int out_ctr_cmp;
8481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8483 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008484 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008485 {
8486 return( 0 );
8487 }
8488
Andres AG2196c7f2016-12-15 17:01:16 +00008489 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8490 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008491 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008492 ssl->conf->renego_period + ep_len, 8 - ep_len );
8493
8494 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008495 {
8496 return( 0 );
8497 }
8498
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008503
8504/*
8505 * Receive application data decrypted from the SSL layer
8506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008508{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008509 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008510 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008511
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008512 if( ssl == NULL || ssl->conf == NULL )
8513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008518 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008521 return( ret );
8522
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008523 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008525 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008526 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008527 return( ret );
8528 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008529 }
8530#endif
8531
Hanno Becker4a810fb2017-05-24 16:27:30 +01008532 /*
8533 * Check if renegotiation is necessary and/or handshake is
8534 * in process. If yes, perform/continue, and fall through
8535 * if an unexpected packet is received while the client
8536 * is waiting for the ServerHello.
8537 *
8538 * (There is no equivalent to the last condition on
8539 * the server-side as it is not treated as within
8540 * a handshake while waiting for the ClientHello
8541 * after a renegotiation request.)
8542 */
8543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008545 ret = ssl_check_ctr_renegotiate( ssl );
8546 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8547 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008550 return( ret );
8551 }
8552#endif
8553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008557 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8558 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008561 return( ret );
8562 }
8563 }
8564
Hanno Beckere41158b2017-10-23 13:30:32 +01008565 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008566 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008567 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008568 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008569 if( ssl->f_get_timer != NULL &&
8570 ssl->f_get_timer( ssl->p_timer ) == -1 )
8571 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008572 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008573 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008574
Hanno Becker327c93b2018-08-15 13:56:18 +01008575 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008576 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008577 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8578 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008579
Hanno Becker4a810fb2017-05-24 16:27:30 +01008580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8581 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008582 }
8583
8584 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008586 {
8587 /*
8588 * OpenSSL sends empty messages to randomize the IV
8589 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008590 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008592 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008593 return( 0 );
8594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008596 return( ret );
8597 }
8598 }
8599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008603
Hanno Becker4a810fb2017-05-24 16:27:30 +01008604 /*
8605 * - For client-side, expect SERVER_HELLO_REQUEST.
8606 * - For server-side, expect CLIENT_HELLO.
8607 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8608 */
8609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008611 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008613 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008616
8617 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008619 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008620 {
8621 continue;
8622 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008625 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008626#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008627
Hanno Becker4a810fb2017-05-24 16:27:30 +01008628#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008629 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008633
8634 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008636 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008637 {
8638 continue;
8639 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008640#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008642 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008643#endif /* MBEDTLS_SSL_SRV_C */
8644
Hanno Becker21df7f92017-10-17 11:03:26 +01008645#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008646 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008647 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8648 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8649 ssl->conf->allow_legacy_renegotiation ==
8650 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8651 {
8652 /*
8653 * Accept renegotiation request
8654 */
Paul Bakker48916f92012-09-16 19:57:18 +00008655
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008656 /* DTLS clients need to know renego is server-initiated */
8657#if defined(MBEDTLS_SSL_PROTO_DTLS)
8658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8659 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8660 {
8661 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8662 }
8663#endif
8664 ret = ssl_start_renegotiation( ssl );
8665 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8666 ret != 0 )
8667 {
8668 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8669 return( ret );
8670 }
8671 }
8672 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008673#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008674 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008675 /*
8676 * Refuse renegotiation
8677 */
8678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_SSL_PROTO_SSL3)
8682 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008683 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008684 /* SSLv3 does not have a "no_renegotiation" warning, so
8685 we send a fatal alert and abort the connection. */
8686 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8687 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8688 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008689 }
8690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8692#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8693 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8694 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8697 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8698 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008699 {
8700 return( ret );
8701 }
Paul Bakker48916f92012-09-16 19:57:18 +00008702 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008703 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8705 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008709 }
Paul Bakker48916f92012-09-16 19:57:18 +00008710 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008711
Hanno Becker90333da2017-10-10 11:27:13 +01008712 /* At this point, we don't know whether the renegotiation has been
8713 * completed or not. The cases to consider are the following:
8714 * 1) The renegotiation is complete. In this case, no new record
8715 * has been read yet.
8716 * 2) The renegotiation is incomplete because the client received
8717 * an application data record while awaiting the ServerHello.
8718 * 3) The renegotiation is incomplete because the client received
8719 * a non-handshake, non-application data message while awaiting
8720 * the ServerHello.
8721 * In each of these case, looping will be the proper action:
8722 * - For 1), the next iteration will read a new record and check
8723 * if it's application data.
8724 * - For 2), the loop condition isn't satisfied as application data
8725 * is present, hence continue is the same as break
8726 * - For 3), the loop condition is satisfied and read_record
8727 * will re-deliver the message that was held back by the client
8728 * when expecting the ServerHello.
8729 */
8730 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008731 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008732#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008734 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008735 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008736 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008737 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008740 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008742 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008743 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008744 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8748 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008751 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008752 }
8753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8757 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008758 }
8759
8760 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008761
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008762 /* We're going to return something now, cancel timer,
8763 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008765 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008766
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008767#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008768 /* If we requested renego but received AppData, resend HelloRequest.
8769 * Do it now, after setting in_offt, to avoid taking this branch
8770 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008772 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008774 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008775 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008778 return( ret );
8779 }
8780 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008782#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008783 }
8784
8785 n = ( len < ssl->in_msglen )
8786 ? len : ssl->in_msglen;
8787
8788 memcpy( buf, ssl->in_offt, n );
8789 ssl->in_msglen -= n;
8790
8791 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008792 {
8793 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008794 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008795 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008796 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008797 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008798 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008799 /* more data available */
8800 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008801 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008804
Paul Bakker23986e52011-04-24 08:57:21 +00008805 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008806}
8807
8808/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008809 * Send application data to be encrypted by the SSL layer, taking care of max
8810 * fragment length and buffer size.
8811 *
8812 * According to RFC 5246 Section 6.2.1:
8813 *
8814 * Zero-length fragments of Application data MAY be sent as they are
8815 * potentially useful as a traffic analysis countermeasure.
8816 *
8817 * Therefore, it is possible that the input message length is 0 and the
8818 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008819 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008820static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008821 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008822{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008823 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8824 const size_t max_len = (size_t) ret;
8825
8826 if( ret < 0 )
8827 {
8828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8829 return( ret );
8830 }
8831
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008832 if( len > max_len )
8833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008835 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008838 "maximum fragment length: %d > %d",
8839 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008841 }
8842 else
8843#endif
8844 len = max_len;
8845 }
Paul Bakker887bd502011-06-08 13:10:54 +00008846
Paul Bakker5121ce52009-01-03 21:22:43 +00008847 if( ssl->out_left != 0 )
8848 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008849 /*
8850 * The user has previously tried to send the data and
8851 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8852 * written. In this case, we expect the high-level write function
8853 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008858 return( ret );
8859 }
8860 }
Paul Bakker887bd502011-06-08 13:10:54 +00008861 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008862 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008863 /*
8864 * The user is trying to send a message the first time, so we need to
8865 * copy the data into the internal buffers and setup the data structure
8866 * to keep track of partial writes
8867 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008868 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008870 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008871
Hanno Becker67bc7c32018-08-06 11:33:50 +01008872 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008875 return( ret );
8876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008877 }
8878
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008879 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008880}
8881
8882/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008883 * Write application data, doing 1/n-1 splitting if necessary.
8884 *
8885 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008886 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008887 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008890static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008891 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008892{
8893 int ret;
8894
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008895 if( ssl->conf->cbc_record_splitting ==
8896 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008897 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8899 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8900 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008901 {
8902 return( ssl_write_real( ssl, buf, len ) );
8903 }
8904
8905 if( ssl->split_done == 0 )
8906 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008907 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008908 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008909 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008910 }
8911
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008912 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8913 return( ret );
8914 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008915
8916 return( ret + 1 );
8917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008918#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008919
8920/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008921 * Write application data (public-facing wrapper)
8922 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008923int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008924{
8925 int ret;
8926
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008928
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008929 if( ssl == NULL || ssl->conf == NULL )
8930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8931
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008932#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008933 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8934 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008935 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008936 return( ret );
8937 }
8938#endif
8939
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008940 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008941 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008942 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008943 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008945 return( ret );
8946 }
8947 }
8948
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008949#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008950 ret = ssl_write_split( ssl, buf, len );
8951#else
8952 ret = ssl_write_real( ssl, buf, len );
8953#endif
8954
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008956
8957 return( ret );
8958}
8959
8960/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008961 * Notify the peer that the connection is being closed
8962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008964{
8965 int ret;
8966
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008967 if( ssl == NULL || ssl->conf == NULL )
8968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008971
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008972 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8978 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8979 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008982 return( ret );
8983 }
8984 }
8985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008987
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008988 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008989}
8990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008992{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008993 if( transform == NULL )
8994 return;
8995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008997 deflateEnd( &transform->ctx_deflate );
8998 inflateEnd( &transform->ctx_inflate );
8999#endif
9000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9002 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004 mbedtls_md_free( &transform->md_ctx_enc );
9005 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009006
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009007 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009008}
9009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010#if defined(MBEDTLS_X509_CRT_PARSE_C)
9011static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009014
9015 while( cur != NULL )
9016 {
9017 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009019 cur = next;
9020 }
9021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009023
Hanno Becker0271f962018-08-16 13:23:47 +01009024#if defined(MBEDTLS_SSL_PROTO_DTLS)
9025
9026static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9027{
9028 unsigned offset;
9029 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9030
9031 if( hs == NULL )
9032 return;
9033
Hanno Becker283f5ef2018-08-24 09:34:47 +01009034 ssl_free_buffered_record( ssl );
9035
Hanno Becker0271f962018-08-16 13:23:47 +01009036 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009037 ssl_buffering_free_slot( ssl, offset );
9038}
9039
9040static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9041 uint8_t slot )
9042{
9043 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9044 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009045
9046 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9047 return;
9048
Hanno Beckere605b192018-08-21 15:59:07 +01009049 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009050 {
Hanno Beckere605b192018-08-21 15:59:07 +01009051 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009052 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009053 mbedtls_free( hs_buf->data );
9054 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009055 }
9056}
9057
9058#endif /* MBEDTLS_SSL_PROTO_DTLS */
9059
Gilles Peskine9b562d52018-04-25 20:32:43 +02009060void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009061{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009062 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9063
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009064 if( handshake == NULL )
9065 return;
9066
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009067#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9068 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9069 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009070 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009071 handshake->async_in_progress = 0;
9072 }
9073#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9074
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009075#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9076 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9077 mbedtls_md5_free( &handshake->fin_md5 );
9078 mbedtls_sha1_free( &handshake->fin_sha1 );
9079#endif
9080#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9081#if defined(MBEDTLS_SHA256_C)
9082 mbedtls_sha256_free( &handshake->fin_sha256 );
9083#endif
9084#if defined(MBEDTLS_SHA512_C)
9085 mbedtls_sha512_free( &handshake->fin_sha512 );
9086#endif
9087#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089#if defined(MBEDTLS_DHM_C)
9090 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#if defined(MBEDTLS_ECDH_C)
9093 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009094#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009095#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009096 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009097#if defined(MBEDTLS_SSL_CLI_C)
9098 mbedtls_free( handshake->ecjpake_cache );
9099 handshake->ecjpake_cache = NULL;
9100 handshake->ecjpake_cache_len = 0;
9101#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009102#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009103
Janos Follath4ae5c292016-02-10 11:27:43 +00009104#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9105 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009106 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009108#endif
9109
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009110#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9111 if( handshake->psk != NULL )
9112 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009113 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009114 mbedtls_free( handshake->psk );
9115 }
9116#endif
9117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9119 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009120 /*
9121 * Free only the linked list wrapper, not the keys themselves
9122 * since the belong to the SNI callback
9123 */
9124 if( handshake->sni_key_cert != NULL )
9125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009127
9128 while( cur != NULL )
9129 {
9130 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009132 cur = next;
9133 }
9134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009136
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009137#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009138 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009139#endif
9140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141#if defined(MBEDTLS_SSL_PROTO_DTLS)
9142 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009143 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009144 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009145#endif
9146
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009147 mbedtls_platform_zeroize( handshake,
9148 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009149}
9150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009152{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009153 if( session == NULL )
9154 return;
9155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009157 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159 mbedtls_x509_crt_free( session->peer_cert );
9160 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009161 }
Paul Bakkered27a042013-04-18 22:46:23 +02009162#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009163
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009164#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009166#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009167
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009168 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009169}
9170
Paul Bakker5121ce52009-01-03 21:22:43 +00009171/*
9172 * Free an SSL context
9173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009175{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009176 if( ssl == NULL )
9177 return;
9178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009180
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009181 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009182 {
Angus Grattond8213d02016-05-25 20:56:48 +10009183 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009185 }
9186
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009187 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009188 {
Angus Grattond8213d02016-05-25 20:56:48 +10009189 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009191 }
9192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009194 if( ssl->compress_buf != NULL )
9195 {
Angus Grattond8213d02016-05-25 20:56:48 +10009196 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009198 }
9199#endif
9200
Paul Bakker48916f92012-09-16 19:57:18 +00009201 if( ssl->transform )
9202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203 mbedtls_ssl_transform_free( ssl->transform );
9204 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009205 }
9206
9207 if( ssl->handshake )
9208 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009209 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9211 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213 mbedtls_free( ssl->handshake );
9214 mbedtls_free( ssl->transform_negotiate );
9215 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009216 }
9217
Paul Bakkerc0463502013-02-14 11:19:38 +01009218 if( ssl->session )
9219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220 mbedtls_ssl_session_free( ssl->session );
9221 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009222 }
9223
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009224#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009225 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009226 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009227 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009229 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009230#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9233 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9236 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009237 }
9238#endif
9239
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009240#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009242#endif
9243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009245
Paul Bakker86f04f42013-02-14 11:20:09 +01009246 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009247 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009248}
9249
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009250/*
9251 * Initialze mbedtls_ssl_config
9252 */
9253void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9254{
9255 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9256}
9257
Simon Butcherc97b6972015-12-27 23:48:17 +00009258#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009259static int ssl_preset_default_hashes[] = {
9260#if defined(MBEDTLS_SHA512_C)
9261 MBEDTLS_MD_SHA512,
9262 MBEDTLS_MD_SHA384,
9263#endif
9264#if defined(MBEDTLS_SHA256_C)
9265 MBEDTLS_MD_SHA256,
9266 MBEDTLS_MD_SHA224,
9267#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009268#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009269 MBEDTLS_MD_SHA1,
9270#endif
9271 MBEDTLS_MD_NONE
9272};
Simon Butcherc97b6972015-12-27 23:48:17 +00009273#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009274
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009275static int ssl_preset_suiteb_ciphersuites[] = {
9276 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9277 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9278 0
9279};
9280
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009281#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009282static int ssl_preset_suiteb_hashes[] = {
9283 MBEDTLS_MD_SHA256,
9284 MBEDTLS_MD_SHA384,
9285 MBEDTLS_MD_NONE
9286};
9287#endif
9288
9289#if defined(MBEDTLS_ECP_C)
9290static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9291 MBEDTLS_ECP_DP_SECP256R1,
9292 MBEDTLS_ECP_DP_SECP384R1,
9293 MBEDTLS_ECP_DP_NONE
9294};
9295#endif
9296
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009297/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009298 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009299 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009300int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009301 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009302{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009303#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009304 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009305#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009306
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009307 /* Use the functions here so that they are covered in tests,
9308 * but otherwise access member directly for efficiency */
9309 mbedtls_ssl_conf_endpoint( conf, endpoint );
9310 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009311
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009312 /*
9313 * Things that are common to all presets
9314 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009315#if defined(MBEDTLS_SSL_CLI_C)
9316 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9317 {
9318 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9319#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9320 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9321#endif
9322 }
9323#endif
9324
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009325#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009326 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009327#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009328
9329#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9330 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9331#endif
9332
9333#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9334 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9335#endif
9336
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009337#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9338 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9339#endif
9340
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009341#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009342 conf->f_cookie_write = ssl_cookie_write_dummy;
9343 conf->f_cookie_check = ssl_cookie_check_dummy;
9344#endif
9345
9346#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9347 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9348#endif
9349
Janos Follath088ce432017-04-10 12:42:31 +01009350#if defined(MBEDTLS_SSL_SRV_C)
9351 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9352#endif
9353
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009354#if defined(MBEDTLS_SSL_PROTO_DTLS)
9355 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9356 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9357#endif
9358
9359#if defined(MBEDTLS_SSL_RENEGOTIATION)
9360 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009361 memset( conf->renego_period, 0x00, 2 );
9362 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009363#endif
9364
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009365#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9366 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9367 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009368 const unsigned char dhm_p[] =
9369 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9370 const unsigned char dhm_g[] =
9371 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9372
Hanno Beckera90658f2017-10-04 15:29:08 +01009373 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9374 dhm_p, sizeof( dhm_p ),
9375 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009376 {
9377 return( ret );
9378 }
9379 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009380#endif
9381
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009382 /*
9383 * Preset-specific defaults
9384 */
9385 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009386 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009387 /*
9388 * NSA Suite B
9389 */
9390 case MBEDTLS_SSL_PRESET_SUITEB:
9391 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9392 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9393 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9394 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9395
9396 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9397 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9398 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9399 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9400 ssl_preset_suiteb_ciphersuites;
9401
9402#if defined(MBEDTLS_X509_CRT_PARSE_C)
9403 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009404#endif
9405
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009406#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009407 conf->sig_hashes = ssl_preset_suiteb_hashes;
9408#endif
9409
9410#if defined(MBEDTLS_ECP_C)
9411 conf->curve_list = ssl_preset_suiteb_curves;
9412#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009413 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009414
9415 /*
9416 * Default
9417 */
9418 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009419 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9420 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9421 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9422 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9423 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9424 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9425 MBEDTLS_SSL_MIN_MINOR_VERSION :
9426 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009427 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9428 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9429
9430#if defined(MBEDTLS_SSL_PROTO_DTLS)
9431 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9432 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9433#endif
9434
9435 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9436 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9437 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9438 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9439 mbedtls_ssl_list_ciphersuites();
9440
9441#if defined(MBEDTLS_X509_CRT_PARSE_C)
9442 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9443#endif
9444
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009445#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009446 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009447#endif
9448
9449#if defined(MBEDTLS_ECP_C)
9450 conf->curve_list = mbedtls_ecp_grp_id_list();
9451#endif
9452
9453#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9454 conf->dhm_min_bitlen = 1024;
9455#endif
9456 }
9457
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009458 return( 0 );
9459}
9460
9461/*
9462 * Free mbedtls_ssl_config
9463 */
9464void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9465{
9466#if defined(MBEDTLS_DHM_C)
9467 mbedtls_mpi_free( &conf->dhm_P );
9468 mbedtls_mpi_free( &conf->dhm_G );
9469#endif
9470
9471#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9472 if( conf->psk != NULL )
9473 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009474 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009475 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009476 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009477 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009478 }
9479
9480 if( conf->psk_identity != NULL )
9481 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009482 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009483 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009484 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009485 conf->psk_identity_len = 0;
9486 }
9487#endif
9488
9489#if defined(MBEDTLS_X509_CRT_PARSE_C)
9490 ssl_key_cert_free( conf->key_cert );
9491#endif
9492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009493 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009494}
9495
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009496#if defined(MBEDTLS_PK_C) && \
9497 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009498/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503#if defined(MBEDTLS_RSA_C)
9504 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9505 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_ECDSA_C)
9508 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9509 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009512}
9513
Hanno Becker7e5437a2017-04-28 17:15:26 +01009514unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9515{
9516 switch( type ) {
9517 case MBEDTLS_PK_RSA:
9518 return( MBEDTLS_SSL_SIG_RSA );
9519 case MBEDTLS_PK_ECDSA:
9520 case MBEDTLS_PK_ECKEY:
9521 return( MBEDTLS_SSL_SIG_ECDSA );
9522 default:
9523 return( MBEDTLS_SSL_SIG_ANON );
9524 }
9525}
9526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009528{
9529 switch( sig )
9530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_RSA_C)
9532 case MBEDTLS_SSL_SIG_RSA:
9533 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#if defined(MBEDTLS_ECDSA_C)
9536 case MBEDTLS_SSL_SIG_ECDSA:
9537 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009538#endif
9539 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009541 }
9542}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009543#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009544
Hanno Becker7e5437a2017-04-28 17:15:26 +01009545#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9546 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9547
9548/* Find an entry in a signature-hash set matching a given hash algorithm. */
9549mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9550 mbedtls_pk_type_t sig_alg )
9551{
9552 switch( sig_alg )
9553 {
9554 case MBEDTLS_PK_RSA:
9555 return( set->rsa );
9556 case MBEDTLS_PK_ECDSA:
9557 return( set->ecdsa );
9558 default:
9559 return( MBEDTLS_MD_NONE );
9560 }
9561}
9562
9563/* Add a signature-hash-pair to a signature-hash set */
9564void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9565 mbedtls_pk_type_t sig_alg,
9566 mbedtls_md_type_t md_alg )
9567{
9568 switch( sig_alg )
9569 {
9570 case MBEDTLS_PK_RSA:
9571 if( set->rsa == MBEDTLS_MD_NONE )
9572 set->rsa = md_alg;
9573 break;
9574
9575 case MBEDTLS_PK_ECDSA:
9576 if( set->ecdsa == MBEDTLS_MD_NONE )
9577 set->ecdsa = md_alg;
9578 break;
9579
9580 default:
9581 break;
9582 }
9583}
9584
9585/* Allow exactly one hash algorithm for each signature. */
9586void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9587 mbedtls_md_type_t md_alg )
9588{
9589 set->rsa = md_alg;
9590 set->ecdsa = md_alg;
9591}
9592
9593#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9594 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9595
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009596/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009597 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009600{
9601 switch( hash )
9602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603#if defined(MBEDTLS_MD5_C)
9604 case MBEDTLS_SSL_HASH_MD5:
9605 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#if defined(MBEDTLS_SHA1_C)
9608 case MBEDTLS_SSL_HASH_SHA1:
9609 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611#if defined(MBEDTLS_SHA256_C)
9612 case MBEDTLS_SSL_HASH_SHA224:
9613 return( MBEDTLS_MD_SHA224 );
9614 case MBEDTLS_SSL_HASH_SHA256:
9615 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617#if defined(MBEDTLS_SHA512_C)
9618 case MBEDTLS_SSL_HASH_SHA384:
9619 return( MBEDTLS_MD_SHA384 );
9620 case MBEDTLS_SSL_HASH_SHA512:
9621 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009622#endif
9623 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009625 }
9626}
9627
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009628/*
9629 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9630 */
9631unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9632{
9633 switch( md )
9634 {
9635#if defined(MBEDTLS_MD5_C)
9636 case MBEDTLS_MD_MD5:
9637 return( MBEDTLS_SSL_HASH_MD5 );
9638#endif
9639#if defined(MBEDTLS_SHA1_C)
9640 case MBEDTLS_MD_SHA1:
9641 return( MBEDTLS_SSL_HASH_SHA1 );
9642#endif
9643#if defined(MBEDTLS_SHA256_C)
9644 case MBEDTLS_MD_SHA224:
9645 return( MBEDTLS_SSL_HASH_SHA224 );
9646 case MBEDTLS_MD_SHA256:
9647 return( MBEDTLS_SSL_HASH_SHA256 );
9648#endif
9649#if defined(MBEDTLS_SHA512_C)
9650 case MBEDTLS_MD_SHA384:
9651 return( MBEDTLS_SSL_HASH_SHA384 );
9652 case MBEDTLS_MD_SHA512:
9653 return( MBEDTLS_SSL_HASH_SHA512 );
9654#endif
9655 default:
9656 return( MBEDTLS_SSL_HASH_NONE );
9657 }
9658}
9659
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009660#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009661/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009662 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009663 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009664 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009665int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009668
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009669 if( ssl->conf->curve_list == NULL )
9670 return( -1 );
9671
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009672 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009673 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009674 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009675
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009676 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009677}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009678#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009679
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009680#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009681/*
9682 * Check if a hash proposed by the peer is in our list.
9683 * Return 0 if we're willing to use it, -1 otherwise.
9684 */
9685int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9686 mbedtls_md_type_t md )
9687{
9688 const int *cur;
9689
9690 if( ssl->conf->sig_hashes == NULL )
9691 return( -1 );
9692
9693 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9694 if( *cur == (int) md )
9695 return( 0 );
9696
9697 return( -1 );
9698}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009699#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701#if defined(MBEDTLS_X509_CRT_PARSE_C)
9702int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9703 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009704 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009705 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009706{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009707 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009709 int usage = 0;
9710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009712 const char *ext_oid;
9713 size_t ext_len;
9714#endif
9715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9717 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009718 ((void) cert);
9719 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009720 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009721#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9724 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009725 {
9726 /* Server part of the key exchange */
9727 switch( ciphersuite->key_exchange )
9728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729 case MBEDTLS_KEY_EXCHANGE_RSA:
9730 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009731 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009732 break;
9733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9735 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9736 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9737 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009738 break;
9739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9741 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009742 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009743 break;
9744
9745 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 case MBEDTLS_KEY_EXCHANGE_NONE:
9747 case MBEDTLS_KEY_EXCHANGE_PSK:
9748 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9749 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009750 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009751 usage = 0;
9752 }
9753 }
9754 else
9755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9757 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009758 }
9759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009761 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009762 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009763 ret = -1;
9764 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009765#else
9766 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9770 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9773 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009774 }
9775 else
9776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9778 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009779 }
9780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009782 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009783 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009784 ret = -1;
9785 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009787
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009788 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009791
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009792/*
9793 * Convert version numbers to/from wire format
9794 * and, for DTLS, to/from TLS equivalent.
9795 *
9796 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009797 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009798 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9799 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009802 unsigned char ver[2] )
9803{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804#if defined(MBEDTLS_SSL_PROTO_DTLS)
9805 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009808 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9809
9810 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9811 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9812 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009813 else
9814#else
9815 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009816#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009817 {
9818 ver[0] = (unsigned char) major;
9819 ver[1] = (unsigned char) minor;
9820 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009821}
9822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009824 const unsigned char ver[2] )
9825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826#if defined(MBEDTLS_SSL_PROTO_DTLS)
9827 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009828 {
9829 *major = 255 - ver[0] + 2;
9830 *minor = 255 - ver[1] + 1;
9831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009833 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9834 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009835 else
9836#else
9837 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009838#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009839 {
9840 *major = ver[0];
9841 *minor = ver[1];
9842 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009843}
9844
Simon Butcher99000142016-10-13 17:21:01 +01009845int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9846{
9847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9848 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9849 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9850
9851 switch( md )
9852 {
9853#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9854#if defined(MBEDTLS_MD5_C)
9855 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009856 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009857#endif
9858#if defined(MBEDTLS_SHA1_C)
9859 case MBEDTLS_SSL_HASH_SHA1:
9860 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9861 break;
9862#endif
9863#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9864#if defined(MBEDTLS_SHA512_C)
9865 case MBEDTLS_SSL_HASH_SHA384:
9866 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9867 break;
9868#endif
9869#if defined(MBEDTLS_SHA256_C)
9870 case MBEDTLS_SSL_HASH_SHA256:
9871 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9872 break;
9873#endif
9874 default:
9875 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9876 }
9877
9878 return 0;
9879#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9880 (void) ssl;
9881 (void) md;
9882
9883 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9884#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9885}
9886
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009887#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9888 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9889int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9890 unsigned char *output,
9891 unsigned char *data, size_t data_len )
9892{
9893 int ret = 0;
9894 mbedtls_md5_context mbedtls_md5;
9895 mbedtls_sha1_context mbedtls_sha1;
9896
9897 mbedtls_md5_init( &mbedtls_md5 );
9898 mbedtls_sha1_init( &mbedtls_sha1 );
9899
9900 /*
9901 * digitally-signed struct {
9902 * opaque md5_hash[16];
9903 * opaque sha_hash[20];
9904 * };
9905 *
9906 * md5_hash
9907 * MD5(ClientHello.random + ServerHello.random
9908 * + ServerParams);
9909 * sha_hash
9910 * SHA(ClientHello.random + ServerHello.random
9911 * + ServerParams);
9912 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009913 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009914 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009916 goto exit;
9917 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009918 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009919 ssl->handshake->randbytes, 64 ) ) != 0 )
9920 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009922 goto exit;
9923 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009924 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009925 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009927 goto exit;
9928 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009929 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009930 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009932 goto exit;
9933 }
9934
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009935 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009936 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009938 goto exit;
9939 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009940 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009941 ssl->handshake->randbytes, 64 ) ) != 0 )
9942 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009944 goto exit;
9945 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009946 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009947 data_len ) ) != 0 )
9948 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009950 goto exit;
9951 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009952 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009953 output + 16 ) ) != 0 )
9954 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009956 goto exit;
9957 }
9958
9959exit:
9960 mbedtls_md5_free( &mbedtls_md5 );
9961 mbedtls_sha1_free( &mbedtls_sha1 );
9962
9963 if( ret != 0 )
9964 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9965 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9966
9967 return( ret );
9968
9969}
9970#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9971 MBEDTLS_SSL_PROTO_TLS1_1 */
9972
9973#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9974 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9975int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02009976 unsigned char *hash, size_t *hashlen,
9977 unsigned char *data, size_t data_len,
9978 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009979{
9980 int ret = 0;
9981 mbedtls_md_context_t ctx;
9982 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02009983 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009984
9985 mbedtls_md_init( &ctx );
9986
9987 /*
9988 * digitally-signed struct {
9989 * opaque client_random[32];
9990 * opaque server_random[32];
9991 * ServerDHParams params;
9992 * };
9993 */
9994 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9995 {
9996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9997 goto exit;
9998 }
9999 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10000 {
10001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10002 goto exit;
10003 }
10004 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10005 {
10006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10007 goto exit;
10008 }
10009 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10010 {
10011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10012 goto exit;
10013 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010014 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010015 {
10016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10017 goto exit;
10018 }
10019
10020exit:
10021 mbedtls_md_free( &ctx );
10022
10023 if( ret != 0 )
10024 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10025 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10026
10027 return( ret );
10028}
10029#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10030 MBEDTLS_SSL_PROTO_TLS1_2 */
10031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032#endif /* MBEDTLS_SSL_TLS_C */