blob: 675150d56e593ceea247935fbc9c27e08f585f93 [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
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
110 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckerd5847772018-08-28 10:09:23 +0100117/* Forward declarations for functions related to message buffering. */
118static void ssl_buffering_free( mbedtls_ssl_context *ssl );
119static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
120 uint8_t slot );
121static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
122static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
123static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
124static int ssl_buffer_message( mbedtls_ssl_context *ssl );
125static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100126static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100127
Hanno Beckera67dee22018-08-22 10:05:20 +0100128static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100129static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100130{
Hanno Becker11682cc2018-08-22 14:41:02 +0100131 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100132
133 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100134 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100135
136 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
137}
138
Hanno Becker67bc7c32018-08-06 11:33:50 +0100139static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
140{
Hanno Becker11682cc2018-08-22 14:41:02 +0100141 size_t const bytes_written = ssl->out_left;
142 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100143
144 /* Double-check that the write-index hasn't gone
145 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100146 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147 {
148 /* Should never happen... */
149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
150 }
151
152 return( (int) ( mtu - bytes_written ) );
153}
154
155static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
156{
157 int ret;
158 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400159 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100160
161#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
162 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
163
164 if( max_len > mfl )
165 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100166
167 /* By the standard (RFC 6066 Sect. 4), the MFL extension
168 * only limits the maximum record payload size, so in theory
169 * we would be allowed to pack multiple records of payload size
170 * MFL into a single datagram. However, this would mean that there's
171 * no way to explicitly communicate MTU restrictions to the peer.
172 *
173 * The following reduction of max_len makes sure that we never
174 * write datagrams larger than MFL + Record Expansion Overhead.
175 */
176 if( max_len <= ssl->out_left )
177 return( 0 );
178
179 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100180#endif
181
182 ret = ssl_get_remaining_space_in_datagram( ssl );
183 if( ret < 0 )
184 return( ret );
185 remaining = (size_t) ret;
186
187 ret = mbedtls_ssl_get_record_expansion( ssl );
188 if( ret < 0 )
189 return( ret );
190 expansion = (size_t) ret;
191
192 if( remaining <= expansion )
193 return( 0 );
194
195 remaining -= expansion;
196 if( remaining >= max_len )
197 remaining = max_len;
198
199 return( (int) remaining );
200}
201
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202/*
203 * Double the retransmit timeout value, within the allowed range,
204 * returning -1 if the maximum value has already been reached.
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200207{
208 uint32_t new_timeout;
209
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200210 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211 return( -1 );
212
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200213 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
214 * in the following way: after the initial transmission and a first
215 * retransmission, back off to a temporary estimated MTU of 508 bytes.
216 * This value is guaranteed to be deliverable (if not guaranteed to be
217 * delivered) of any compliant IPv4 (and IPv6) network, and should work
218 * on most non-IP stacks too. */
219 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400220 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200221 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
223 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200224
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200225 new_timeout = 2 * ssl->handshake->retransmit_timeout;
226
227 /* Avoid arithmetic overflow and range overflow */
228 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200229 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200230 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200231 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200232 }
233
234 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200236 ssl->handshake->retransmit_timeout ) );
237
238 return( 0 );
239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200242{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200243 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200245 ssl->handshake->retransmit_timeout ) );
246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200250/*
251 * Convert max_fragment_length codes to length.
252 * RFC 6066 says:
253 * enum{
254 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
255 * } MaxFragmentLength;
256 * and we add 0 -> extension unused
257 */
Angus Grattond8213d02016-05-25 20:56:48 +1000258static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200259{
Angus Grattond8213d02016-05-25 20:56:48 +1000260 switch( mfl )
261 {
262 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
263 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
264 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
265 return 512;
266 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
267 return 1024;
268 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
269 return 2048;
270 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
271 return 4096;
272 default:
273 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
274 }
275}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200277
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200278#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_ssl_session_free( dst );
282 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285 if( src->peer_cert != NULL )
286 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200287 int ret;
288
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200289 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200290 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200291 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200296 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299 dst->peer_cert = NULL;
300 return( ret );
301 }
302 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200304
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200305#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306 if( src->ticket != NULL )
307 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200308 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200309 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200310 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200311
312 memcpy( dst->ticket, src->ticket, src->ticket_len );
313 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200314#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200315
316 return( 0 );
317}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200318#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
321int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200322 const unsigned char *key_enc, const unsigned char *key_dec,
323 size_t keylen,
324 const unsigned char *iv_enc, const unsigned char *iv_dec,
325 size_t ivlen,
326 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200327 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
329int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
330int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
331int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
332int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
333#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000334
Paul Bakker5121ce52009-01-03 21:22:43 +0000335/*
336 * Key material generation
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200339static int ssl3_prf( const unsigned char *secret, size_t slen,
340 const char *label,
341 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000342 unsigned char *dstbuf, size_t dlen )
343{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100344 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000345 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200346 mbedtls_md5_context md5;
347 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000348 unsigned char padding[16];
349 unsigned char sha1sum[20];
350 ((void)label);
351
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200352 mbedtls_md5_init( &md5 );
353 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200354
Paul Bakker5f70b252012-09-13 14:23:06 +0000355 /*
356 * SSLv3:
357 * block =
358 * MD5( secret + SHA1( 'A' + secret + random ) ) +
359 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
360 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
361 * ...
362 */
363 for( i = 0; i < dlen / 16; i++ )
364 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200365 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000366
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100367 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100368 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100369 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100370 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100371 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100372 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100373 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100374 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100375 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100376 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000377
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100378 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100379 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100380 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100381 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100382 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100383 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100384 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100385 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000386 }
387
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200389 mbedtls_md5_free( &md5 );
390 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000391
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500392 mbedtls_platform_zeroize( padding, sizeof( padding ) );
393 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000394
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100395 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200400static int tls1_prf( const unsigned char *secret, size_t slen,
401 const char *label,
402 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000403 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000404{
Paul Bakker23986e52011-04-24 08:57:21 +0000405 size_t nb, hs;
406 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200407 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 unsigned char tmp[128];
409 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 const mbedtls_md_info_t *md_info;
411 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100412 int ret;
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419 hs = ( slen + 1 ) / 2;
420 S1 = secret;
421 S2 = secret + slen - hs;
422
423 nb = strlen( label );
424 memcpy( tmp + 20, label, nb );
425 memcpy( tmp + 20 + nb, random, rlen );
426 nb += rlen;
427
428 /*
429 * First compute P_md5(secret,label+random)[0..dlen]
430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100435 return( ret );
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
438 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
439 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441 for( i = 0; i < dlen; i += 16 )
442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_md_hmac_reset ( &md_ctx );
444 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
445 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_md_hmac_reset ( &md_ctx );
448 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
449 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
452
453 for( j = 0; j < k; j++ )
454 dstbuf[i + j] = h_i[j];
455 }
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 /*
460 * XOR out with P_sha1(secret,label+random)[0..dlen]
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466 return( ret );
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
469 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
470 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 for( i = 0; i < dlen; i += 20 )
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_hmac_reset ( &md_ctx );
475 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
476 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_hmac_reset ( &md_ctx );
479 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
480 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
483
484 for( j = 0; j < k; j++ )
485 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
486 }
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100489
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500490 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
491 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 return( 0 );
494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
498static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100499 const unsigned char *secret, size_t slen,
500 const char *label,
501 const unsigned char *random, size_t rlen,
502 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000503{
504 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100505 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000506 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
508 const mbedtls_md_info_t *md_info;
509 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100510 int ret;
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100518
519 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000521
522 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100523 memcpy( tmp + md_len, label, nb );
524 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000525 nb += rlen;
526
527 /*
528 * Compute P_<hash>(secret, label + random)[0..dlen]
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100531 return( ret );
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
534 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
535 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100536
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100537 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_md_hmac_reset ( &md_ctx );
540 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
541 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_md_hmac_reset ( &md_ctx );
544 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
545 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000546
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100547 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000548
549 for( j = 0; j < k; j++ )
550 dstbuf[i + j] = h_i[j];
551 }
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500555 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
556 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000557
558 return( 0 );
559}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100562static int tls_prf_sha256( const unsigned char *secret, size_t slen,
563 const char *label,
564 const unsigned char *random, size_t rlen,
565 unsigned char *dstbuf, size_t dlen )
566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100568 label, random, rlen, dstbuf, dlen ) );
569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200573static int tls_prf_sha384( const unsigned char *secret, size_t slen,
574 const char *label,
575 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000576 unsigned char *dstbuf, size_t dlen )
577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100579 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_SHA512_C */
582#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
587 defined(MBEDTLS_SSL_PROTO_TLS1_1)
588static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200589#endif
Paul Bakker380da532012-04-18 16:10:25 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
592static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
593static void ssl_calc_finished_ssl( 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) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
597static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
598static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200599#endif
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
602#if defined(MBEDTLS_SHA256_C)
603static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
604static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
605static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200606#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_SHA512_C)
609static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
610static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
611static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100612#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614
Hanno Becker7d0a5692018-10-23 15:26:22 +0100615#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
616 defined(MBEDTLS_USE_PSA_CRYPTO)
617static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
618{
619 if( ssl->conf->f_psk != NULL )
620 {
621 /* If we've used a callback to select the PSK,
622 * the static configuration is irrelevant. */
623 if( ssl->handshake->psk_opaque != 0 )
624 return( 1 );
625
626 return( 0 );
627 }
628
629 if( ssl->conf->psk_opaque != 0 )
630 return( 1 );
631
632 return( 0 );
633}
634#endif /* MBEDTLS_USE_PSA_CRYPTO &&
635 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200639 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000640#if defined(MBEDTLS_USE_PSA_CRYPTO)
641 int psa_fallthrough;
642#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 unsigned char keyblk[256];
645 unsigned char *key1;
646 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100647 unsigned char *mac_enc;
648 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000649 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200650 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000651 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 const mbedtls_cipher_info_t *cipher_info;
653 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100654
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000655 /* cf. RFC 5246, Section 8.1:
656 * "The master secret is always exactly 48 bytes in length." */
657 size_t const master_secret_len = 48;
658
Hanno Becker35b23c72018-10-23 12:10:41 +0100659#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
660 unsigned char session_hash[48];
661#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_ssl_session *session = ssl->session_negotiate;
664 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
665 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100670 if( cipher_info == NULL )
671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100673 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100675 }
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100678 if( md_info == NULL )
679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100681 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100683 }
684
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000686 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_SSL_PROTO_SSL3)
689 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000690 {
Paul Bakker48916f92012-09-16 19:57:18 +0000691 handshake->tls_prf = ssl3_prf;
692 handshake->calc_verify = ssl_calc_verify_ssl;
693 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695 else
696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
698 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000699 {
Paul Bakker48916f92012-09-16 19:57:18 +0000700 handshake->tls_prf = tls1_prf;
701 handshake->calc_verify = ssl_calc_verify_tls;
702 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000703 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200704 else
705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
707#if defined(MBEDTLS_SHA512_C)
708 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
709 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000710 {
Paul Bakker48916f92012-09-16 19:57:18 +0000711 handshake->tls_prf = tls_prf_sha384;
712 handshake->calc_verify = ssl_calc_verify_tls_sha384;
713 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000714 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000715 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA256_C)
718 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000719 {
Paul Bakker48916f92012-09-16 19:57:18 +0000720 handshake->tls_prf = tls_prf_sha256;
721 handshake->calc_verify = ssl_calc_verify_tls_sha256;
722 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000723 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200724 else
725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200730 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000731
732 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 * SSLv3:
734 * master =
735 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
736 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
737 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200738 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200739 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 * master = PRF( premaster, "master secret", randbytes )[0..47]
741 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100742 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
745 }
746 else
747 {
748 /* The label for the KDF used for key expansion.
749 * This is either "master secret" or "extended master secret"
750 * depending on whether the Extended Master Secret extension
751 * is used. */
752 char const *lbl = "master secret";
753
754 /* The salt for the KDF used for key expansion.
755 * - If the Extended Master Secret extension is not used,
756 * this is ClientHello.Random + ServerHello.Random
757 * (see Sect. 8.1 in RFC 5246).
758 * - If the Extended Master Secret extension is used,
759 * this is the transcript of the handshake so far.
760 * (see Sect. 4 in RFC 7627). */
761 unsigned char const *salt = handshake->randbytes;
762 size_t salt_len = 64;
763
764#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
765 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
766 ssl->transform_negotiate->ciphersuite_info;
767 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
768#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
771 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200774
Hanno Becker35b23c72018-10-23 12:10:41 +0100775 lbl = "extended master secret";
776 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200777 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
779 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100782 if( md_type == MBEDTLS_MD_SHA384 )
783 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200784 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100785#endif /* MBEDTLS_SHA512_C */
786 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200787 }
788 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100790 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200791
Hanno Becker35b23c72018-10-23 12:10:41 +0100792 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200793 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100794#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
795
Hanno Becker7d0a5692018-10-23 15:26:22 +0100796#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
797 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
798 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
799 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
800 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100801 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100802 /* Perform PSK-to-MS expansion in a single step. */
803 psa_status_t status;
804 psa_algorithm_t alg;
805 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500806 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100807
808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
809
810 psk = ssl->conf->psk_opaque;
811 if( ssl->handshake->psk_opaque != 0 )
812 psk = ssl->handshake->psk_opaque;
813
814 if( md_type == MBEDTLS_MD_SHA384 )
815 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
816 else
817 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
818
819 status = psa_key_derivation( &generator, psk, alg,
820 salt, salt_len,
821 (unsigned char const *) lbl,
822 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000823 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100824 if( status != PSA_SUCCESS )
825 {
826 psa_generator_abort( &generator );
827 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
828 }
829
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000830 status = psa_generator_read( &generator, session->master,
831 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100832 if( status != PSA_SUCCESS )
833 {
834 psa_generator_abort( &generator );
835 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
836 }
837
838 status = psa_generator_abort( &generator );
839 if( status != PSA_SUCCESS )
840 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100841 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100842 else
843#endif
844 {
845 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
846 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000847 session->master,
848 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100849 if( ret != 0 )
850 {
851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
852 return( ret );
853 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200854
Hanno Becker7d0a5692018-10-23 15:26:22 +0100855 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
856 handshake->premaster,
857 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100858
Hanno Becker7d0a5692018-10-23 15:26:22 +0100859 mbedtls_platform_zeroize( handshake->premaster,
860 sizeof(handshake->premaster) );
861 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
864 /*
865 * Swap the client and server random values.
866 */
Paul Bakker48916f92012-09-16 19:57:18 +0000867 memcpy( tmp, handshake->randbytes, 64 );
868 memcpy( handshake->randbytes, tmp + 32, 32 );
869 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500870 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
872 /*
873 * SSLv3:
874 * key block =
875 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
876 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
877 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
878 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
879 * ...
880 *
881 * TLSv1:
882 * key block = PRF( master, "key expansion", randbytes )
883 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100884 ret = handshake->tls_prf( session->master, 48, "key expansion",
885 handshake->randbytes, 64, keyblk, 256 );
886 if( ret != 0 )
887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100889 return( ret );
890 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
893 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
894 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
895 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
896 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500898 mbedtls_platform_zeroize( handshake->randbytes,
899 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
901 /*
902 * Determine the appropriate key, IV and MAC length.
903 */
Paul Bakker68884e32013-01-07 18:20:04 +0100904
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200905 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200908 cipher_info->mode == MBEDTLS_MODE_CCM ||
909 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000911 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200912
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200913 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000914 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200915
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200916 /* All modes haves 96-bit IVs;
917 * GCM and CCM has 4 implicit and 8 explicit bytes
918 * ChachaPoly has all 12 bytes implicit
919 */
Paul Bakker68884e32013-01-07 18:20:04 +0100920 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200921 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
922 transform->fixed_ivlen = 12;
923 else
924 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200925
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200926 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
927 taglen = transform->ciphersuite_info->flags &
928 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
929
930
931 /* Minimum length of encrypted record */
932 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
933 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100934 }
935 else
936 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200937 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
939 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200942 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100943 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200945 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000946 mac_key_len = mbedtls_md_get_size( md_info );
947 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200950 /*
951 * If HMAC is to be truncated, we shall keep the leftmost bytes,
952 * (rfc 6066 page 13 or rfc 2104 section 4),
953 * so we only need to adjust the length here.
954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000958
959#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
960 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000961 * HMAC implementation which also truncates the key
962 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000963 mac_key_len = transform->maclen;
964#endif
965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200967
968 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100969 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200971 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200973 transform->minlen = transform->maclen;
974 else
Paul Bakker68884e32013-01-07 18:20:04 +0100975 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200976 /*
977 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100978 * 1. if EtM is in use: one block plus MAC
979 * otherwise: * first multiple of blocklen greater than maclen
980 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
983 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100984 {
985 transform->minlen = transform->maclen
986 + cipher_info->block_size;
987 }
988 else
989#endif
990 {
991 transform->minlen = transform->maclen
992 + cipher_info->block_size
993 - transform->maclen % cipher_info->block_size;
994 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
997 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
998 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200999 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001000 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001001#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1003 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1004 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001005 {
1006 transform->minlen += transform->ivlen;
1007 }
1008 else
1009#endif
1010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001013 }
Paul Bakker68884e32013-01-07 18:20:04 +01001014 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 }
1016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001018 transform->keylen, transform->minlen, transform->ivlen,
1019 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
1021 /*
1022 * Finally setup the cipher contexts, IVs and MAC secrets.
1023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001025 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001027 key1 = keyblk + mac_key_len * 2;
1028 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
Paul Bakker68884e32013-01-07 18:20:04 +01001030 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001031 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001032
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001033 /*
1034 * This is not used in TLS v1.1.
1035 */
Paul Bakker48916f92012-09-16 19:57:18 +00001036 iv_copy_len = ( transform->fixed_ivlen ) ?
1037 transform->fixed_ivlen : transform->ivlen;
1038 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1039 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001040 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 }
1042 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#endif /* MBEDTLS_SSL_CLI_C */
1044#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001047 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1048 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Hanno Becker81c7b182017-11-09 18:39:33 +00001050 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001051 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001053 /*
1054 * This is not used in TLS v1.1.
1055 */
Paul Bakker48916f92012-09-16 19:57:18 +00001056 iv_copy_len = ( transform->fixed_ivlen ) ?
1057 transform->fixed_ivlen : transform->ivlen;
1058 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1059 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001060 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001062 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_SSL_PROTO_SSL3)
1070 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001071 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001072 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001076 }
1077
Hanno Becker81c7b182017-11-09 18:39:33 +00001078 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1079 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001080 }
1081 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1083#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1084 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1085 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001086 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001087 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1088 For AEAD-based ciphersuites, there is nothing to do here. */
1089 if( mac_key_len != 0 )
1090 {
1091 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1092 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1093 }
Paul Bakker68884e32013-01-07 18:20:04 +01001094 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001095 else
1096#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001100 }
Paul Bakker68884e32013-01-07 18:20:04 +01001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1103 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001104 {
1105 int ret = 0;
1106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001110 transform->iv_enc, transform->iv_dec,
1111 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001112 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001113 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1116 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001117 }
1118 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001120
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001121#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1122 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001123 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001124 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1125 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001126 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001127 iv_copy_len );
1128 }
1129#endif
1130
Hanno Beckerf704bef2018-11-16 15:21:18 +00001131#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001132
1133 /* Only use PSA-based ciphers for TLS-1.2.
1134 * That's relevant at least for TLS-1.0, where
1135 * we assume that mbedtls_cipher_crypt() updates
1136 * the structure field for the IV, which the PSA-based
1137 * implementation currently doesn't. */
1138#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1139 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001140 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001141 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1142 cipher_info, taglen );
1143 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1144 {
1145 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1146 return( ret );
1147 }
1148
1149 if( ret == 0 )
1150 {
1151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1152 psa_fallthrough = 0;
1153 }
1154 else
1155 {
1156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1157 psa_fallthrough = 1;
1158 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001159 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001160 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001161 psa_fallthrough = 1;
1162#else
1163 psa_fallthrough = 1;
1164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001165
Hanno Beckercb1cc802018-11-17 22:27:38 +00001166 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001167#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001168 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001169 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001172 return( ret );
1173 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001174
Hanno Beckerf704bef2018-11-16 15:21:18 +00001175#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001176 /* Only use PSA-based ciphers for TLS-1.2.
1177 * That's relevant at least for TLS-1.0, where
1178 * we assume that mbedtls_cipher_crypt() updates
1179 * the structure field for the IV, which the PSA-based
1180 * implementation currently doesn't. */
1181#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1182 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001183 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001184 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1185 cipher_info, taglen );
1186 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1187 {
1188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1189 return( ret );
1190 }
1191
1192 if( ret == 0 )
1193 {
1194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1195 psa_fallthrough = 0;
1196 }
1197 else
1198 {
1199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1200 psa_fallthrough = 1;
1201 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001202 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001203 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001204 psa_fallthrough = 1;
1205#else
1206 psa_fallthrough = 1;
1207#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001208
Hanno Beckercb1cc802018-11-17 22:27:38 +00001209 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001210#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001211 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001212 cipher_info ) ) != 0 )
1213 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001214 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001215 return( ret );
1216 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001219 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001223 return( ret );
1224 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001227 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001231 return( ret );
1232 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_CIPHER_MODE_CBC)
1235 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1238 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001241 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001242 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1245 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001248 return( ret );
1249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001253 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001256 // Initialize compression
1257 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001259 {
Paul Bakker16770332013-10-11 09:59:44 +02001260 if( ssl->compress_buf == NULL )
1261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001263 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001264 if( ssl->compress_buf == NULL )
1265 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001267 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001268 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001269 }
1270 }
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001273
Paul Bakker48916f92012-09-16 19:57:18 +00001274 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1275 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001276
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001277 if( deflateInit( &transform->ctx_deflate,
1278 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001279 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1282 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001283 }
1284 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
1289 return( 0 );
1290}
1291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_SSL_PROTO_SSL3)
1293void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001295 mbedtls_md5_context md5;
1296 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 unsigned char pad_1[48];
1298 unsigned char pad_2[48];
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001302 mbedtls_md5_init( &md5 );
1303 mbedtls_sha1_init( &sha1 );
1304
1305 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1306 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Paul Bakker380da532012-04-18 16:10:25 +00001308 memset( pad_1, 0x36, 48 );
1309 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001311 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1312 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1313 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001315 mbedtls_md5_starts_ret( &md5 );
1316 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1317 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1318 mbedtls_md5_update_ret( &md5, hash, 16 );
1319 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001321 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1322 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1323 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001324
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001325 mbedtls_sha1_starts_ret( &sha1 );
1326 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1327 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1328 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1329 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001333
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001334 mbedtls_md5_free( &md5 );
1335 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001336
Paul Bakker380da532012-04-18 16:10:25 +00001337 return;
1338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1342void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001343{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001344 mbedtls_md5_context md5;
1345 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001348
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001349 mbedtls_md5_init( &md5 );
1350 mbedtls_sha1_init( &sha1 );
1351
1352 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1353 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001354
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001355 mbedtls_md5_finish_ret( &md5, hash );
1356 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001360
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001361 mbedtls_md5_free( &md5 );
1362 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001363
Paul Bakker380da532012-04-18 16:10:25 +00001364 return;
1365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1369#if defined(MBEDTLS_SHA256_C)
1370void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001371{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001372 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001373
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001374 mbedtls_sha256_init( &sha256 );
1375
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001377
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001378 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001379 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001383
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001384 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001385
Paul Bakker380da532012-04-18 16:10:25 +00001386 return;
1387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_SHA512_C)
1391void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001392{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001393 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001394
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001395 mbedtls_sha512_init( &sha512 );
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001398
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001399 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001405 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001406
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 return;
1408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_SHA512_C */
1410#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1413int 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 +02001414{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001415 unsigned char *p = ssl->handshake->premaster;
1416 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001417 const unsigned char *psk = ssl->conf->psk;
1418 size_t psk_len = ssl->conf->psk_len;
1419
1420 /* If the psk callback was called, use its result */
1421 if( ssl->handshake->psk != NULL )
1422 {
1423 psk = ssl->handshake->psk;
1424 psk_len = ssl->handshake->psk_len;
1425 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001426
1427 /*
1428 * PMS = struct {
1429 * opaque other_secret<0..2^16-1>;
1430 * opaque psk<0..2^16-1>;
1431 * };
1432 * with "other_secret" depending on the particular key exchange
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1435 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001436 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001437 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001439
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001440 *(p++) = (unsigned char)( psk_len >> 8 );
1441 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001442
1443 if( end < p || (size_t)( end - p ) < psk_len )
1444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1445
1446 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001447 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001448 }
1449 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1451#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1452 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001453 {
1454 /*
1455 * other_secret already set by the ClientKeyExchange message,
1456 * and is 48 bytes long
1457 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001458 if( end - p < 2 )
1459 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1460
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001461 *p++ = 0;
1462 *p++ = 48;
1463 p += 48;
1464 }
1465 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1467#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1468 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001469 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001470 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001471 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001472
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001473 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001475 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001476 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001479 return( ret );
1480 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001481 *(p++) = (unsigned char)( len >> 8 );
1482 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001483 p += len;
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001486 }
1487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1489#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1490 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001491 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001492 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001493 size_t zlen;
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001496 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001497 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001500 return( ret );
1501 }
1502
1503 *(p++) = (unsigned char)( zlen >> 8 );
1504 *(p++) = (unsigned char)( zlen );
1505 p += zlen;
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001508 }
1509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001514 }
1515
1516 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001517 if( end - p < 2 )
1518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001519
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001520 *(p++) = (unsigned char)( psk_len >> 8 );
1521 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001522
1523 if( end < p || (size_t)( end - p ) < psk_len )
1524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1525
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001526 memcpy( p, psk, psk_len );
1527 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001528
1529 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1530
1531 return( 0 );
1532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001536/*
1537 * SSLv3.0 MAC functions
1538 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001539#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001540static void ssl_mac( mbedtls_md_context_t *md_ctx,
1541 const unsigned char *secret,
1542 const unsigned char *buf, size_t len,
1543 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001544 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001545{
1546 unsigned char header[11];
1547 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001548 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1550 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001551
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001552 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001554 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001555 else
Paul Bakker68884e32013-01-07 18:20:04 +01001556 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001557
1558 memcpy( header, ctr, 8 );
1559 header[ 8] = (unsigned char) type;
1560 header[ 9] = (unsigned char)( len >> 8 );
1561 header[10] = (unsigned char)( len );
1562
Paul Bakker68884e32013-01-07 18:20:04 +01001563 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 mbedtls_md_starts( md_ctx );
1565 mbedtls_md_update( md_ctx, secret, md_size );
1566 mbedtls_md_update( md_ctx, padding, padlen );
1567 mbedtls_md_update( md_ctx, header, 11 );
1568 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001569 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001570
Paul Bakker68884e32013-01-07 18:20:04 +01001571 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 mbedtls_md_starts( md_ctx );
1573 mbedtls_md_update( md_ctx, secret, md_size );
1574 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001575 mbedtls_md_update( md_ctx, out, md_size );
1576 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1581 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001582 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001583#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001584#endif
1585
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001586/* The function below is only used in the Lucky 13 counter-measure in
1587 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1588#if defined(SSL_SOME_MODES_USE_MAC) && \
1589 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1590 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1591 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1592/* This function makes sure every byte in the memory region is accessed
1593 * (in ascending addresses order) */
1594static void ssl_read_memory( unsigned char *p, size_t len )
1595{
1596 unsigned char acc = 0;
1597 volatile unsigned char force;
1598
1599 for( ; len != 0; p++, len-- )
1600 acc ^= *p;
1601
1602 force = acc;
1603 (void) force;
1604}
1605#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1606
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001607/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001613 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001617 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1620 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001621 }
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001626 ssl->out_msg, ssl->out_msglen );
1627
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001629 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001631#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 if( mode == MBEDTLS_MODE_STREAM ||
1633 ( mode == MBEDTLS_MODE_CBC
1634#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1635 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001636#endif
1637 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_PROTO_SSL3)
1640 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001641 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001642 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001643
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001644 ssl_mac( &ssl->transform_out->md_ctx_enc,
1645 ssl->transform_out->mac_enc,
1646 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001647 ssl->out_ctr, ssl->out_msgtype,
1648 mac );
1649
1650 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001651 }
1652 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1655 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1656 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001657 {
Hanno Becker992b6872017-11-09 18:57:39 +00001658 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1661 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1662 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1663 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001664 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001665 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001667
1668 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 }
1670 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001671#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001675 }
1676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 ssl->out_msg + ssl->out_msglen,
1679 ssl->transform_out->maclen );
1680
1681 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001682 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001683 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001684#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001686 /*
1687 * Encrypt
1688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1690 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001692 int ret;
1693 size_t olen = 0;
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 "including %d bytes of padding",
1697 ssl->out_msglen, 0 ) );
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001700 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001701 ssl->transform_out->ivlen,
1702 ssl->out_msg, ssl->out_msglen,
1703 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001706 return( ret );
1707 }
1708
1709 if( ssl->out_msglen != olen )
1710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001713 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 }
Paul Bakker68884e32013-01-07 18:20:04 +01001715 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001717#if defined(MBEDTLS_GCM_C) || \
1718 defined(MBEDTLS_CCM_C) || \
1719 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001721 mode == MBEDTLS_MODE_CCM ||
1722 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001723 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001724 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001725 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001726 unsigned char *enc_msg;
1727 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001728 unsigned char iv[12];
1729 mbedtls_ssl_transform *transform = ssl->transform_out;
1730 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001732 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001733
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001734 /*
1735 * Prepare additional authenticated data
1736 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001737 memcpy( add_data, ssl->out_ctr, 8 );
1738 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001740 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001741 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1742 add_data[12] = ssl->out_msglen & 0xFF;
1743
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001744 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001745
Paul Bakker68884e32013-01-07 18:20:04 +01001746 /*
1747 * Generate IV
1748 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001749 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1750 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001751 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001752 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1753 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1754 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1755
1756 }
1757 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1758 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001759 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001760 unsigned char i;
1761
1762 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1763
1764 for( i = 0; i < 8; i++ )
1765 iv[i+4] ^= ssl->out_ctr[i];
1766 }
1767 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001768 {
1769 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001772 }
1773
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001774 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1775 iv, transform->ivlen );
1776 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1777 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001778
Paul Bakker68884e32013-01-07 18:20:04 +01001779 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001780 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001781 */
1782 enc_msg = ssl->out_msg;
1783 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001784 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001787 "including 0 bytes of padding",
1788 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001789
Paul Bakker68884e32013-01-07 18:20:04 +01001790 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001791 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001792 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001793 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1794 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001795 add_data, 13,
1796 enc_msg, enc_msglen,
1797 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001798 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001801 return( ret );
1802 }
1803
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001804 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001808 }
1809
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001810 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001811 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001814 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1817#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001818 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001820 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001821 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001822 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001823 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001824
Paul Bakker48916f92012-09-16 19:57:18 +00001825 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1826 ssl->transform_out->ivlen;
1827 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 padlen = 0;
1829
1830 for( i = 0; i <= padlen; i++ )
1831 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1832
1833 ssl->out_msglen += padlen + 1;
1834
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001835 enc_msglen = ssl->out_msglen;
1836 enc_msg = ssl->out_msg;
1837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001839 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001840 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1841 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001844 {
1845 /*
1846 * Generate IV
1847 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001848 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001849 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001850 if( ret != 0 )
1851 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001852
Paul Bakker92be97b2013-01-02 17:30:03 +01001853 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001854 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001855
1856 /*
1857 * Fix pointer positions and message length with added IV
1858 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001859 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001860 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001861 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001862 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001866 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001867 ssl->out_msglen, ssl->transform_out->ivlen,
1868 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001871 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001872 ssl->transform_out->ivlen,
1873 enc_msg, enc_msglen,
1874 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001877 return( ret );
1878 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001879
Paul Bakkercca5b812013-08-31 17:40:26 +02001880 if( enc_msglen != olen )
1881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001884 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1887 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001888 {
1889 /*
1890 * Save IV in SSL3 and TLS1
1891 */
1892 memcpy( ssl->transform_out->iv_enc,
1893 ssl->transform_out->cipher_ctx_enc.iv,
1894 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001896#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001899 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001900 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001901 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1902
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001903 /*
1904 * MAC(MAC_write_key, seq_num +
1905 * TLSCipherText.type +
1906 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001907 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001908 * IV + // except for TLS 1.0
1909 * ENC(content + padding + padding_length));
1910 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001911 unsigned char pseudo_hdr[13];
1912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001914
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001915 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1916 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001917 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1918 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1923 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001924 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001925 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001927
Hanno Becker3d8c9072018-01-05 16:24:22 +00001928 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1929 ssl->transform_out->maclen );
1930
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001931 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001932 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001933 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001938 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001942 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001944 /* Make extra sure authentication was performed, exactly once */
1945 if( auth_done != 1 )
1946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1948 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001949 }
1950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
1953 return( 0 );
1954}
1955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001959 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001960#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001961 size_t padlen = 0, correct = 1;
1962#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001966 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1969 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001970 }
1971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001973
Paul Bakker48916f92012-09-16 19:57:18 +00001974 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001977 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
1980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1982 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001983 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001984 int ret;
1985 size_t olen = 0;
1986
Paul Bakker68884e32013-01-07 18:20:04 +01001987 padlen = 0;
1988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001990 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001991 ssl->transform_in->ivlen,
1992 ssl->in_msg, ssl->in_msglen,
1993 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001996 return( ret );
1997 }
1998
1999 if( ssl->in_msglen != olen )
2000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 }
Paul Bakker68884e32013-01-07 18:20:04 +01002005 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002007#if defined(MBEDTLS_GCM_C) || \
2008 defined(MBEDTLS_CCM_C) || \
2009 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002011 mode == MBEDTLS_MODE_CCM ||
2012 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002013 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002014 int ret;
2015 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002016 unsigned char *dec_msg;
2017 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002018 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002019 unsigned char iv[12];
2020 mbedtls_ssl_transform *transform = ssl->transform_in;
2021 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002023 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002024
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002025 /*
2026 * Compute and update sizes
2027 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002028 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002031 "+ taglen (%d)", ssl->in_msglen,
2032 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002034 }
2035 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2036
Paul Bakker68884e32013-01-07 18:20:04 +01002037 dec_msg = ssl->in_msg;
2038 dec_msg_result = ssl->in_msg;
2039 ssl->in_msglen = dec_msglen;
2040
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002041 /*
2042 * Prepare additional authenticated data
2043 */
Paul Bakker68884e32013-01-07 18:20:04 +01002044 memcpy( add_data, ssl->in_ctr, 8 );
2045 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002047 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002048 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2049 add_data[12] = ssl->in_msglen & 0xFF;
2050
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002051 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002052
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002053 /*
2054 * Prepare IV
2055 */
2056 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2057 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002058 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002059 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2060 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002061
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002062 }
2063 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2064 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002065 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002066 unsigned char i;
2067
2068 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2069
2070 for( i = 0; i < 8; i++ )
2071 iv[i+4] ^= ssl->in_ctr[i];
2072 }
2073 else
2074 {
2075 /* Reminder if we ever add an AEAD mode with a different size */
2076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2077 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2078 }
2079
2080 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002082
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002083 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002084 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002087 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002088 add_data, 13,
2089 dec_msg, dec_msglen,
2090 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002091 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2096 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002097
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002098 return( ret );
2099 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002100 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002101
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002102 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2105 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002106 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002107 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2110#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002111 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 {
Paul Bakker45829992013-01-03 14:52:21 +01002114 /*
2115 * Decrypt and check the padding
2116 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002117 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002118 unsigned char *dec_msg;
2119 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002120 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002121 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002122 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 /*
Paul Bakker45829992013-01-03 14:52:21 +01002125 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2128 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002129 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002130#endif
Paul Bakker45829992013-01-03 14:52:21 +01002131
2132 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2133 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002136 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2137 ssl->transform_in->ivlen,
2138 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002140 }
2141
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002142 dec_msglen = ssl->in_msglen;
2143 dec_msg = ssl->in_msg;
2144 dec_msg_result = ssl->in_msg;
2145
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002146 /*
2147 * Authenticate before decrypt if enabled
2148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2150 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002151 {
Hanno Becker992b6872017-11-09 18:57:39 +00002152 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002153 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002156
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002157 dec_msglen -= ssl->transform_in->maclen;
2158 ssl->in_msglen -= ssl->transform_in->maclen;
2159
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002160 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2161 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2162 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2163 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2168 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002169 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002170 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002174 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002175 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002176 ssl->transform_in->maclen );
2177
Hanno Becker992b6872017-11-09 18:57:39 +00002178 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2179 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002184 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002185 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002188
2189 /*
2190 * Check length sanity
2191 */
2192 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002195 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002197 }
2198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002200 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002201 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002204 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002205 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002206 dec_msglen -= ssl->transform_in->ivlen;
2207 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002208
Paul Bakker48916f92012-09-16 19:57:18 +00002209 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002210 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002211 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002215 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002216 ssl->transform_in->ivlen,
2217 dec_msg, dec_msglen,
2218 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002221 return( ret );
2222 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002223
Paul Bakkercca5b812013-08-31 17:40:26 +02002224 if( dec_msglen != olen )
2225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002228 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2231 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002232 {
2233 /*
2234 * Save IV in SSL3 and TLS1
2235 */
2236 memcpy( ssl->transform_in->iv_dec,
2237 ssl->transform_in->cipher_ctx_dec.iv,
2238 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002240#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
2242 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002243
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002244 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002245 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247#if defined(MBEDTLS_SSL_DEBUG_ALL)
2248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002249 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002250#endif
Paul Bakker45829992013-01-03 14:52:21 +01002251 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002252 correct = 0;
2253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#if defined(MBEDTLS_SSL_PROTO_SSL3)
2256 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
Paul Bakker48916f92012-09-16 19:57:18 +00002258 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260#if defined(MBEDTLS_SSL_DEBUG_ALL)
2261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002263 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002264#endif
Paul Bakker45829992013-01-03 14:52:21 +01002265 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 }
2267 }
2268 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2270#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2271 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2272 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
2274 /*
Paul Bakker45829992013-01-03 14:52:21 +01002275 * TLSv1+: always check the padding up to the first failure
2276 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002278 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002279 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002280 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002281
Paul Bakker956c9e02013-12-19 14:42:28 +01002282 /*
2283 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002284 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002285 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002286 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002287 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002288 *
2289 * In both cases we reset padding_idx to a safe value (0) to
2290 * prevent out-of-buffer reads.
2291 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002292 correct &= ( padlen <= ssl->in_msglen );
2293 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002294 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002295
2296 padding_idx *= correct;
2297
Angus Grattonb512bc12018-06-19 15:57:50 +10002298 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002299 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002300 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002301 pad_count += real_count *
2302 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2303 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002304
2305 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002308 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002310#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002311 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002313 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2315 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002319 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002320
2321 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002323 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002325 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002331#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002334#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
2336 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002337 * Authenticate if not done yet.
2338 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002340#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002341 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002342 {
Hanno Becker992b6872017-11-09 18:57:39 +00002343 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002344
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002345 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002346
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002347 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2348 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350#if defined(MBEDTLS_SSL_PROTO_SSL3)
2351 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002352 {
2353 ssl_mac( &ssl->transform_in->md_ctx_dec,
2354 ssl->transform_in->mac_dec,
2355 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002356 ssl->in_ctr, ssl->in_msgtype,
2357 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002358 }
2359 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2361#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2362 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2363 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002364 {
2365 /*
2366 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002367 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002368 *
2369 * Known timing attacks:
2370 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2371 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002372 * To compensate for different timings for the MAC calculation
2373 * depending on how much padding was removed (which is determined
2374 * by padlen), process extra_run more blocks through the hash
2375 * function.
2376 *
2377 * The formula in the paper is
2378 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2379 * where L1 is the size of the header plus the decrypted message
2380 * plus CBC padding and L2 is the size of the header plus the
2381 * decrypted message. This is for an underlying hash function
2382 * with 64-byte blocks.
2383 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2384 * correctly. We round down instead of up, so -56 is the correct
2385 * value for our calculations instead of -55.
2386 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002387 * Repeat the formula rather than defining a block_size variable.
2388 * This avoids requiring division by a variable at runtime
2389 * (which would be marginally less efficient and would require
2390 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002391 */
2392 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002393
2394 /*
2395 * The next two sizes are the minimum and maximum values of
2396 * in_msglen over all padlen values.
2397 *
2398 * They're independent of padlen, since we previously did
2399 * in_msglen -= padlen.
2400 *
2401 * Note that max_len + maclen is never more than the buffer
2402 * length, as we previously did in_msglen -= maclen too.
2403 */
2404 const size_t max_len = ssl->in_msglen + padlen;
2405 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2406
Gilles Peskine20b44082018-05-29 14:06:49 +02002407 switch( ssl->transform_in->ciphersuite_info->mac )
2408 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002409#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2410 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002411 case MBEDTLS_MD_MD5:
2412 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002413 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002414 /* 8 bytes of message size, 64-byte compression blocks */
2415 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2416 ( 13 + ssl->in_msglen + 8 ) / 64;
2417 break;
2418#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002419#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002420 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002421 /* 16 bytes of message size, 128-byte compression blocks */
2422 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2423 ( 13 + ssl->in_msglen + 16 ) / 128;
2424 break;
2425#endif
2426 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2429 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002430
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002431 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2434 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2435 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2436 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002437 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002438 /* Make sure we access everything even when padlen > 0. This
2439 * makes the synchronisation requirements for just-in-time
2440 * Prime+Probe attacks much tighter and hopefully impractical. */
2441 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002442 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002443
2444 /* Call mbedtls_md_process at least once due to cache attacks
2445 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002446 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002450
2451 /* Make sure we access all the memory that could contain the MAC,
2452 * before we check it in the next code block. This makes the
2453 * synchronisation requirements for just-in-time Prime+Probe
2454 * attacks much tighter and hopefully impractical. */
2455 ssl_read_memory( ssl->in_msg + min_len,
2456 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002457 }
2458 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2460 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002466#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002467 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2468 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2469 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Hanno Becker992b6872017-11-09 18:57:39 +00002472 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2473 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475#if defined(MBEDTLS_SSL_DEBUG_ALL)
2476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002477#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002478 correct = 0;
2479 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002480 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002481 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002482
2483 /*
2484 * Finally check the correct flag
2485 */
2486 if( correct == 0 )
2487 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002488#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002489
2490 /* Make extra sure authentication was performed, exactly once */
2491 if( auth_done != 1 )
2492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 if( ssl->in_msglen == 0 )
2498 {
Angus Gratton34817922018-06-19 15:58:22 +10002499#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2500 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2501 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2502 {
2503 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2505 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2506 }
2507#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2508
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 ssl->nb_zero++;
2510
2511 /*
2512 * Three or more empty messages may be a DoS attack
2513 * (excessive CPU consumption).
2514 */
2515 if( ssl->nb_zero > 3 )
2516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 }
2521 }
2522 else
2523 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002526 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002527 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002528 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002529 }
2530 else
2531#endif
2532 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002533 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002534 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2535 if( ++ssl->in_ctr[i - 1] != 0 )
2536 break;
2537
2538 /* The loop goes to its end iff the counter is wrapping */
2539 if( i == ssl_ep_len( ssl ) )
2540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2542 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002543 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002544 }
2545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
2548 return( 0 );
2549}
2550
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002551#undef MAC_NONE
2552#undef MAC_PLAINTEXT
2553#undef MAC_CIPHERTEXT
2554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002556/*
2557 * Compression/decompression functions
2558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002560{
2561 int ret;
2562 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002563 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002564 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002565 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002568
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002569 if( len_pre == 0 )
2570 return( 0 );
2571
Paul Bakker2770fbd2012-07-03 13:30:23 +00002572 memcpy( msg_pre, ssl->out_msg, len_pre );
2573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002575 ssl->out_msglen ) );
2576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002578 ssl->out_msg, ssl->out_msglen );
2579
Paul Bakker48916f92012-09-16 19:57:18 +00002580 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2581 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2582 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002583 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002584
Paul Bakker48916f92012-09-16 19:57:18 +00002585 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002586 if( ret != Z_OK )
2587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2589 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002590 }
2591
Angus Grattond8213d02016-05-25 20:56:48 +10002592 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002593 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002596 ssl->out_msglen ) );
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002599 ssl->out_msg, ssl->out_msglen );
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002602
2603 return( 0 );
2604}
2605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002607{
2608 int ret;
2609 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002610 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002611 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002612 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002615
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002616 if( len_pre == 0 )
2617 return( 0 );
2618
Paul Bakker2770fbd2012-07-03 13:30:23 +00002619 memcpy( msg_pre, ssl->in_msg, len_pre );
2620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002622 ssl->in_msglen ) );
2623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002625 ssl->in_msg, ssl->in_msglen );
2626
Paul Bakker48916f92012-09-16 19:57:18 +00002627 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2628 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2629 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002630 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002631 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002632
Paul Bakker48916f92012-09-16 19:57:18 +00002633 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002634 if( ret != Z_OK )
2635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2637 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002638 }
2639
Angus Grattond8213d02016-05-25 20:56:48 +10002640 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002641 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002644 ssl->in_msglen ) );
2645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002647 ssl->in_msg, ssl->in_msglen );
2648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002650
2651 return( 0 );
2652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2656static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#if defined(MBEDTLS_SSL_PROTO_DTLS)
2659static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002660{
2661 /* If renegotiation is not enforced, retransmit until we would reach max
2662 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002663 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002664 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002665 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002666 unsigned char doublings = 1;
2667
2668 while( ratio != 0 )
2669 {
2670 ++doublings;
2671 ratio >>= 1;
2672 }
2673
2674 if( ++ssl->renego_records_seen > doublings )
2675 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002677 return( 0 );
2678 }
2679 }
2680
2681 return( ssl_write_hello_request( ssl ) );
2682}
2683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002685
Paul Bakker5121ce52009-01-03 21:22:43 +00002686/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002687 * Fill the input message buffer by appending data to it.
2688 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002689 *
2690 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2691 * available (from this read and/or a previous one). Otherwise, an error code
2692 * is returned (possibly EOF or WANT_READ).
2693 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002694 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2695 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2696 * since we always read a whole datagram at once.
2697 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002698 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002699 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002702{
Paul Bakker23986e52011-04-24 08:57:21 +00002703 int ret;
2704 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002708 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002711 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002713 }
2714
Angus Grattond8213d02016-05-25 20:56:48 +10002715 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2718 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002719 }
2720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002722 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002724 uint32_t timeout;
2725
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002726 /* Just to be sure */
2727 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2728 {
2729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2730 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2731 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2732 }
2733
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002734 /*
2735 * The point is, we need to always read a full datagram at once, so we
2736 * sometimes read more then requested, and handle the additional data.
2737 * It could be the rest of the current record (while fetching the
2738 * header) and/or some other records in the same datagram.
2739 */
2740
2741 /*
2742 * Move to the next record in the already read datagram if applicable
2743 */
2744 if( ssl->next_record_offset != 0 )
2745 {
2746 if( ssl->in_left < ssl->next_record_offset )
2747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2749 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002750 }
2751
2752 ssl->in_left -= ssl->next_record_offset;
2753
2754 if( ssl->in_left != 0 )
2755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002757 ssl->next_record_offset ) );
2758 memmove( ssl->in_hdr,
2759 ssl->in_hdr + ssl->next_record_offset,
2760 ssl->in_left );
2761 }
2762
2763 ssl->next_record_offset = 0;
2764 }
2765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002768
2769 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002770 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002771 */
2772 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002775 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002776 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002777
2778 /*
2779 * A record can't be split accross datagrams. If we need to read but
2780 * are not at the beginning of a new record, the caller did something
2781 * wrong.
2782 */
2783 if( ssl->in_left != 0 )
2784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002787 }
2788
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002789 /*
2790 * Don't even try to read if time's out already.
2791 * This avoids by-passing the timer when repeatedly receiving messages
2792 * that will end up being dropped.
2793 */
2794 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002795 {
2796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002797 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002798 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002799 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002800 {
Angus Grattond8213d02016-05-25 20:56:48 +10002801 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002804 timeout = ssl->handshake->retransmit_timeout;
2805 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002806 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002809
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002810 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002811 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2812 timeout );
2813 else
2814 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002817
2818 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002820 }
2821
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002822 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002825 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002828 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002829 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002832 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002833 }
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002838 return( ret );
2839 }
2840
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002841 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002842 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002844 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002846 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002847 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002850 return( ret );
2851 }
2852
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002853 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002856 }
2857
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 if( ret < 0 )
2859 return( ret );
2860
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002861 ssl->in_left = ret;
2862 }
2863 else
2864#endif
2865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002867 ssl->in_left, nb_want ) );
2868
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002869 while( ssl->in_left < nb_want )
2870 {
2871 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002872
2873 if( ssl_check_timer( ssl ) != 0 )
2874 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2875 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002876 {
2877 if( ssl->f_recv_timeout != NULL )
2878 {
2879 ret = ssl->f_recv_timeout( ssl->p_bio,
2880 ssl->in_hdr + ssl->in_left, len,
2881 ssl->conf->read_timeout );
2882 }
2883 else
2884 {
2885 ret = ssl->f_recv( ssl->p_bio,
2886 ssl->in_hdr + ssl->in_left, len );
2887 }
2888 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002891 ssl->in_left, nb_want ) );
2892 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002893
2894 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002896
2897 if( ret < 0 )
2898 return( ret );
2899
mohammad160352aecb92018-03-28 23:41:40 -07002900 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002901 {
Darryl Green11999bb2018-03-13 15:22:58 +00002902 MBEDTLS_SSL_DEBUG_MSG( 1,
2903 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002904 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2906 }
2907
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002908 ssl->in_left += ret;
2909 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 }
2911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
2914 return( 0 );
2915}
2916
2917/*
2918 * Flush any data not yet written
2919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002921{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002922 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002923 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002927 if( ssl->f_send == NULL )
2928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002930 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002932 }
2933
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002934 /* Avoid incrementing counter if data is flushed */
2935 if( ssl->out_left == 0 )
2936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002938 return( 0 );
2939 }
2940
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 while( ssl->out_left > 0 )
2942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2944 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Hanno Becker2b1e3542018-08-06 11:19:13 +01002946 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002947 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
2951 if( ret <= 0 )
2952 return( ret );
2953
mohammad160352aecb92018-03-28 23:41:40 -07002954 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002955 {
Darryl Green11999bb2018-03-13 15:22:58 +00002956 MBEDTLS_SSL_DEBUG_MSG( 1,
2957 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002958 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2960 }
2961
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 ssl->out_left -= ret;
2963 }
2964
Hanno Becker2b1e3542018-08-06 11:19:13 +01002965#if defined(MBEDTLS_SSL_PROTO_DTLS)
2966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002967 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002968 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002969 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002970 else
2971#endif
2972 {
2973 ssl->out_hdr = ssl->out_buf + 8;
2974 }
2975 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
2979 return( 0 );
2980}
2981
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002982/*
2983 * Functions to handle the DTLS retransmission state machine
2984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002986/*
2987 * Append current handshake message to current outgoing flight
2988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2993 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2994 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002995
2996 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002997 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002998 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003001 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003002 }
3003
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003004 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003005 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003008 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003009 }
3010
3011 /* Copy current handshake message with headers */
3012 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3013 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003014 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003015 msg->next = NULL;
3016
3017 /* Append to the current flight */
3018 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003019 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003020 else
3021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003023 while( cur->next != NULL )
3024 cur = cur->next;
3025 cur->next = msg;
3026 }
3027
Hanno Becker3b235902018-08-06 09:54:53 +01003028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003029 return( 0 );
3030}
3031
3032/*
3033 * Free the current flight of handshake messages
3034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 mbedtls_ssl_flight_item *cur = flight;
3038 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003039
3040 while( cur != NULL )
3041 {
3042 next = cur->next;
3043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 mbedtls_free( cur->p );
3045 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003046
3047 cur = next;
3048 }
3049}
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3052static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003053#endif
3054
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003055/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003056 * Swap transform_out and out_ctr with the alternative ones
3057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003061 unsigned char tmp_out_ctr[8];
3062
3063 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003066 return;
3067 }
3068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003070
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003071 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003072 tmp_transform = ssl->transform_out;
3073 ssl->transform_out = ssl->handshake->alt_transform_out;
3074 ssl->handshake->alt_transform_out = tmp_transform;
3075
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003076 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003077 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3078 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003079 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003080
3081 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003082 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3085 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003091 }
3092 }
3093#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003094}
3095
3096/*
3097 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003098 */
3099int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3100{
3101 int ret = 0;
3102
3103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3104
3105 ret = mbedtls_ssl_flight_transmit( ssl );
3106
3107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3108
3109 return( ret );
3110}
3111
3112/*
3113 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003114 *
3115 * Need to remember the current message in case flush_output returns
3116 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003117 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003118 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003119int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003120{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003121 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003125 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003127
3128 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003129 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003130 ssl_swap_epochs( ssl );
3131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003133 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003134
3135 while( ssl->handshake->cur_msg != NULL )
3136 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003137 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003138 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003139
Hanno Beckere1dcb032018-08-17 16:47:58 +01003140 int const is_finished =
3141 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3142 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3143
Hanno Becker04da1892018-08-14 13:22:10 +01003144 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3145 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3146
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003147 /* Swap epochs before sending Finished: we can't do it after
3148 * sending ChangeCipherSpec, in case write returns WANT_READ.
3149 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003150 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003151 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003153 ssl_swap_epochs( ssl );
3154 }
3155
Hanno Becker67bc7c32018-08-06 11:33:50 +01003156 ret = ssl_get_remaining_payload_in_datagram( ssl );
3157 if( ret < 0 )
3158 return( ret );
3159 max_frag_len = (size_t) ret;
3160
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003161 /* CCS is copied as is, while HS messages may need fragmentation */
3162 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3163 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003164 if( max_frag_len == 0 )
3165 {
3166 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3167 return( ret );
3168
3169 continue;
3170 }
3171
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003172 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003173 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003174 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003175
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003176 /* Update position inside current message */
3177 ssl->handshake->cur_msg_p += cur->len;
3178 }
3179 else
3180 {
3181 const unsigned char * const p = ssl->handshake->cur_msg_p;
3182 const size_t hs_len = cur->len - 12;
3183 const size_t frag_off = p - ( cur->p + 12 );
3184 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003185 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003186
Hanno Beckere1dcb032018-08-17 16:47:58 +01003187 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003188 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003189 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003190 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003191
Hanno Becker67bc7c32018-08-06 11:33:50 +01003192 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3193 return( ret );
3194
3195 continue;
3196 }
3197 max_hs_frag_len = max_frag_len - 12;
3198
3199 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3200 max_hs_frag_len : rem_len;
3201
3202 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003203 {
3204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003205 (unsigned) cur_hs_frag_len,
3206 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003207 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003208
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003209 /* Messages are stored with handshake headers as if not fragmented,
3210 * copy beginning of headers then fill fragmentation fields.
3211 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3212 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003213
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003214 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3215 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3216 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3217
Hanno Becker67bc7c32018-08-06 11:33:50 +01003218 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3219 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3220 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003221
3222 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3223
Hanno Becker3f7b9732018-08-28 09:53:25 +01003224 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003225 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3226 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003227 ssl->out_msgtype = cur->type;
3228
3229 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003230 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003231 }
3232
3233 /* If done with the current message move to the next one if any */
3234 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3235 {
3236 if( cur->next != NULL )
3237 {
3238 ssl->handshake->cur_msg = cur->next;
3239 ssl->handshake->cur_msg_p = cur->next->p + 12;
3240 }
3241 else
3242 {
3243 ssl->handshake->cur_msg = NULL;
3244 ssl->handshake->cur_msg_p = NULL;
3245 }
3246 }
3247
3248 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003249 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252 return( ret );
3253 }
3254 }
3255
Hanno Becker67bc7c32018-08-06 11:33:50 +01003256 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3257 return( ret );
3258
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003259 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3261 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003262 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003265 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3266 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003267
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003269
3270 return( 0 );
3271}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003272
3273/*
3274 * To be called when the last message of an incoming flight is received.
3275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003277{
3278 /* We won't need to resend that one any more */
3279 ssl_flight_free( ssl->handshake->flight );
3280 ssl->handshake->flight = NULL;
3281 ssl->handshake->cur_msg = NULL;
3282
3283 /* The next incoming flight will start with this msg_seq */
3284 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3285
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003286 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003287 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003288
Hanno Becker0271f962018-08-16 13:23:47 +01003289 /* Clear future message buffering structure. */
3290 ssl_buffering_free( ssl );
3291
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003292 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003293 ssl_set_timer( ssl, 0 );
3294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3296 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003299 }
3300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003302}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003303
3304/*
3305 * To be called when the last message of an outgoing flight is send.
3306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003308{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003309 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003310 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3313 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003316 }
3317 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321
Paul Bakker5121ce52009-01-03 21:22:43 +00003322/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003323 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325
3326/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003327 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003328 *
3329 * - fill in handshake headers
3330 * - update handshake checksum
3331 * - DTLS: save message for resending
3332 * - then pass to the record layer
3333 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003334 * DTLS: except for HelloRequest, messages are only queued, and will only be
3335 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003336 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003337 * Inputs:
3338 * - ssl->out_msglen: 4 + actual handshake message len
3339 * (4 is the size of handshake headers for TLS)
3340 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3341 * - ssl->out_msg + 4: the handshake message body
3342 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003343 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003344 * - ssl->out_msglen: the length of the record contents
3345 * (including handshake headers but excluding record headers)
3346 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003347 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003348int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003349{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003350 int ret;
3351 const size_t hs_len = ssl->out_msglen - 4;
3352 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003353
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3355
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003356 /*
3357 * Sanity checks
3358 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003359 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003360 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3361 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003362 /* In SSLv3, the client might send a NoCertificate alert. */
3363#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3364 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3365 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3366 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3367#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3368 {
3369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3370 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3371 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003374 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3375 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3376 ssl->handshake == NULL )
3377 {
3378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3379 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003383 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003386 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3388 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003389 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003390#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003391
Hanno Beckerb50a2532018-08-06 11:52:54 +01003392 /* Double-check that we did not exceed the bounds
3393 * of the outgoing record buffer.
3394 * This should never fail as the various message
3395 * writing functions must obey the bounds of the
3396 * outgoing record buffer, but better be safe.
3397 *
3398 * Note: We deliberately do not check for the MTU or MFL here.
3399 */
3400 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3401 {
3402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3403 "size %u, maximum %u",
3404 (unsigned) ssl->out_msglen,
3405 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3406 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3407 }
3408
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003409 /*
3410 * Fill handshake headers
3411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003414 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3415 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3416 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003417
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003418 /*
3419 * DTLS has additional fields in the Handshake layer,
3420 * between the length field and the actual payload:
3421 * uint16 message_seq;
3422 * uint24 fragment_offset;
3423 * uint24 fragment_length;
3424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003426 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003427 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003428 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003429 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003430 {
3431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3432 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003433 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003434 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3436 }
3437
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003438 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003439 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003440
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003441 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003442 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003443 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003444 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3445 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3446 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003447 }
3448 else
3449 {
3450 ssl->out_msg[4] = 0;
3451 ssl->out_msg[5] = 0;
3452 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003453
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003454 /* Handshake hashes are computed without fragmentation,
3455 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003456 memset( ssl->out_msg + 6, 0x00, 3 );
3457 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003458 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003460
Hanno Becker0207e532018-08-28 10:28:28 +01003461 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003462 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3463 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 }
3465
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003466 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003468 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003469 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3470 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003471 {
3472 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003475 return( ret );
3476 }
3477 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003478 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003479#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003480 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003481 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003482 {
3483 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3484 return( ret );
3485 }
3486 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003487
3488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3489
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003490 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003491}
3492
3493/*
3494 * Record layer functions
3495 */
3496
3497/*
3498 * Write current record.
3499 *
3500 * Uses:
3501 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3502 * - ssl->out_msglen: length of the record content (excl headers)
3503 * - ssl->out_msg: record content
3504 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003506{
3507 int ret, done = 0;
3508 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003509 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003510
3511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003514 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003516 {
3517 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520 return( ret );
3521 }
3522
3523 len = ssl->out_msglen;
3524 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3528 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 ret = mbedtls_ssl_hw_record_write( ssl );
3533 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3536 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003537 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003538
3539 if( ret == 0 )
3540 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003543 if( !done )
3544 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003545 unsigned i;
3546 size_t protected_record_size;
3547
Paul Bakker05ef8352012-05-08 09:17:57 +00003548 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003550 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003551
Hanno Becker19859472018-08-06 09:40:20 +01003552 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003553 ssl->out_len[0] = (unsigned char)( len >> 8 );
3554 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003555
Paul Bakker48916f92012-09-16 19:57:18 +00003556 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003557 {
3558 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003561 return( ret );
3562 }
3563
3564 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003565 ssl->out_len[0] = (unsigned char)( len >> 8 );
3566 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003567 }
3568
Hanno Becker2b1e3542018-08-06 11:19:13 +01003569 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3570
3571#if defined(MBEDTLS_SSL_PROTO_DTLS)
3572 /* In case of DTLS, double-check that we don't exceed
3573 * the remaining space in the datagram. */
3574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3575 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003576 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003577 if( ret < 0 )
3578 return( ret );
3579
3580 if( protected_record_size > (size_t) ret )
3581 {
3582 /* Should never happen */
3583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3584 }
3585 }
3586#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003589 "version = [%d:%d], msglen = %d",
3590 ssl->out_hdr[0], ssl->out_hdr[1],
3591 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003594 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003595
3596 ssl->out_left += protected_record_size;
3597 ssl->out_hdr += protected_record_size;
3598 ssl_update_out_pointers( ssl, ssl->transform_out );
3599
Hanno Becker04484622018-08-06 09:49:38 +01003600 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3601 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3602 break;
3603
3604 /* The loop goes to its end iff the counter is wrapping */
3605 if( i == ssl_ep_len( ssl ) )
3606 {
3607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3608 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3609 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 }
3611
Hanno Becker67bc7c32018-08-06 11:33:50 +01003612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3614 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003615 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003616 size_t remaining;
3617 ret = ssl_get_remaining_payload_in_datagram( ssl );
3618 if( ret < 0 )
3619 {
3620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3621 ret );
3622 return( ret );
3623 }
3624
3625 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003626 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003627 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003628 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003629 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003630 else
3631 {
Hanno Becker513815a2018-08-20 11:56:09 +01003632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003633 }
3634 }
3635#endif /* MBEDTLS_SSL_PROTO_DTLS */
3636
3637 if( ( flush == SSL_FORCE_FLUSH ) &&
3638 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 return( ret );
3642 }
3643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
3646 return( 0 );
3647}
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003650
3651static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3652{
3653 if( ssl->in_msglen < ssl->in_hslen ||
3654 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3655 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3656 {
3657 return( 1 );
3658 }
3659 return( 0 );
3660}
Hanno Becker44650b72018-08-16 12:51:11 +01003661
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003662static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003663{
3664 return( ( ssl->in_msg[9] << 16 ) |
3665 ( ssl->in_msg[10] << 8 ) |
3666 ssl->in_msg[11] );
3667}
3668
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003669static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003670{
3671 return( ( ssl->in_msg[6] << 16 ) |
3672 ( ssl->in_msg[7] << 8 ) |
3673 ssl->in_msg[8] );
3674}
3675
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003676static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003677{
3678 uint32_t msg_len, frag_off, frag_len;
3679
3680 msg_len = ssl_get_hs_total_len( ssl );
3681 frag_off = ssl_get_hs_frag_off( ssl );
3682 frag_len = ssl_get_hs_frag_len( ssl );
3683
3684 if( frag_off > msg_len )
3685 return( -1 );
3686
3687 if( frag_len > msg_len - frag_off )
3688 return( -1 );
3689
3690 if( frag_len + 12 > ssl->in_msglen )
3691 return( -1 );
3692
3693 return( 0 );
3694}
3695
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003696/*
3697 * Mark bits in bitmask (used for DTLS HS reassembly)
3698 */
3699static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3700{
3701 unsigned int start_bits, end_bits;
3702
3703 start_bits = 8 - ( offset % 8 );
3704 if( start_bits != 8 )
3705 {
3706 size_t first_byte_idx = offset / 8;
3707
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003708 /* Special case */
3709 if( len <= start_bits )
3710 {
3711 for( ; len != 0; len-- )
3712 mask[first_byte_idx] |= 1 << ( start_bits - len );
3713
3714 /* Avoid potential issues with offset or len becoming invalid */
3715 return;
3716 }
3717
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003718 offset += start_bits; /* Now offset % 8 == 0 */
3719 len -= start_bits;
3720
3721 for( ; start_bits != 0; start_bits-- )
3722 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3723 }
3724
3725 end_bits = len % 8;
3726 if( end_bits != 0 )
3727 {
3728 size_t last_byte_idx = ( offset + len ) / 8;
3729
3730 len -= end_bits; /* Now len % 8 == 0 */
3731
3732 for( ; end_bits != 0; end_bits-- )
3733 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3734 }
3735
3736 memset( mask + offset / 8, 0xFF, len / 8 );
3737}
3738
3739/*
3740 * Check that bitmask is full
3741 */
3742static int ssl_bitmask_check( unsigned char *mask, size_t len )
3743{
3744 size_t i;
3745
3746 for( i = 0; i < len / 8; i++ )
3747 if( mask[i] != 0xFF )
3748 return( -1 );
3749
3750 for( i = 0; i < len % 8; i++ )
3751 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3752 return( -1 );
3753
3754 return( 0 );
3755}
3756
Hanno Becker56e205e2018-08-16 09:06:12 +01003757/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003758static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003759 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003760{
Hanno Becker56e205e2018-08-16 09:06:12 +01003761 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003762
Hanno Becker56e205e2018-08-16 09:06:12 +01003763 alloc_len = 12; /* Handshake header */
3764 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003765
Hanno Beckerd07df862018-08-16 09:14:58 +01003766 if( add_bitmap )
3767 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003768
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003769 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003770}
Hanno Becker56e205e2018-08-16 09:06:12 +01003771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003773
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003774static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003775{
3776 return( ( ssl->in_msg[1] << 16 ) |
3777 ( ssl->in_msg[2] << 8 ) |
3778 ssl->in_msg[3] );
3779}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003780
Simon Butcher99000142016-10-13 17:21:01 +01003781int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003782{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003786 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003788 }
3789
Hanno Becker12555c62018-08-16 12:47:53 +01003790 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003793 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003794 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003798 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003799 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003800 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003801
Hanno Becker44650b72018-08-16 12:51:11 +01003802 if( ssl_check_hs_header( ssl ) != 0 )
3803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3805 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3806 }
3807
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003808 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003809 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3810 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3811 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3812 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003813 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003814 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3815 {
3816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3817 recv_msg_seq,
3818 ssl->handshake->in_msg_seq ) );
3819 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3820 }
3821
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003822 /* Retransmit only on last message from previous flight, to avoid
3823 * too many retransmissions.
3824 * Besides, No sane server ever retransmits HelloVerifyRequest */
3825 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003829 "message_seq = %d, start_of_flight = %d",
3830 recv_msg_seq,
3831 ssl->handshake->in_flight_start_seq ) );
3832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003836 return( ret );
3837 }
3838 }
3839 else
3840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003842 "message_seq = %d, expected = %d",
3843 recv_msg_seq,
3844 ssl->handshake->in_msg_seq ) );
3845 }
3846
Hanno Becker90333da2017-10-10 11:27:13 +01003847 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003848 }
3849 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003850
Hanno Becker6d97ef52018-08-16 13:09:04 +01003851 /* Message reassembly is handled alongside buffering of future
3852 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003853 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003854 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003855 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003858 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003859 }
3860 }
3861 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003863 /* With TLS we don't handle fragmentation (for now) */
3864 if( ssl->in_msglen < ssl->in_hslen )
3865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3867 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003868 }
3869
Simon Butcher99000142016-10-13 17:21:01 +01003870 return( 0 );
3871}
3872
3873void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3874{
Hanno Becker0271f962018-08-16 13:23:47 +01003875 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003876
Hanno Becker0271f962018-08-16 13:23:47 +01003877 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003878 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003879 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003880 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003881
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003882 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003885 ssl->handshake != NULL )
3886 {
Hanno Becker0271f962018-08-16 13:23:47 +01003887 unsigned offset;
3888 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003889
Hanno Becker0271f962018-08-16 13:23:47 +01003890 /* Increment handshake sequence number */
3891 hs->in_msg_seq++;
3892
3893 /*
3894 * Clear up handshake buffering and reassembly structure.
3895 */
3896
3897 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003898 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003899
3900 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003901 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3902 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003903 offset++, hs_buf++ )
3904 {
3905 *hs_buf = *(hs_buf + 1);
3906 }
3907
3908 /* Create a fresh last entry */
3909 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003910 }
3911#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003912}
3913
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003914/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003915 * DTLS anti-replay: RFC 6347 4.1.2.6
3916 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003917 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3918 * Bit n is set iff record number in_window_top - n has been seen.
3919 *
3920 * Usually, in_window_top is the last record number seen and the lsb of
3921 * in_window is set. The only exception is the initial state (record number 0
3922 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3925static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003926{
3927 ssl->in_window_top = 0;
3928 ssl->in_window = 0;
3929}
3930
3931static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3932{
3933 return( ( (uint64_t) buf[0] << 40 ) |
3934 ( (uint64_t) buf[1] << 32 ) |
3935 ( (uint64_t) buf[2] << 24 ) |
3936 ( (uint64_t) buf[3] << 16 ) |
3937 ( (uint64_t) buf[4] << 8 ) |
3938 ( (uint64_t) buf[5] ) );
3939}
3940
3941/*
3942 * Return 0 if sequence number is acceptable, -1 otherwise
3943 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003945{
3946 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3947 uint64_t bit;
3948
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003949 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003950 return( 0 );
3951
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003952 if( rec_seqnum > ssl->in_window_top )
3953 return( 0 );
3954
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003955 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003956
3957 if( bit >= 64 )
3958 return( -1 );
3959
3960 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3961 return( -1 );
3962
3963 return( 0 );
3964}
3965
3966/*
3967 * Update replay window on new validated record
3968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003970{
3971 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3972
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003973 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003974 return;
3975
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003976 if( rec_seqnum > ssl->in_window_top )
3977 {
3978 /* Update window_top and the contents of the window */
3979 uint64_t shift = rec_seqnum - ssl->in_window_top;
3980
3981 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003982 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003983 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003984 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003985 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003986 ssl->in_window |= 1;
3987 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003988
3989 ssl->in_window_top = rec_seqnum;
3990 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003991 else
3992 {
3993 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003994 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003995
3996 if( bit < 64 ) /* Always true, but be extra sure */
3997 ssl->in_window |= (uint64_t) 1 << bit;
3998 }
3999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004001
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004002#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004003/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004004static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4005
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004006/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004007 * Without any SSL context, check if a datagram looks like a ClientHello with
4008 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004009 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004010 *
4011 * - if cookie is valid, return 0
4012 * - if ClientHello looks superficially valid but cookie is not,
4013 * fill obuf and set olen, then
4014 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4015 * - otherwise return a specific error code
4016 */
4017static int ssl_check_dtls_clihlo_cookie(
4018 mbedtls_ssl_cookie_write_t *f_cookie_write,
4019 mbedtls_ssl_cookie_check_t *f_cookie_check,
4020 void *p_cookie,
4021 const unsigned char *cli_id, size_t cli_id_len,
4022 const unsigned char *in, size_t in_len,
4023 unsigned char *obuf, size_t buf_len, size_t *olen )
4024{
4025 size_t sid_len, cookie_len;
4026 unsigned char *p;
4027
4028 if( f_cookie_write == NULL || f_cookie_check == NULL )
4029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4030
4031 /*
4032 * Structure of ClientHello with record and handshake headers,
4033 * and expected values. We don't need to check a lot, more checks will be
4034 * done when actually parsing the ClientHello - skipping those checks
4035 * avoids code duplication and does not make cookie forging any easier.
4036 *
4037 * 0-0 ContentType type; copied, must be handshake
4038 * 1-2 ProtocolVersion version; copied
4039 * 3-4 uint16 epoch; copied, must be 0
4040 * 5-10 uint48 sequence_number; copied
4041 * 11-12 uint16 length; (ignored)
4042 *
4043 * 13-13 HandshakeType msg_type; (ignored)
4044 * 14-16 uint24 length; (ignored)
4045 * 17-18 uint16 message_seq; copied
4046 * 19-21 uint24 fragment_offset; copied, must be 0
4047 * 22-24 uint24 fragment_length; (ignored)
4048 *
4049 * 25-26 ProtocolVersion client_version; (ignored)
4050 * 27-58 Random random; (ignored)
4051 * 59-xx SessionID session_id; 1 byte len + sid_len content
4052 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4053 * ...
4054 *
4055 * Minimum length is 61 bytes.
4056 */
4057 if( in_len < 61 ||
4058 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4059 in[3] != 0 || in[4] != 0 ||
4060 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4061 {
4062 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4063 }
4064
4065 sid_len = in[59];
4066 if( sid_len > in_len - 61 )
4067 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4068
4069 cookie_len = in[60 + sid_len];
4070 if( cookie_len > in_len - 60 )
4071 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4072
4073 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4074 cli_id, cli_id_len ) == 0 )
4075 {
4076 /* Valid cookie */
4077 return( 0 );
4078 }
4079
4080 /*
4081 * If we get here, we've got an invalid cookie, let's prepare HVR.
4082 *
4083 * 0-0 ContentType type; copied
4084 * 1-2 ProtocolVersion version; copied
4085 * 3-4 uint16 epoch; copied
4086 * 5-10 uint48 sequence_number; copied
4087 * 11-12 uint16 length; olen - 13
4088 *
4089 * 13-13 HandshakeType msg_type; hello_verify_request
4090 * 14-16 uint24 length; olen - 25
4091 * 17-18 uint16 message_seq; copied
4092 * 19-21 uint24 fragment_offset; copied
4093 * 22-24 uint24 fragment_length; olen - 25
4094 *
4095 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4096 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4097 *
4098 * Minimum length is 28.
4099 */
4100 if( buf_len < 28 )
4101 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4102
4103 /* Copy most fields and adapt others */
4104 memcpy( obuf, in, 25 );
4105 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4106 obuf[25] = 0xfe;
4107 obuf[26] = 0xff;
4108
4109 /* Generate and write actual cookie */
4110 p = obuf + 28;
4111 if( f_cookie_write( p_cookie,
4112 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4113 {
4114 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4115 }
4116
4117 *olen = p - obuf;
4118
4119 /* Go back and fill length fields */
4120 obuf[27] = (unsigned char)( *olen - 28 );
4121
4122 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4123 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4124 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4125
4126 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4127 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4128
4129 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4130}
4131
4132/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004133 * Handle possible client reconnect with the same UDP quadruplet
4134 * (RFC 6347 Section 4.2.8).
4135 *
4136 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4137 * that looks like a ClientHello.
4138 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004139 * - if the input looks like a ClientHello without cookies,
4140 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004141 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004142 * - if the input looks like a ClientHello with a valid cookie,
4143 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004144 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004145 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004146 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004147 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004148 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4149 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004150 */
4151static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4152{
4153 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004154 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004155
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004156 ret = ssl_check_dtls_clihlo_cookie(
4157 ssl->conf->f_cookie_write,
4158 ssl->conf->f_cookie_check,
4159 ssl->conf->p_cookie,
4160 ssl->cli_id, ssl->cli_id_len,
4161 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004162 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004163
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004164 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4165
4166 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004167 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004168 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004169 * If the error is permanent we'll catch it later,
4170 * if it's not, then hopefully it'll work next time. */
4171 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4172
4173 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004174 }
4175
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004176 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004177 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004178 /* Got a valid cookie, partially reset context */
4179 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4180 {
4181 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4182 return( ret );
4183 }
4184
4185 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004186 }
4187
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004188 return( ret );
4189}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004190#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004191
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004192/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004193 * ContentType type;
4194 * ProtocolVersion version;
4195 * uint16 epoch; // DTLS only
4196 * uint48 sequence_number; // DTLS only
4197 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004198 *
4199 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004200 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004201 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4202 *
4203 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004204 * 1. proceed with the record if this function returns 0
4205 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4206 * 3. return CLIENT_RECONNECT if this function return that value
4207 * 4. drop the whole datagram if this function returns anything else.
4208 * Point 2 is needed when the peer is resending, and we have already received
4209 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004212{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004213 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 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 +02004216
Paul Bakker5121ce52009-01-03 21:22:43 +00004217 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004218 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004219 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004223 ssl->in_msgtype,
4224 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004225
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004226 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4228 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4229 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4230 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004233
4234#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004235 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4236 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004237 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4238#endif /* MBEDTLS_SSL_PROTO_DTLS */
4239 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4240 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004243 }
4244
4245 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004246 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4249 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 }
4251
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004252 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4255 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004256 }
4257
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004258 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004259 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004260 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4263 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004264 }
4265
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004266 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004267 * DTLS-related tests.
4268 * Check epoch before checking length constraint because
4269 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4270 * message gets duplicated before the corresponding Finished message,
4271 * the second ChangeCipherSpec should be discarded because it belongs
4272 * to an old epoch, but not because its length is shorter than
4273 * the minimum record length for packets using the new record transform.
4274 * Note that these two kinds of failures are handled differently,
4275 * as an unexpected record is silently skipped but an invalid
4276 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004277 */
4278#if defined(MBEDTLS_SSL_PROTO_DTLS)
4279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4280 {
4281 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4282
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004283 /* Check epoch (and sequence number) with DTLS */
4284 if( rec_epoch != ssl->in_epoch )
4285 {
4286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4287 "expected %d, received %d",
4288 ssl->in_epoch, rec_epoch ) );
4289
4290#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4291 /*
4292 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4293 * access the first byte of record content (handshake type), as we
4294 * have an active transform (possibly iv_len != 0), so use the
4295 * fact that the record header len is 13 instead.
4296 */
4297 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4298 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4299 rec_epoch == 0 &&
4300 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4301 ssl->in_left > 13 &&
4302 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4303 {
4304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4305 "from the same port" ) );
4306 return( ssl_handle_possible_reconnect( ssl ) );
4307 }
4308 else
4309#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004310 {
4311 /* Consider buffering the record. */
4312 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4313 {
4314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4315 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4316 }
4317
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004318 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004319 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004320 }
4321
4322#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4323 /* Replay detection only works for the current epoch */
4324 if( rec_epoch == ssl->in_epoch &&
4325 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4326 {
4327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4328 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4329 }
4330#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004331
Hanno Becker52c6dc62017-05-26 16:07:36 +01004332 /* Drop unexpected ApplicationData records,
4333 * except at the beginning of renegotiations */
4334 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4335 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4336#if defined(MBEDTLS_SSL_RENEGOTIATION)
4337 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4338 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4339#endif
4340 )
4341 {
4342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4343 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4344 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004345 }
4346#endif /* MBEDTLS_SSL_PROTO_DTLS */
4347
Hanno Becker52c6dc62017-05-26 16:07:36 +01004348
4349 /* Check length against bounds of the current transform and version */
4350 if( ssl->transform_in == NULL )
4351 {
4352 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004353 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004354 {
4355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4356 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4357 }
4358 }
4359 else
4360 {
4361 if( ssl->in_msglen < ssl->transform_in->minlen )
4362 {
4363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4364 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4365 }
4366
4367#if defined(MBEDTLS_SSL_PROTO_SSL3)
4368 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004369 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004370 {
4371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4372 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4373 }
4374#endif
4375#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4376 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4377 /*
4378 * TLS encrypted messages can have up to 256 bytes of padding
4379 */
4380 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4381 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004382 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004383 {
4384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4385 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4386 }
4387#endif
4388 }
4389
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004390 return( 0 );
4391}
Paul Bakker5121ce52009-01-03 21:22:43 +00004392
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004393/*
4394 * If applicable, decrypt (and decompress) record content
4395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004397{
4398 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4401 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4404 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 ret = mbedtls_ssl_hw_record_read( ssl );
4409 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4412 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004413 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004414
4415 if( ret == 0 )
4416 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004419 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004420 {
4421 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 return( ret );
4425 }
4426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004428 ssl->in_msg, ssl->in_msglen );
4429
Angus Grattond8213d02016-05-25 20:56:48 +10004430 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 }
4435 }
4436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004438 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004440 {
4441 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004444 return( ret );
4445 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004453 }
4454#endif
4455
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004456 return( 0 );
4457}
4458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004460
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004461/*
4462 * Read a record.
4463 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004464 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4465 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4466 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004467 */
Hanno Becker1097b342018-08-15 14:09:41 +01004468
4469/* Helper functions for mbedtls_ssl_read_record(). */
4470static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004471static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4472static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004473
Hanno Becker327c93b2018-08-15 13:56:18 +01004474int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004475 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004476{
4477 int ret;
4478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004480
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004481 if( ssl->keep_current_message == 0 )
4482 {
4483 do {
Simon Butcher99000142016-10-13 17:21:01 +01004484
Hanno Becker26994592018-08-15 14:14:59 +01004485 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004486 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004487 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004488
Hanno Beckere74d5562018-08-15 14:26:08 +01004489 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004490 {
Hanno Becker40f50842018-08-15 14:48:01 +01004491#if defined(MBEDTLS_SSL_PROTO_DTLS)
4492 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004493
Hanno Becker40f50842018-08-15 14:48:01 +01004494 /* We only check for buffered messages if the
4495 * current datagram is fully consumed. */
4496 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004497 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004498 {
Hanno Becker40f50842018-08-15 14:48:01 +01004499 if( ssl_load_buffered_message( ssl ) == 0 )
4500 have_buffered = 1;
4501 }
4502
4503 if( have_buffered == 0 )
4504#endif /* MBEDTLS_SSL_PROTO_DTLS */
4505 {
4506 ret = ssl_get_next_record( ssl );
4507 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4508 continue;
4509
4510 if( ret != 0 )
4511 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004512 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004513 return( ret );
4514 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004515 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004516 }
4517
4518 ret = mbedtls_ssl_handle_message_type( ssl );
4519
Hanno Becker40f50842018-08-15 14:48:01 +01004520#if defined(MBEDTLS_SSL_PROTO_DTLS)
4521 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4522 {
4523 /* Buffer future message */
4524 ret = ssl_buffer_message( ssl );
4525 if( ret != 0 )
4526 return( ret );
4527
4528 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4529 }
4530#endif /* MBEDTLS_SSL_PROTO_DTLS */
4531
Hanno Becker90333da2017-10-10 11:27:13 +01004532 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4533 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004534
4535 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004536 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004537 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004538 return( ret );
4539 }
4540
Hanno Becker327c93b2018-08-15 13:56:18 +01004541 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004542 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004543 {
4544 mbedtls_ssl_update_handshake_status( ssl );
4545 }
Simon Butcher99000142016-10-13 17:21:01 +01004546 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004547 else
Simon Butcher99000142016-10-13 17:21:01 +01004548 {
Hanno Becker02f59072018-08-15 14:00:24 +01004549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004550 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004551 }
4552
4553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4554
4555 return( 0 );
4556}
4557
Hanno Becker40f50842018-08-15 14:48:01 +01004558#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004559static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004560{
Hanno Becker40f50842018-08-15 14:48:01 +01004561 if( ssl->in_left > ssl->next_record_offset )
4562 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004563
Hanno Becker40f50842018-08-15 14:48:01 +01004564 return( 0 );
4565}
4566
4567static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4568{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004569 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004570 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004571 int ret = 0;
4572
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004573 if( hs == NULL )
4574 return( -1 );
4575
Hanno Beckere00ae372018-08-20 09:39:42 +01004576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4577
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004578 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4579 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4580 {
4581 /* Check if we have seen a ChangeCipherSpec before.
4582 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004583 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004584 {
4585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4586 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004587 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004588 }
4589
Hanno Becker39b8bc92018-08-28 17:17:13 +01004590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004591 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4592 ssl->in_msglen = 1;
4593 ssl->in_msg[0] = 1;
4594
4595 /* As long as they are equal, the exact value doesn't matter. */
4596 ssl->in_left = 0;
4597 ssl->next_record_offset = 0;
4598
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004599 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004600 goto exit;
4601 }
Hanno Becker37f95322018-08-16 13:55:32 +01004602
Hanno Beckerb8f50142018-08-28 10:01:34 +01004603#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004604 /* Debug only */
4605 {
4606 unsigned offset;
4607 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4608 {
4609 hs_buf = &hs->buffering.hs[offset];
4610 if( hs_buf->is_valid == 1 )
4611 {
4612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4613 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004614 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004615 }
4616 }
4617 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004618#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004619
4620 /* Check if we have buffered and/or fully reassembled the
4621 * next handshake message. */
4622 hs_buf = &hs->buffering.hs[0];
4623 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4624 {
4625 /* Synthesize a record containing the buffered HS message. */
4626 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4627 ( hs_buf->data[2] << 8 ) |
4628 hs_buf->data[3];
4629
4630 /* Double-check that we haven't accidentally buffered
4631 * a message that doesn't fit into the input buffer. */
4632 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4633 {
4634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4636 }
4637
4638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4639 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4640 hs_buf->data, msg_len + 12 );
4641
4642 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4643 ssl->in_hslen = msg_len + 12;
4644 ssl->in_msglen = msg_len + 12;
4645 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4646
4647 ret = 0;
4648 goto exit;
4649 }
4650 else
4651 {
4652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4653 hs->in_msg_seq ) );
4654 }
4655
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004656 ret = -1;
4657
4658exit:
4659
4660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4661 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004662}
4663
Hanno Beckera02b0b42018-08-21 17:20:27 +01004664static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4665 size_t desired )
4666{
4667 int offset;
4668 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4670 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004671
Hanno Becker01315ea2018-08-21 17:22:17 +01004672 /* Get rid of future records epoch first, if such exist. */
4673 ssl_free_buffered_record( ssl );
4674
4675 /* Check if we have enough space available now. */
4676 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4677 hs->buffering.total_bytes_buffered ) )
4678 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004680 return( 0 );
4681 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004682
Hanno Becker4f432ad2018-08-28 10:02:32 +01004683 /* We don't have enough space to buffer the next expected handshake
4684 * message. Remove buffers used for future messages to gain space,
4685 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004686 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4687 offset >= 0; offset-- )
4688 {
4689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4690 offset ) );
4691
Hanno Beckerb309b922018-08-23 13:18:05 +01004692 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004693
4694 /* Check if we have enough space available now. */
4695 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4696 hs->buffering.total_bytes_buffered ) )
4697 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004699 return( 0 );
4700 }
4701 }
4702
4703 return( -1 );
4704}
4705
Hanno Becker40f50842018-08-15 14:48:01 +01004706static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4707{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004708 int ret = 0;
4709 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4710
4711 if( hs == NULL )
4712 return( 0 );
4713
4714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4715
4716 switch( ssl->in_msgtype )
4717 {
4718 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004720
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004721 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004722 break;
4723
4724 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004725 {
4726 unsigned recv_msg_seq_offset;
4727 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4728 mbedtls_ssl_hs_buffer *hs_buf;
4729 size_t msg_len = ssl->in_hslen - 12;
4730
4731 /* We should never receive an old handshake
4732 * message - double-check nonetheless. */
4733 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4734 {
4735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4737 }
4738
4739 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4740 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4741 {
4742 /* Silently ignore -- message too far in the future */
4743 MBEDTLS_SSL_DEBUG_MSG( 2,
4744 ( "Ignore future HS message with sequence number %u, "
4745 "buffering window %u - %u",
4746 recv_msg_seq, ssl->handshake->in_msg_seq,
4747 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4748
4749 goto exit;
4750 }
4751
4752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4753 recv_msg_seq, recv_msg_seq_offset ) );
4754
4755 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4756
4757 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004758 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004759 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004760 size_t reassembly_buf_sz;
4761
Hanno Becker37f95322018-08-16 13:55:32 +01004762 hs_buf->is_fragmented =
4763 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4764
4765 /* We copy the message back into the input buffer
4766 * after reassembly, so check that it's not too large.
4767 * This is an implementation-specific limitation
4768 * and not one from the standard, hence it is not
4769 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004770 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004771 {
4772 /* Ignore message */
4773 goto exit;
4774 }
4775
Hanno Beckere0b150f2018-08-21 15:51:03 +01004776 /* Check if we have enough space to buffer the message. */
4777 if( hs->buffering.total_bytes_buffered >
4778 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4782 }
4783
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004784 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4785 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004786
4787 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4788 hs->buffering.total_bytes_buffered ) )
4789 {
4790 if( recv_msg_seq_offset > 0 )
4791 {
4792 /* If we can't buffer a future message because
4793 * of space limitations -- ignore. */
4794 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",
4795 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4796 (unsigned) hs->buffering.total_bytes_buffered ) );
4797 goto exit;
4798 }
Hanno Beckere1801392018-08-21 16:51:05 +01004799 else
4800 {
4801 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",
4802 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4803 (unsigned) hs->buffering.total_bytes_buffered ) );
4804 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004805
Hanno Beckera02b0b42018-08-21 17:20:27 +01004806 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004807 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004808 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",
4809 (unsigned) msg_len,
4810 (unsigned) reassembly_buf_sz,
4811 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004812 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004813 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4814 goto exit;
4815 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004816 }
4817
4818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4819 msg_len ) );
4820
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004821 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4822 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004823 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004824 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004825 goto exit;
4826 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004827 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004828
4829 /* Prepare final header: copy msg_type, length and message_seq,
4830 * then add standardised fragment_offset and fragment_length */
4831 memcpy( hs_buf->data, ssl->in_msg, 6 );
4832 memset( hs_buf->data + 6, 0, 3 );
4833 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4834
4835 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004836
4837 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004838 }
4839 else
4840 {
4841 /* Make sure msg_type and length are consistent */
4842 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4843 {
4844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4845 /* Ignore */
4846 goto exit;
4847 }
4848 }
4849
Hanno Becker4422bbb2018-08-20 09:40:19 +01004850 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004851 {
4852 size_t frag_len, frag_off;
4853 unsigned char * const msg = hs_buf->data + 12;
4854
4855 /*
4856 * Check and copy current fragment
4857 */
4858
4859 /* Validation of header fields already done in
4860 * mbedtls_ssl_prepare_handshake_record(). */
4861 frag_off = ssl_get_hs_frag_off( ssl );
4862 frag_len = ssl_get_hs_frag_len( ssl );
4863
4864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4865 frag_off, frag_len ) );
4866 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4867
4868 if( hs_buf->is_fragmented )
4869 {
4870 unsigned char * const bitmask = msg + msg_len;
4871 ssl_bitmask_set( bitmask, frag_off, frag_len );
4872 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4873 msg_len ) == 0 );
4874 }
4875 else
4876 {
4877 hs_buf->is_complete = 1;
4878 }
4879
4880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4881 hs_buf->is_complete ? "" : "not yet " ) );
4882 }
4883
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004884 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004885 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004886
4887 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004888 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004889 break;
4890 }
4891
4892exit:
4893
4894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4895 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004896}
4897#endif /* MBEDTLS_SSL_PROTO_DTLS */
4898
Hanno Becker1097b342018-08-15 14:09:41 +01004899static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004900{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004901 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004902 * Consume last content-layer message and potentially
4903 * update in_msglen which keeps track of the contents'
4904 * consumption state.
4905 *
4906 * (1) Handshake messages:
4907 * Remove last handshake message, move content
4908 * and adapt in_msglen.
4909 *
4910 * (2) Alert messages:
4911 * Consume whole record content, in_msglen = 0.
4912 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004913 * (3) Change cipher spec:
4914 * Consume whole record content, in_msglen = 0.
4915 *
4916 * (4) Application data:
4917 * Don't do anything - the record layer provides
4918 * the application data as a stream transport
4919 * and consumes through mbedtls_ssl_read only.
4920 *
4921 */
4922
4923 /* Case (1): Handshake messages */
4924 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004925 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004926 /* Hard assertion to be sure that no application data
4927 * is in flight, as corrupting ssl->in_msglen during
4928 * ssl->in_offt != NULL is fatal. */
4929 if( ssl->in_offt != NULL )
4930 {
4931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4933 }
4934
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004935 /*
4936 * Get next Handshake message in the current record
4937 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004938
Hanno Becker4a810fb2017-05-24 16:27:30 +01004939 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004940 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004941 * current handshake content: If DTLS handshake
4942 * fragmentation is used, that's the fragment
4943 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004944 * size here is faulty and should be changed at
4945 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004946 * (2) While it doesn't seem to cause problems, one
4947 * has to be very careful not to assume that in_hslen
4948 * is always <= in_msglen in a sensible communication.
4949 * Again, it's wrong for DTLS handshake fragmentation.
4950 * The following check is therefore mandatory, and
4951 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004952 * Additionally, ssl->in_hslen might be arbitrarily out of
4953 * bounds after handling a DTLS message with an unexpected
4954 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004955 */
4956 if( ssl->in_hslen < ssl->in_msglen )
4957 {
4958 ssl->in_msglen -= ssl->in_hslen;
4959 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4960 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004961
Hanno Becker4a810fb2017-05-24 16:27:30 +01004962 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4963 ssl->in_msg, ssl->in_msglen );
4964 }
4965 else
4966 {
4967 ssl->in_msglen = 0;
4968 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004969
Hanno Becker4a810fb2017-05-24 16:27:30 +01004970 ssl->in_hslen = 0;
4971 }
4972 /* Case (4): Application data */
4973 else if( ssl->in_offt != NULL )
4974 {
4975 return( 0 );
4976 }
4977 /* Everything else (CCS & Alerts) */
4978 else
4979 {
4980 ssl->in_msglen = 0;
4981 }
4982
Hanno Becker1097b342018-08-15 14:09:41 +01004983 return( 0 );
4984}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004985
Hanno Beckere74d5562018-08-15 14:26:08 +01004986static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4987{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004988 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004989 return( 1 );
4990
4991 return( 0 );
4992}
4993
Hanno Becker5f066e72018-08-16 14:56:31 +01004994#if defined(MBEDTLS_SSL_PROTO_DTLS)
4995
4996static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4997{
4998 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4999 if( hs == NULL )
5000 return;
5001
Hanno Becker01315ea2018-08-21 17:22:17 +01005002 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005003 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005004 hs->buffering.total_bytes_buffered -=
5005 hs->buffering.future_record.len;
5006
5007 mbedtls_free( hs->buffering.future_record.data );
5008 hs->buffering.future_record.data = NULL;
5009 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005010}
5011
5012static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5013{
5014 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5015 unsigned char * rec;
5016 size_t rec_len;
5017 unsigned rec_epoch;
5018
5019 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5020 return( 0 );
5021
5022 if( hs == NULL )
5023 return( 0 );
5024
Hanno Becker5f066e72018-08-16 14:56:31 +01005025 rec = hs->buffering.future_record.data;
5026 rec_len = hs->buffering.future_record.len;
5027 rec_epoch = hs->buffering.future_record.epoch;
5028
5029 if( rec == NULL )
5030 return( 0 );
5031
Hanno Becker4cb782d2018-08-20 11:19:05 +01005032 /* Only consider loading future records if the
5033 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005034 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005035 return( 0 );
5036
Hanno Becker5f066e72018-08-16 14:56:31 +01005037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5038
5039 if( rec_epoch != ssl->in_epoch )
5040 {
5041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5042 goto exit;
5043 }
5044
5045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5046
5047 /* Double-check that the record is not too large */
5048 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5049 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5050 {
5051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5053 }
5054
5055 memcpy( ssl->in_hdr, rec, rec_len );
5056 ssl->in_left = rec_len;
5057 ssl->next_record_offset = 0;
5058
5059 ssl_free_buffered_record( ssl );
5060
5061exit:
5062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5063 return( 0 );
5064}
5065
5066static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5067{
5068 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5069 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005070 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005071
5072 /* Don't buffer future records outside handshakes. */
5073 if( hs == NULL )
5074 return( 0 );
5075
5076 /* Only buffer handshake records (we are only interested
5077 * in Finished messages). */
5078 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5079 return( 0 );
5080
5081 /* Don't buffer more than one future epoch record. */
5082 if( hs->buffering.future_record.data != NULL )
5083 return( 0 );
5084
Hanno Becker01315ea2018-08-21 17:22:17 +01005085 /* Don't buffer record if there's not enough buffering space remaining. */
5086 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5087 hs->buffering.total_bytes_buffered ) )
5088 {
5089 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",
5090 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5091 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005092 return( 0 );
5093 }
5094
Hanno Becker5f066e72018-08-16 14:56:31 +01005095 /* Buffer record */
5096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5097 ssl->in_epoch + 1 ) );
5098 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5099 rec_hdr_len + ssl->in_msglen );
5100
5101 /* ssl_parse_record_header() only considers records
5102 * of the next epoch as candidates for buffering. */
5103 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005104 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005105
5106 hs->buffering.future_record.data =
5107 mbedtls_calloc( 1, hs->buffering.future_record.len );
5108 if( hs->buffering.future_record.data == NULL )
5109 {
5110 /* If we run out of RAM trying to buffer a
5111 * record from the next epoch, just ignore. */
5112 return( 0 );
5113 }
5114
Hanno Becker01315ea2018-08-21 17:22:17 +01005115 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005116
Hanno Becker01315ea2018-08-21 17:22:17 +01005117 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005118 return( 0 );
5119}
5120
5121#endif /* MBEDTLS_SSL_PROTO_DTLS */
5122
Hanno Beckere74d5562018-08-15 14:26:08 +01005123static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005124{
5125 int ret;
5126
Hanno Becker5f066e72018-08-16 14:56:31 +01005127#if defined(MBEDTLS_SSL_PROTO_DTLS)
5128 /* We might have buffered a future record; if so,
5129 * and if the epoch matches now, load it.
5130 * On success, this call will set ssl->in_left to
5131 * the length of the buffered record, so that
5132 * the calls to ssl_fetch_input() below will
5133 * essentially be no-ops. */
5134 ret = ssl_load_buffered_record( ssl );
5135 if( ret != 0 )
5136 return( ret );
5137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005142 return( ret );
5143 }
5144
5145 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005148 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5149 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005150 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005151 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5152 {
5153 ret = ssl_buffer_future_record( ssl );
5154 if( ret != 0 )
5155 return( ret );
5156
5157 /* Fall through to handling of unexpected records */
5158 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5159 }
5160
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005161 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5162 {
5163 /* Skip unexpected record (but not whole datagram) */
5164 ssl->next_record_offset = ssl->in_msglen
5165 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005166
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5168 "(header)" ) );
5169 }
5170 else
5171 {
5172 /* Skip invalid record and the rest of the datagram */
5173 ssl->next_record_offset = 0;
5174 ssl->in_left = 0;
5175
5176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5177 "(header)" ) );
5178 }
5179
5180 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005181 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005182 }
5183#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005184 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005185 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005186
5187 /*
5188 * Read and optionally decrypt the message contents
5189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5191 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005194 return( ret );
5195 }
5196
5197 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005202 if( ssl->next_record_offset < ssl->in_left )
5203 {
5204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5205 }
5206 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005207 else
5208#endif
5209 ssl->in_left = 0;
5210
5211 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005215 {
5216 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5218 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005219 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005220 /* Except when waiting for Finished as a bad mac here
5221 * probably means something went wrong in the handshake
5222 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5223 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5224 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5225 {
5226#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5227 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5228 {
5229 mbedtls_ssl_send_alert_message( ssl,
5230 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5231 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5232 }
5233#endif
5234 return( ret );
5235 }
5236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005238 if( ssl->conf->badmac_limit != 0 &&
5239 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5242 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005243 }
5244#endif
5245
Hanno Becker4a810fb2017-05-24 16:27:30 +01005246 /* As above, invalid records cause
5247 * dismissal of the whole datagram. */
5248
5249 ssl->next_record_offset = 0;
5250 ssl->in_left = 0;
5251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005253 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005254 }
5255
5256 return( ret );
5257 }
5258 else
5259#endif
5260 {
5261 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5263 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265 mbedtls_ssl_send_alert_message( ssl,
5266 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5267 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005268 }
5269#endif
5270 return( ret );
5271 }
5272 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005273
Simon Butcher99000142016-10-13 17:21:01 +01005274 return( 0 );
5275}
5276
5277int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5278{
5279 int ret;
5280
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005281 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005282 * Handle particular types of records
5283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005285 {
Simon Butcher99000142016-10-13 17:21:01 +01005286 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5287 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005288 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005290 }
5291
Hanno Beckere678eaa2018-08-21 14:57:46 +01005292 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005293 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005294 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005295 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5297 ssl->in_msglen ) );
5298 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005299 }
5300
Hanno Beckere678eaa2018-08-21 14:57:46 +01005301 if( ssl->in_msg[0] != 1 )
5302 {
5303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5304 ssl->in_msg[0] ) );
5305 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5306 }
5307
5308#if defined(MBEDTLS_SSL_PROTO_DTLS)
5309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5310 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5311 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5312 {
5313 if( ssl->handshake == NULL )
5314 {
5315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5316 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5317 }
5318
5319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5320 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5321 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005322#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005323 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005327 if( ssl->in_msglen != 2 )
5328 {
5329 /* Note: Standard allows for more than one 2 byte alert
5330 to be packed in a single message, but Mbed TLS doesn't
5331 currently support this. */
5332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5333 ssl->in_msglen ) );
5334 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5335 }
5336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005338 ssl->in_msg[0], ssl->in_msg[1] ) );
5339
5340 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005341 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005346 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005348 }
5349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5351 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5354 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005355 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005356
5357#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5358 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5359 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5360 {
Hanno Becker90333da2017-10-10 11:27:13 +01005361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005362 /* Will be handled when trying to parse ServerHello */
5363 return( 0 );
5364 }
5365#endif
5366
5367#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5368 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5369 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5370 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5371 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5372 {
5373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5374 /* Will be handled in mbedtls_ssl_parse_certificate() */
5375 return( 0 );
5376 }
5377#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5378
5379 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005380 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005381 }
5382
Hanno Beckerc76c6192017-06-06 10:03:17 +01005383#if defined(MBEDTLS_SSL_PROTO_DTLS)
5384 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5385 ssl->handshake != NULL &&
5386 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5387 {
5388 ssl_handshake_wrapup_free_hs_transform( ssl );
5389 }
5390#endif
5391
Paul Bakker5121ce52009-01-03 21:22:43 +00005392 return( 0 );
5393}
5394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005396{
5397 int ret;
5398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5400 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5401 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005402 {
5403 return( ret );
5404 }
5405
5406 return( 0 );
5407}
5408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005410 unsigned char level,
5411 unsigned char message )
5412{
5413 int ret;
5414
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005415 if( ssl == NULL || ssl->conf == NULL )
5416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005422 ssl->out_msglen = 2;
5423 ssl->out_msg[0] = level;
5424 ssl->out_msg[1] = message;
5425
Hanno Becker67bc7c32018-08-06 11:33:50 +01005426 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005429 return( ret );
5430 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005432
5433 return( 0 );
5434}
5435
Paul Bakker5121ce52009-01-03 21:22:43 +00005436/*
5437 * Handshake functions
5438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5440 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5441 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5442 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5443 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5444 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5445 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005446/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5454 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005455 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5456 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005459 ssl->state++;
5460 return( 0 );
5461 }
5462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005465}
5466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5474 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005475 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5476 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005479 ssl->state++;
5480 return( 0 );
5481 }
5482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005485}
Gilles Peskinef9828522017-05-03 12:28:43 +02005486
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005487#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005488/* Some certificate support -> implement write and parse */
5489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005491{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005493 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 const mbedtls_x509_crt *crt;
5495 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5500 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005501 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5502 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005505 ssl->state++;
5506 return( 0 );
5507 }
5508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005510 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 {
5512 if( ssl->client_auth == 0 )
5513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 ssl->state++;
5516 return( 0 );
5517 }
5518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005520 /*
5521 * If using SSLv3 and got no cert, send an Alert message
5522 * (otherwise an empty Certificate message will be sent).
5523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5525 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005526 {
5527 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5529 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5530 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005533 goto write_msg;
5534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537#endif /* MBEDTLS_SSL_CLI_C */
5538#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005539 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5544 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 }
5546 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005550
5551 /*
5552 * 0 . 0 handshake type
5553 * 1 . 3 handshake length
5554 * 4 . 6 length of all certs
5555 * 7 . 9 length of cert. 1
5556 * 10 . n-1 peer certificate
5557 * n . n+2 length of cert. 2
5558 * n+3 . ... upper level cert, etc.
5559 */
5560 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562
Paul Bakker29087132010-03-21 21:03:34 +00005563 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005564 {
5565 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005566 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005569 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005571 }
5572
5573 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5574 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5575 ssl->out_msg[i + 2] = (unsigned char)( n );
5576
5577 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5578 i += n; crt = crt->next;
5579 }
5580
5581 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5582 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5583 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5584
5585 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5587 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005588
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005589#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005590write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005591#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005592
5593 ssl->state++;
5594
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005595 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005596 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005598 return( ret );
5599 }
5600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005602
Paul Bakkered27a042013-04-18 22:46:23 +02005603 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005604}
5605
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005606/*
5607 * Once the certificate message is read, parse it into a cert chain and
5608 * perform basic checks, but leave actual verification to the caller
5609 */
5610static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005611{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005612 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005613 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005614 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616#if defined(MBEDTLS_SSL_SRV_C)
5617#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005618 /*
5619 * Check if the client sent an empty certificate
5620 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005621 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005623 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005624 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5626 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5627 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005630
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005631 /* The client was asked for a certificate but didn't send
5632 one. The client should know what's going on, so we
5633 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005634 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005635 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 }
5637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5641 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005642 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5646 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5647 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5648 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005651
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005652 /* The client was asked for a certificate but didn't send
5653 one. The client should know what's going on, so we
5654 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005655 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005656 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 }
5658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5660 MBEDTLS_SSL_PROTO_TLS1_2 */
5661#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005666 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5667 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005669 }
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5672 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005675 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5676 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005678 }
5679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005681
Paul Bakker5121ce52009-01-03 21:22:43 +00005682 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005684 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005685 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005686
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005687 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005688 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005691 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5692 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005694 }
5695
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005696 /* In case we tried to reuse a session but it failed */
5697 if( ssl->session_negotiate->peer_cert != NULL )
5698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5700 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005701 }
5702
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005703 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005705 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005708 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5709 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005710 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 }
5712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005714
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005715 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005716
5717 while( i < ssl->in_hslen )
5718 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005719 if ( i + 3 > ssl->in_hslen ) {
5720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5721 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5722 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5723 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005725 if( ssl->in_msg[i] != 0 )
5726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005728 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005731 }
5732
5733 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5734 | (unsigned int) ssl->in_msg[i + 2];
5735 i += 3;
5736
5737 if( n < 128 || i + n > ssl->in_hslen )
5738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005740 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5741 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005743 }
5744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005746 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005747 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005748 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005749 case 0: /*ok*/
5750 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5751 /* Ignore certificate with an unknown algorithm: maybe a
5752 prior certificate was already trusted. */
5753 break;
5754
5755 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5756 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5757 goto crt_parse_der_failed;
5758
5759 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5760 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5761 goto crt_parse_der_failed;
5762
5763 default:
5764 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5765 crt_parse_der_failed:
5766 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005768 return( ret );
5769 }
5770
5771 i += n;
5772 }
5773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005775
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005776 /*
5777 * On client, make sure the server cert doesn't change during renego to
5778 * avoid "triple handshake" attack: https://secure-resumption.com/
5779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005781 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005783 {
5784 if( ssl->session->peer_cert == NULL )
5785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005787 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5788 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005790 }
5791
5792 if( ssl->session->peer_cert->raw.len !=
5793 ssl->session_negotiate->peer_cert->raw.len ||
5794 memcmp( ssl->session->peer_cert->raw.p,
5795 ssl->session_negotiate->peer_cert->raw.p,
5796 ssl->session->peer_cert->raw.len ) != 0 )
5797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005799 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005802 }
5803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005805
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005806 return( 0 );
5807}
5808
5809int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5810{
5811 int ret;
5812 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5813 ssl->transform_negotiate->ciphersuite_info;
5814#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5815 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5816 ? ssl->handshake->sni_authmode
5817 : ssl->conf->authmode;
5818#else
5819 const int authmode = ssl->conf->authmode;
5820#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005821 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005822
5823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5824
5825 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5826 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5827 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5828 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5829 {
5830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5831 ssl->state++;
5832 return( 0 );
5833 }
5834
5835#if defined(MBEDTLS_SSL_SRV_C)
5836 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5837 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5838 {
5839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5840 ssl->state++;
5841 return( 0 );
5842 }
5843
5844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5845 authmode == MBEDTLS_SSL_VERIFY_NONE )
5846 {
5847 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005849
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005850 ssl->state++;
5851 return( 0 );
5852 }
5853#endif
5854
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005855#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5856 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005857 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005858 {
5859 goto crt_verify;
5860 }
5861#endif
5862
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005863 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005864 {
5865 /* mbedtls_ssl_read_record may have sent an alert already. We
5866 let it decide whether to alert. */
5867 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5868 return( ret );
5869 }
5870
5871 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5872 {
5873#if defined(MBEDTLS_SSL_SRV_C)
5874 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5875 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5876 {
5877 ret = 0;
5878 }
5879#endif
5880
5881 ssl->state++;
5882 return( ret );
5883 }
5884
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005885#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5886 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005887 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005888
5889crt_verify:
5890 if( ssl->handshake->ecrs_enabled)
5891 rs_ctx = &ssl->handshake->ecrs_ctx;
5892#endif
5893
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005894 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005896 mbedtls_x509_crt *ca_chain;
5897 mbedtls_x509_crl *ca_crl;
5898
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005899#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005900 if( ssl->handshake->sni_ca_chain != NULL )
5901 {
5902 ca_chain = ssl->handshake->sni_ca_chain;
5903 ca_crl = ssl->handshake->sni_ca_crl;
5904 }
5905 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005906#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005907 {
5908 ca_chain = ssl->conf->ca_chain;
5909 ca_crl = ssl->conf->ca_crl;
5910 }
5911
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005912 /*
5913 * Main check: verify certificate
5914 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005915 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005916 ssl->session_negotiate->peer_cert,
5917 ca_chain, ca_crl,
5918 ssl->conf->cert_profile,
5919 ssl->hostname,
5920 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005921 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005922
5923 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005926 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005927
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005928#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5929 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02005930 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005931#endif
5932
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005933 /*
5934 * Secondary checks: always done, but change 'ret' only if it was 0
5935 */
5936
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005937#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005940
5941 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005943 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005944 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005945 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005948 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005950 }
5951 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005952#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005955 ciphersuite_info,
5956 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005957 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005960 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005962 }
5963
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005964 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5965 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5966 * with details encoded in the verification flags. All other kinds
5967 * of error codes, including those from the user provided f_vrfy
5968 * functions, are treated as fatal and lead to a failure of
5969 * ssl_parse_certificate even if verification was optional. */
5970 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5971 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5972 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5973 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005974 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005975 }
5976
5977 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5978 {
5979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5980 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5981 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005982
5983 if( ret != 0 )
5984 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005985 uint8_t alert;
5986
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005987 /* The certificate may have been rejected for several reasons.
5988 Pick one and send the corresponding alert. Which alert to send
5989 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005990 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5991 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5992 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5993 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5994 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5995 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5996 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5997 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5998 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5999 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6000 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6001 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6002 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6003 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6004 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6005 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6006 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6007 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6008 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6009 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006010 else
6011 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006012 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6013 alert );
6014 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006015
Hanno Beckere6706e62017-05-15 16:05:15 +01006016#if defined(MBEDTLS_DEBUG_C)
6017 if( ssl->session_negotiate->verify_result != 0 )
6018 {
6019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6020 ssl->session_negotiate->verify_result ) );
6021 }
6022 else
6023 {
6024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6025 }
6026#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 }
6028
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006029 ssl->state++;
6030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006032
6033 return( ret );
6034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6036 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6037 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6038 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6039 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6040 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6041 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006044{
6045 int ret;
6046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 ssl->out_msglen = 1;
6051 ssl->out_msg[0] = 1;
6052
Paul Bakker5121ce52009-01-03 21:22:43 +00006053 ssl->state++;
6054
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006055 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006056 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 return( ret );
6059 }
6060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062
6063 return( 0 );
6064}
6065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006067{
6068 int ret;
6069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006071
Hanno Becker327c93b2018-08-15 13:56:18 +01006072 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 return( ret );
6076 }
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006081 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6082 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 }
6085
Hanno Beckere678eaa2018-08-21 14:57:46 +01006086 /* CCS records are only accepted if they have length 1 and content '1',
6087 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006088
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006089 /*
6090 * Switch to our negotiated transform and session parameters for inbound
6091 * data.
6092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006094 ssl->transform_in = ssl->transform_negotiate;
6095 ssl->session_in = ssl->session_negotiate;
6096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006098 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006101 ssl_dtls_replay_reset( ssl );
6102#endif
6103
6104 /* Increment epoch */
6105 if( ++ssl->in_epoch == 0 )
6106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006108 /* This is highly unlikely to happen for legitimate reasons, so
6109 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006111 }
6112 }
6113 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006115 memset( ssl->in_ctr, 0, 8 );
6116
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006117 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6120 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006125 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6126 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006128 }
6129 }
6130#endif
6131
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 ssl->state++;
6133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006135
6136 return( 0 );
6137}
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6140 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006141{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006142 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6145 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6146 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006147 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006148 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6151#if defined(MBEDTLS_SHA512_C)
6152 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006153 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6154 else
6155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156#if defined(MBEDTLS_SHA256_C)
6157 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006158 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006159 else
6160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006164 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006165 }
Paul Bakker380da532012-04-18 16:10:25 +00006166}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6171 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006172 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6173 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6176#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006177 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006180 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006183}
6184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006186 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6189 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006190 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6191 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6194#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006195 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006198 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006201}
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6204 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6205static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006206 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006207{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006208 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6209 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006210}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006211#endif
Paul Bakker380da532012-04-18 16:10:25 +00006212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6214#if defined(MBEDTLS_SHA256_C)
6215static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006216 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006217{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006218 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006219}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006220#endif
Paul Bakker380da532012-04-18 16:10:25 +00006221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222#if defined(MBEDTLS_SHA512_C)
6223static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006224 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006225{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006226 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006227}
Paul Bakker769075d2012-11-24 11:26:46 +01006228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006232static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006234{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006235 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006236 mbedtls_md5_context md5;
6237 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006238
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 unsigned char padbuf[48];
6240 unsigned char md5sum[16];
6241 unsigned char sha1sum[20];
6242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006244 if( !session )
6245 session = ssl->session;
6246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006248
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006249 mbedtls_md5_init( &md5 );
6250 mbedtls_sha1_init( &sha1 );
6251
6252 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6253 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006254
6255 /*
6256 * SSLv3:
6257 * hash =
6258 * MD5( master + pad2 +
6259 * MD5( handshake + sender + master + pad1 ) )
6260 * + SHA1( master + pad2 +
6261 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 */
6263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006265 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6266 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006270 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6271 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006275 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006276
Paul Bakker1ef83d62012-04-11 12:09:53 +00006277 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006278
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006279 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6280 mbedtls_md5_update_ret( &md5, session->master, 48 );
6281 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6282 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006283
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006284 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6285 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6286 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6287 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006288
Paul Bakker1ef83d62012-04-11 12:09:53 +00006289 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006291 mbedtls_md5_starts_ret( &md5 );
6292 mbedtls_md5_update_ret( &md5, session->master, 48 );
6293 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6294 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6295 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006296
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006297 mbedtls_sha1_starts_ret( &sha1 );
6298 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6299 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6300 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6301 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006304
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006305 mbedtls_md5_free( &md5 );
6306 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006307
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006308 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6309 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6310 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006317static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006319{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006320 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006321 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006322 mbedtls_md5_context md5;
6323 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006324 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006327 if( !session )
6328 session = ssl->session;
6329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006331
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006332 mbedtls_md5_init( &md5 );
6333 mbedtls_sha1_init( &sha1 );
6334
6335 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6336 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006337
Paul Bakker1ef83d62012-04-11 12:09:53 +00006338 /*
6339 * TLSv1:
6340 * hash = PRF( master, finished_label,
6341 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6342 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006345 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6346 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006350 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6351 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006352#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006355 ? "client finished"
6356 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006358 mbedtls_md5_finish_ret( &md5, padbuf );
6359 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006360
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006361 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006362 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006365
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006366 mbedtls_md5_free( &md5 );
6367 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006368
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006369 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6376#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006377static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006379{
6380 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006381 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006382 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006383 unsigned char padbuf[32];
6384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006386 if( !session )
6387 session = ssl->session;
6388
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006389 mbedtls_sha256_init( &sha256 );
6390
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006392
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006393 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006394
6395 /*
6396 * TLSv1.2:
6397 * hash = PRF( master, finished_label,
6398 * Hash( handshake ) )[0.11]
6399 */
6400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401#if !defined(MBEDTLS_SHA256_ALT)
6402 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006403 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006404#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006407 ? "client finished"
6408 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006410 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006411
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006412 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006413 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006416
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006417 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006418
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006419 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006426static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006428{
6429 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006430 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006431 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006432 unsigned char padbuf[48];
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006435 if( !session )
6436 session = ssl->session;
6437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006438 mbedtls_sha512_init( &sha512 );
6439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006441
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006442 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006443
6444 /*
6445 * TLSv1.2:
6446 * hash = PRF( master, finished_label,
6447 * Hash( handshake ) )[0.11]
6448 */
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006451 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6452 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006453#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006456 ? "client finished"
6457 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006459 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006460
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006461 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006462 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006465
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006466 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006467
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006468 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006471}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472#endif /* MBEDTLS_SHA512_C */
6473#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006478
6479 /*
6480 * Free our handshake params
6481 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006482 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006484 ssl->handshake = NULL;
6485
6486 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006487 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006488 */
6489 if( ssl->transform )
6490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 mbedtls_ssl_transform_free( ssl->transform );
6492 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006493 }
6494 ssl->transform = ssl->transform_negotiate;
6495 ssl->transform_negotiate = NULL;
6496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006498}
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006501{
6502 int resume = ssl->handshake->resume;
6503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506#if defined(MBEDTLS_SSL_RENEGOTIATION)
6507 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006510 ssl->renego_records_seen = 0;
6511 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006512#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006513
6514 /*
6515 * Free the previous session and switch in the current one
6516 */
Paul Bakker0a597072012-09-25 21:55:46 +00006517 if( ssl->session )
6518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006520 /* RFC 7366 3.1: keep the EtM state */
6521 ssl->session_negotiate->encrypt_then_mac =
6522 ssl->session->encrypt_then_mac;
6523#endif
6524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 mbedtls_ssl_session_free( ssl->session );
6526 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006527 }
6528 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006529 ssl->session_negotiate = NULL;
6530
Paul Bakker0a597072012-09-25 21:55:46 +00006531 /*
6532 * Add cache entry
6533 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006534 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006535 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006536 resume == 0 )
6537 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006538 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006540 }
Paul Bakker0a597072012-09-25 21:55:46 +00006541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006543 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006544 ssl->handshake->flight != NULL )
6545 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006546 /* Cancel handshake timer */
6547 ssl_set_timer( ssl, 0 );
6548
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006549 /* Keep last flight around in case we need to resend it:
6550 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006552 }
6553 else
6554#endif
6555 ssl_handshake_wrapup_free_hs_transform( ssl );
6556
Paul Bakker48916f92012-09-16 19:57:18 +00006557 ssl->state++;
6558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006560}
6561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006563{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006564 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006567
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006568 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006569
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006570 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006571
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006572 /*
6573 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6574 * may define some other value. Currently (early 2016), no defined
6575 * ciphersuite does this (and this is unlikely to change as activity has
6576 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006581 ssl->verify_data_len = hash_len;
6582 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006583#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006584
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6587 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
6589 /*
6590 * In case of session resuming, invert the client and server
6591 * ChangeCipherSpec messages order.
6592 */
Paul Bakker0a597072012-09-25 21:55:46 +00006593 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006596 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006600 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006602#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 }
6604 else
6605 ssl->state++;
6606
Paul Bakker48916f92012-09-16 19:57:18 +00006607 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006608 * Switch to our negotiated transform and session parameters for outbound
6609 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006614 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006615 {
6616 unsigned char i;
6617
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006618 /* Remember current epoch settings for resending */
6619 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006620 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006621
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006622 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006623 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006624
6625 /* Increment epoch */
6626 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006627 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006628 break;
6629
6630 /* The loop goes to its end iff the counter is wrapping */
6631 if( i == 0 )
6632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6634 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006635 }
6636 }
6637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006639 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006640
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006641 ssl->transform_out = ssl->transform_negotiate;
6642 ssl->session_out = ssl->session_negotiate;
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6645 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6650 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006651 }
6652 }
6653#endif
6654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006656 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006658#endif
6659
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006660 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006662 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006663 return( ret );
6664 }
6665
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006666#if defined(MBEDTLS_SSL_PROTO_DTLS)
6667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6668 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6669 {
6670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6671 return( ret );
6672 }
6673#endif
6674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006676
6677 return( 0 );
6678}
6679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006681#define SSL_MAX_HASH_LEN 36
6682#else
6683#define SSL_MAX_HASH_LEN 12
6684#endif
6685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006687{
Paul Bakker23986e52011-04-24 08:57:21 +00006688 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006689 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006690 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006693
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006694 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006695
Hanno Becker327c93b2018-08-15 13:56:18 +01006696 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006699 return( ret );
6700 }
6701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6706 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006708 }
6709
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006710 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#if defined(MBEDTLS_SSL_PROTO_SSL3)
6712 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006713 hash_len = 36;
6714 else
6715#endif
6716 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6719 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6723 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006725 }
6726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006728 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6732 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006734 }
6735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006737 ssl->verify_data_len = hash_len;
6738 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006739#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006740
Paul Bakker0a597072012-09-25 21:55:46 +00006741 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006744 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006748 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006750#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 }
6752 else
6753 ssl->state++;
6754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006758#endif
6759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006761
6762 return( 0 );
6763}
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6770 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6771 mbedtls_md5_init( &handshake->fin_md5 );
6772 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006773 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6774 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6777#if defined(MBEDTLS_SHA256_C)
6778 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006779 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#if defined(MBEDTLS_SHA512_C)
6782 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006783 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006786
6787 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006788
6789#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6790 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6791 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6792#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794#if defined(MBEDTLS_DHM_C)
6795 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797#if defined(MBEDTLS_ECDH_C)
6798 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006799#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006800#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006801 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006802#if defined(MBEDTLS_SSL_CLI_C)
6803 handshake->ecjpake_cache = NULL;
6804 handshake->ecjpake_cache_len = 0;
6805#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006806#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006807
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006808#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006809 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006810#endif
6811
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006812#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6813 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6814#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006815}
6816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6822 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824 mbedtls_md_init( &transform->md_ctx_enc );
6825 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006826}
6827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006831}
6832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006834{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006835 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006836 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006838 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006840 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006841 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006842
6843 /*
6844 * Either the pointers are now NULL or cleared properly and can be freed.
6845 * Now allocate missing structures.
6846 */
6847 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006848 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006849 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006850 }
Paul Bakker48916f92012-09-16 19:57:18 +00006851
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006852 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006853 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006854 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006855 }
Paul Bakker48916f92012-09-16 19:57:18 +00006856
Paul Bakker82788fb2014-10-20 13:59:19 +02006857 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006858 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006859 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006860 }
Paul Bakker48916f92012-09-16 19:57:18 +00006861
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006862 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006863 if( ssl->handshake == NULL ||
6864 ssl->transform_negotiate == NULL ||
6865 ssl->session_negotiate == NULL )
6866 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869 mbedtls_free( ssl->handshake );
6870 mbedtls_free( ssl->transform_negotiate );
6871 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006872
6873 ssl->handshake = NULL;
6874 ssl->transform_negotiate = NULL;
6875 ssl->session_negotiate = NULL;
6876
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006877 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006878 }
6879
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006880 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006882 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006883 ssl_handshake_params_init( ssl->handshake );
6884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6887 {
6888 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006889
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006890 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6891 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6892 else
6893 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006894
6895 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006896 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006897#endif
6898
Paul Bakker48916f92012-09-16 19:57:18 +00006899 return( 0 );
6900}
6901
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006902#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006903/* Dummy cookie callbacks for defaults */
6904static int ssl_cookie_write_dummy( void *ctx,
6905 unsigned char **p, unsigned char *end,
6906 const unsigned char *cli_id, size_t cli_id_len )
6907{
6908 ((void) ctx);
6909 ((void) p);
6910 ((void) end);
6911 ((void) cli_id);
6912 ((void) cli_id_len);
6913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006915}
6916
6917static int ssl_cookie_check_dummy( void *ctx,
6918 const unsigned char *cookie, size_t cookie_len,
6919 const unsigned char *cli_id, size_t cli_id_len )
6920{
6921 ((void) ctx);
6922 ((void) cookie);
6923 ((void) cookie_len);
6924 ((void) cli_id);
6925 ((void) cli_id_len);
6926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006928}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006929#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006930
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006931/* Once ssl->out_hdr as the address of the beginning of the
6932 * next outgoing record is set, deduce the other pointers.
6933 *
6934 * Note: For TLS, we save the implicit record sequence number
6935 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6936 * and the caller has to make sure there's space for this.
6937 */
6938
6939static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6940 mbedtls_ssl_transform *transform )
6941{
6942#if defined(MBEDTLS_SSL_PROTO_DTLS)
6943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6944 {
6945 ssl->out_ctr = ssl->out_hdr + 3;
6946 ssl->out_len = ssl->out_hdr + 11;
6947 ssl->out_iv = ssl->out_hdr + 13;
6948 }
6949 else
6950#endif
6951 {
6952 ssl->out_ctr = ssl->out_hdr - 8;
6953 ssl->out_len = ssl->out_hdr + 3;
6954 ssl->out_iv = ssl->out_hdr + 5;
6955 }
6956
6957 /* Adjust out_msg to make space for explicit IV, if used. */
6958 if( transform != NULL &&
6959 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6960 {
6961 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6962 }
6963 else
6964 ssl->out_msg = ssl->out_iv;
6965}
6966
6967/* Once ssl->in_hdr as the address of the beginning of the
6968 * next incoming record is set, deduce the other pointers.
6969 *
6970 * Note: For TLS, we save the implicit record sequence number
6971 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6972 * and the caller has to make sure there's space for this.
6973 */
6974
6975static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6976 mbedtls_ssl_transform *transform )
6977{
6978#if defined(MBEDTLS_SSL_PROTO_DTLS)
6979 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6980 {
6981 ssl->in_ctr = ssl->in_hdr + 3;
6982 ssl->in_len = ssl->in_hdr + 11;
6983 ssl->in_iv = ssl->in_hdr + 13;
6984 }
6985 else
6986#endif
6987 {
6988 ssl->in_ctr = ssl->in_hdr - 8;
6989 ssl->in_len = ssl->in_hdr + 3;
6990 ssl->in_iv = ssl->in_hdr + 5;
6991 }
6992
6993 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6994 if( transform != NULL &&
6995 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6996 {
6997 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6998 }
6999 else
7000 ssl->in_msg = ssl->in_iv;
7001}
7002
Paul Bakker5121ce52009-01-03 21:22:43 +00007003/*
7004 * Initialize an SSL context
7005 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007006void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7007{
7008 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7009}
7010
7011/*
7012 * Setup an SSL context
7013 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007014
7015static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7016{
7017 /* Set the incoming and outgoing record pointers. */
7018#if defined(MBEDTLS_SSL_PROTO_DTLS)
7019 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7020 {
7021 ssl->out_hdr = ssl->out_buf;
7022 ssl->in_hdr = ssl->in_buf;
7023 }
7024 else
7025#endif /* MBEDTLS_SSL_PROTO_DTLS */
7026 {
7027 ssl->out_hdr = ssl->out_buf + 8;
7028 ssl->in_hdr = ssl->in_buf + 8;
7029 }
7030
7031 /* Derive other internal pointers. */
7032 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7033 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7034}
7035
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007036int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007037 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007038{
Paul Bakker48916f92012-09-16 19:57:18 +00007039 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007040
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007041 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007042
7043 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007044 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007045 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007046
7047 /* Set to NULL in case of an error condition */
7048 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007049
Angus Grattond8213d02016-05-25 20:56:48 +10007050 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7051 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007052 {
Angus Grattond8213d02016-05-25 20:56:48 +10007053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007054 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007055 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007056 }
7057
7058 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7059 if( ssl->out_buf == NULL )
7060 {
7061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007062 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007063 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007064 }
7065
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007066 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007067
Paul Bakker48916f92012-09-16 19:57:18 +00007068 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007069 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
7071 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007072
7073error:
7074 mbedtls_free( ssl->in_buf );
7075 mbedtls_free( ssl->out_buf );
7076
7077 ssl->conf = NULL;
7078
7079 ssl->in_buf = NULL;
7080 ssl->out_buf = NULL;
7081
7082 ssl->in_hdr = NULL;
7083 ssl->in_ctr = NULL;
7084 ssl->in_len = NULL;
7085 ssl->in_iv = NULL;
7086 ssl->in_msg = NULL;
7087
7088 ssl->out_hdr = NULL;
7089 ssl->out_ctr = NULL;
7090 ssl->out_len = NULL;
7091 ssl->out_iv = NULL;
7092 ssl->out_msg = NULL;
7093
k-stachowiak9f7798e2018-07-31 16:52:32 +02007094 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007095}
7096
7097/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007098 * Reset an initialized and used SSL context for re-use while retaining
7099 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007100 *
7101 * If partial is non-zero, keep data in the input buffer and client ID.
7102 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007103 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007104static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007105{
Paul Bakker48916f92012-09-16 19:57:18 +00007106 int ret;
7107
Hanno Becker7e772132018-08-10 12:38:21 +01007108#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7109 !defined(MBEDTLS_SSL_SRV_C)
7110 ((void) partial);
7111#endif
7112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007115 /* Cancel any possibly running timer */
7116 ssl_set_timer( ssl, 0 );
7117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118#if defined(MBEDTLS_SSL_RENEGOTIATION)
7119 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007120 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007121
7122 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7124 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007127
Paul Bakker7eb013f2011-10-06 12:37:39 +00007128 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007129 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007130
7131 ssl->in_msgtype = 0;
7132 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007134 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007135 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007138 ssl_dtls_replay_reset( ssl );
7139#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007140
7141 ssl->in_hslen = 0;
7142 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007143
7144 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007145
7146 ssl->out_msgtype = 0;
7147 ssl->out_msglen = 0;
7148 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7150 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007151 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007152#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007153
Hanno Becker19859472018-08-06 09:40:20 +01007154 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7155
Paul Bakker48916f92012-09-16 19:57:18 +00007156 ssl->transform_in = NULL;
7157 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007158
Hanno Becker78640902018-08-13 16:35:15 +01007159 ssl->session_in = NULL;
7160 ssl->session_out = NULL;
7161
Angus Grattond8213d02016-05-25 20:56:48 +10007162 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007163
7164#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007165 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007166#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7167 {
7168 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007169 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007170 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7173 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7176 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7179 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007180 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007181 }
7182#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007183
Paul Bakker48916f92012-09-16 19:57:18 +00007184 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 mbedtls_ssl_transform_free( ssl->transform );
7187 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007188 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007189 }
Paul Bakker48916f92012-09-16 19:57:18 +00007190
Paul Bakkerc0463502013-02-14 11:19:38 +01007191 if( ssl->session )
7192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 mbedtls_ssl_session_free( ssl->session );
7194 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007195 ssl->session = NULL;
7196 }
7197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007199 ssl->alpn_chosen = NULL;
7200#endif
7201
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007202#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007203#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007204 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007205#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007206 {
7207 mbedtls_free( ssl->cli_id );
7208 ssl->cli_id = NULL;
7209 ssl->cli_id_len = 0;
7210 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007211#endif
7212
Paul Bakker48916f92012-09-16 19:57:18 +00007213 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7214 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007215
7216 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007217}
7218
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007219/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007220 * Reset an initialized and used SSL context for re-use while retaining
7221 * all application-set variables, function pointers and data.
7222 */
7223int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7224{
7225 return( ssl_session_reset_int( ssl, 0 ) );
7226}
7227
7228/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007229 * SSL set accessors
7230 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007231void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007232{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007233 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007234}
7235
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007236void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007237{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007238 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007239}
7240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007242void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007243{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007244 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007245}
7246#endif
7247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007249void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007250{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007251 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007252}
7253#endif
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007256
Hanno Becker1841b0a2018-08-24 11:13:57 +01007257void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7258 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007259{
7260 ssl->disable_datagram_packing = !allow_packing;
7261}
7262
7263void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7264 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007266 conf->hs_timeout_min = min;
7267 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007268}
7269#endif
7270
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007271void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007272{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007273 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007274}
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007277void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007278 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007279 void *p_vrfy )
7280{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007281 conf->f_vrfy = f_vrfy;
7282 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007285
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007286void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007287 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007288 void *p_rng )
7289{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007290 conf->f_rng = f_rng;
7291 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007292}
7293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007294void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007295 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007296 void *p_dbg )
7297{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007298 conf->f_dbg = f_dbg;
7299 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007300}
7301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007303 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007304 mbedtls_ssl_send_t *f_send,
7305 mbedtls_ssl_recv_t *f_recv,
7306 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007307{
7308 ssl->p_bio = p_bio;
7309 ssl->f_send = f_send;
7310 ssl->f_recv = f_recv;
7311 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007312}
7313
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007314#if defined(MBEDTLS_SSL_PROTO_DTLS)
7315void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7316{
7317 ssl->mtu = mtu;
7318}
7319#endif
7320
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007321void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007322{
7323 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007324}
7325
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007326void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7327 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007328 mbedtls_ssl_set_timer_t *f_set_timer,
7329 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007330{
7331 ssl->p_timer = p_timer;
7332 ssl->f_set_timer = f_set_timer;
7333 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007334
7335 /* Make sure we start with no timer running */
7336 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007337}
7338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007340void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007341 void *p_cache,
7342 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7343 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007344{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007345 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007346 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007347 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351#if defined(MBEDTLS_SSL_CLI_C)
7352int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007353{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007354 int ret;
7355
7356 if( ssl == NULL ||
7357 session == NULL ||
7358 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007359 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007362 }
7363
7364 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7365 return( ret );
7366
Paul Bakker0a597072012-09-25 21:55:46 +00007367 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007368
7369 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007370}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007372
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007373void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007374 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007376 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7377 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7378 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7379 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007380}
7381
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007382void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007383 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007384 int major, int minor )
7385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007387 return;
7388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007390 return;
7391
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007392 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007393}
7394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007396void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007397 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007398{
7399 conf->cert_profile = profile;
7400}
7401
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007402/* Append a new keycert entry to a (possibly empty) list */
7403static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7404 mbedtls_x509_crt *cert,
7405 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007406{
niisato8ee24222018-06-25 19:05:48 +09007407 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007408
niisato8ee24222018-06-25 19:05:48 +09007409 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7410 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007411 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007412
niisato8ee24222018-06-25 19:05:48 +09007413 new_cert->cert = cert;
7414 new_cert->key = key;
7415 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007416
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007417 /* Update head is the list was null, else add to the end */
7418 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007419 {
niisato8ee24222018-06-25 19:05:48 +09007420 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007421 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007422 else
7423 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007424 mbedtls_ssl_key_cert *cur = *head;
7425 while( cur->next != NULL )
7426 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007427 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007428 }
7429
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007430 return( 0 );
7431}
7432
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007433int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007434 mbedtls_x509_crt *own_cert,
7435 mbedtls_pk_context *pk_key )
7436{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007437 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007438}
7439
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007440void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007441 mbedtls_x509_crt *ca_chain,
7442 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007443{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007444 conf->ca_chain = ca_chain;
7445 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007448
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007449#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7450int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7451 mbedtls_x509_crt *own_cert,
7452 mbedtls_pk_context *pk_key )
7453{
7454 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7455 own_cert, pk_key ) );
7456}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007457
7458void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7459 mbedtls_x509_crt *ca_chain,
7460 mbedtls_x509_crl *ca_crl )
7461{
7462 ssl->handshake->sni_ca_chain = ca_chain;
7463 ssl->handshake->sni_ca_crl = ca_crl;
7464}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007465
7466void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7467 int authmode )
7468{
7469 ssl->handshake->sni_authmode = authmode;
7470}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007471#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7472
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007473#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007474/*
7475 * Set EC J-PAKE password for current handshake
7476 */
7477int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7478 const unsigned char *pw,
7479 size_t pw_len )
7480{
7481 mbedtls_ecjpake_role role;
7482
Janos Follath8eb64132016-06-03 15:40:57 +01007483 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7485
7486 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7487 role = MBEDTLS_ECJPAKE_SERVER;
7488 else
7489 role = MBEDTLS_ECJPAKE_CLIENT;
7490
7491 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7492 role,
7493 MBEDTLS_MD_SHA256,
7494 MBEDTLS_ECP_DP_SECP256R1,
7495 pw, pw_len ) );
7496}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007497#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007500
7501static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7502{
7503 /* Remove reference to existing PSK, if any. */
7504#if defined(MBEDTLS_USE_PSA_CRYPTO)
7505 if( conf->psk_opaque != 0 )
7506 {
7507 /* The maintenance of the PSK key slot is the
7508 * user's responsibility. */
7509 conf->psk_opaque = 0;
7510 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007511 /* This and the following branch should never
7512 * be taken simultaenously as we maintain the
7513 * invariant that raw and opaque PSKs are never
7514 * configured simultaneously. As a safeguard,
7515 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007516#endif /* MBEDTLS_USE_PSA_CRYPTO */
7517 if( conf->psk != NULL )
7518 {
7519 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7520
7521 mbedtls_free( conf->psk );
7522 conf->psk = NULL;
7523 conf->psk_len = 0;
7524 }
7525
7526 /* Remove reference to PSK identity, if any. */
7527 if( conf->psk_identity != NULL )
7528 {
7529 mbedtls_free( conf->psk_identity );
7530 conf->psk_identity = NULL;
7531 conf->psk_identity_len = 0;
7532 }
7533}
7534
Hanno Becker7390c712018-11-15 13:33:04 +00007535/* This function assumes that PSK identity in the SSL config is unset.
7536 * It checks that the provided identity is well-formed and attempts
7537 * to make a copy of it in the SSL config.
7538 * On failure, the PSK identity in the config remains unset. */
7539static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7540 unsigned char const *psk_identity,
7541 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007542{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007543 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007544 if( psk_identity == NULL ||
7545 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007546 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007547 {
7548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7549 }
7550
Hanno Becker7390c712018-11-15 13:33:04 +00007551 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7552 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007553 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007554
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007555 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007556 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007557
7558 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007559}
7560
Hanno Becker7390c712018-11-15 13:33:04 +00007561int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7562 const unsigned char *psk, size_t psk_len,
7563 const unsigned char *psk_identity, size_t psk_identity_len )
7564{
7565 int ret;
7566 /* Remove opaque/raw PSK + PSK Identity */
7567 ssl_conf_remove_psk( conf );
7568
7569 /* Check and set raw PSK */
7570 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7572 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7573 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7574 conf->psk_len = psk_len;
7575 memcpy( conf->psk, psk, conf->psk_len );
7576
7577 /* Check and set PSK Identity */
7578 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7579 if( ret != 0 )
7580 ssl_conf_remove_psk( conf );
7581
7582 return( ret );
7583}
7584
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007585static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7586{
7587#if defined(MBEDTLS_USE_PSA_CRYPTO)
7588 if( ssl->handshake->psk_opaque != 0 )
7589 {
7590 ssl->handshake->psk_opaque = 0;
7591 }
7592 else
7593#endif /* MBEDTLS_USE_PSA_CRYPTO */
7594 if( ssl->handshake->psk != NULL )
7595 {
7596 mbedtls_platform_zeroize( ssl->handshake->psk,
7597 ssl->handshake->psk_len );
7598 mbedtls_free( ssl->handshake->psk );
7599 ssl->handshake->psk_len = 0;
7600 }
7601}
7602
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007603int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7604 const unsigned char *psk, size_t psk_len )
7605{
7606 if( psk == NULL || ssl->handshake == NULL )
7607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7608
7609 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7611
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007612 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007613
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007614 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007615 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007616
7617 ssl->handshake->psk_len = psk_len;
7618 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7619
7620 return( 0 );
7621}
7622
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007623#if defined(MBEDTLS_USE_PSA_CRYPTO)
7624int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007625 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007626 const unsigned char *psk_identity,
7627 size_t psk_identity_len )
7628{
Hanno Becker7390c712018-11-15 13:33:04 +00007629 int ret;
7630 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007631 ssl_conf_remove_psk( conf );
7632
Hanno Becker7390c712018-11-15 13:33:04 +00007633 /* Check and set opaque PSK */
7634 if( psk_slot == 0 )
7635 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007636 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007637
7638 /* Check and set PSK Identity */
7639 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7640 psk_identity_len );
7641 if( ret != 0 )
7642 ssl_conf_remove_psk( conf );
7643
7644 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007645}
7646
7647int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007648 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007649{
7650 if( psk_slot == 0 || ssl->handshake == NULL )
7651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7652
7653 ssl_remove_psk( ssl );
7654 ssl->handshake->psk_opaque = psk_slot;
7655 return( 0 );
7656}
7657#endif /* MBEDTLS_USE_PSA_CRYPTO */
7658
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007659void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007661 size_t),
7662 void *p_psk )
7663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007664 conf->f_psk = f_psk;
7665 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007668
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007669#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007670
7671#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007672int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007673{
7674 int ret;
7675
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007676 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7677 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7678 {
7679 mbedtls_mpi_free( &conf->dhm_P );
7680 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007681 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007683
7684 return( 0 );
7685}
Hanno Becker470a8c42017-10-04 15:28:46 +01007686#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007687
Hanno Beckera90658f2017-10-04 15:29:08 +01007688int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7689 const unsigned char *dhm_P, size_t P_len,
7690 const unsigned char *dhm_G, size_t G_len )
7691{
7692 int ret;
7693
7694 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7695 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7696 {
7697 mbedtls_mpi_free( &conf->dhm_P );
7698 mbedtls_mpi_free( &conf->dhm_G );
7699 return( ret );
7700 }
7701
7702 return( 0 );
7703}
Paul Bakker5121ce52009-01-03 21:22:43 +00007704
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007705int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007706{
7707 int ret;
7708
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007709 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7710 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7711 {
7712 mbedtls_mpi_free( &conf->dhm_P );
7713 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007714 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007715 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007716
7717 return( 0 );
7718}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007719#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007720
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007721#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7722/*
7723 * Set the minimum length for Diffie-Hellman parameters
7724 */
7725void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7726 unsigned int bitlen )
7727{
7728 conf->dhm_min_bitlen = bitlen;
7729}
7730#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7731
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007732#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007733/*
7734 * Set allowed/preferred hashes for handshake signatures
7735 */
7736void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7737 const int *hashes )
7738{
7739 conf->sig_hashes = hashes;
7740}
Hanno Becker947194e2017-04-07 13:25:49 +01007741#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007742
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007743#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007744/*
7745 * Set the allowed elliptic curves
7746 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007747void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007748 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007749{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007750 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007751}
Hanno Becker947194e2017-04-07 13:25:49 +01007752#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007753
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007754#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007756{
Hanno Becker947194e2017-04-07 13:25:49 +01007757 /* Initialize to suppress unnecessary compiler warning */
7758 size_t hostname_len = 0;
7759
7760 /* Check if new hostname is valid before
7761 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007762 if( hostname != NULL )
7763 {
7764 hostname_len = strlen( hostname );
7765
7766 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7768 }
7769
7770 /* Now it's clear that we will overwrite the old hostname,
7771 * so we can free it safely */
7772
7773 if( ssl->hostname != NULL )
7774 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007775 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007776 mbedtls_free( ssl->hostname );
7777 }
7778
7779 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007780
Paul Bakker5121ce52009-01-03 21:22:43 +00007781 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007782 {
7783 ssl->hostname = NULL;
7784 }
7785 else
7786 {
7787 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007788 if( ssl->hostname == NULL )
7789 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007790
Hanno Becker947194e2017-04-07 13:25:49 +01007791 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007792
Hanno Becker947194e2017-04-07 13:25:49 +01007793 ssl->hostname[hostname_len] = '\0';
7794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007795
7796 return( 0 );
7797}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007798#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007799
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007800#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007801void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007803 const unsigned char *, size_t),
7804 void *p_sni )
7805{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007806 conf->f_sni = f_sni;
7807 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007812int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007813{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007814 size_t cur_len, tot_len;
7815 const char **p;
7816
7817 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007818 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7819 * MUST NOT be truncated."
7820 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007821 */
7822 tot_len = 0;
7823 for( p = protos; *p != NULL; p++ )
7824 {
7825 cur_len = strlen( *p );
7826 tot_len += cur_len;
7827
7828 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007830 }
7831
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007832 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007833
7834 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007835}
7836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007838{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007839 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007842
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007843void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007844{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007845 conf->max_major_ver = major;
7846 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007847}
7848
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007849void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007850{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007851 conf->min_major_ver = major;
7852 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007853}
7854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007856void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007857{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007858 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007859}
7860#endif
7861
Janos Follath088ce432017-04-10 12:42:31 +01007862#if defined(MBEDTLS_SSL_SRV_C)
7863void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7864 char cert_req_ca_list )
7865{
7866 conf->cert_req_ca_list = cert_req_ca_list;
7867}
7868#endif
7869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007871void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007872{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007873 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007874}
7875#endif
7876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007878void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007879{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007880 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007881}
7882#endif
7883
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007884#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007885void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007886{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007887 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007888}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007889#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007892int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007895 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007898 }
7899
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007900 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007901
7902 return( 0 );
7903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007907void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007908{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007909 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007914void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007915{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007916 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007917}
7918#endif
7919
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007920void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007923}
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007926void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007928 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007929}
7930
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007931void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007932{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007933 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007934}
7935
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007936void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007937 const unsigned char period[8] )
7938{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007939 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007944#if defined(MBEDTLS_SSL_CLI_C)
7945void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007946{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007947 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007948}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007949#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007950
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007951#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007952void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7953 mbedtls_ssl_ticket_write_t *f_ticket_write,
7954 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7955 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007956{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007957 conf->f_ticket_write = f_ticket_write;
7958 conf->f_ticket_parse = f_ticket_parse;
7959 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007960}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007963
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007964#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7965void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7966 mbedtls_ssl_export_keys_t *f_export_keys,
7967 void *p_export_keys )
7968{
7969 conf->f_export_keys = f_export_keys;
7970 conf->p_export_keys = p_export_keys;
7971}
7972#endif
7973
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007974#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007975void mbedtls_ssl_conf_async_private_cb(
7976 mbedtls_ssl_config *conf,
7977 mbedtls_ssl_async_sign_t *f_async_sign,
7978 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7979 mbedtls_ssl_async_resume_t *f_async_resume,
7980 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007981 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007982{
7983 conf->f_async_sign_start = f_async_sign;
7984 conf->f_async_decrypt_start = f_async_decrypt;
7985 conf->f_async_resume = f_async_resume;
7986 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007987 conf->p_async_config_data = async_config_data;
7988}
7989
Gilles Peskine8f97af72018-04-26 11:46:10 +02007990void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7991{
7992 return( conf->p_async_config_data );
7993}
7994
Gilles Peskine1febfef2018-04-30 11:54:39 +02007995void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007996{
7997 if( ssl->handshake == NULL )
7998 return( NULL );
7999 else
8000 return( ssl->handshake->user_async_ctx );
8001}
8002
Gilles Peskine1febfef2018-04-30 11:54:39 +02008003void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008004 void *ctx )
8005{
8006 if( ssl->handshake != NULL )
8007 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008008}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008009#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008010
Paul Bakker5121ce52009-01-03 21:22:43 +00008011/*
8012 * SSL get accessors
8013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008015{
8016 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8017}
8018
Hanno Becker8b170a02017-10-10 11:51:19 +01008019int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8020{
8021 /*
8022 * Case A: We're currently holding back
8023 * a message for further processing.
8024 */
8025
8026 if( ssl->keep_current_message == 1 )
8027 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008028 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008029 return( 1 );
8030 }
8031
8032 /*
8033 * Case B: Further records are pending in the current datagram.
8034 */
8035
8036#if defined(MBEDTLS_SSL_PROTO_DTLS)
8037 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8038 ssl->in_left > ssl->next_record_offset )
8039 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008041 return( 1 );
8042 }
8043#endif /* MBEDTLS_SSL_PROTO_DTLS */
8044
8045 /*
8046 * Case C: A handshake message is being processed.
8047 */
8048
Hanno Becker8b170a02017-10-10 11:51:19 +01008049 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8050 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008052 return( 1 );
8053 }
8054
8055 /*
8056 * Case D: An application data message is being processed
8057 */
8058 if( ssl->in_offt != NULL )
8059 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008061 return( 1 );
8062 }
8063
8064 /*
8065 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008066 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008067 * we implement support for multiple alerts in single records.
8068 */
8069
8070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8071 return( 0 );
8072}
8073
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008074uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008075{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008076 if( ssl->session != NULL )
8077 return( ssl->session->verify_result );
8078
8079 if( ssl->session_negotiate != NULL )
8080 return( ssl->session_negotiate->verify_result );
8081
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008082 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008083}
8084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008086{
Paul Bakker926c8e42013-03-06 10:23:34 +01008087 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008088 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008091}
8092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008096 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008097 {
8098 switch( ssl->minor_ver )
8099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008101 return( "DTLSv1.0" );
8102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008103 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008104 return( "DTLSv1.2" );
8105
8106 default:
8107 return( "unknown (DTLS)" );
8108 }
8109 }
8110#endif
8111
Paul Bakker43ca69c2011-01-15 17:35:19 +00008112 switch( ssl->minor_ver )
8113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008115 return( "SSLv3.0" );
8116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008118 return( "TLSv1.0" );
8119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008121 return( "TLSv1.1" );
8122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008124 return( "TLSv1.2" );
8125
Paul Bakker43ca69c2011-01-15 17:35:19 +00008126 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008127 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008128 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008129}
8130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008132{
Hanno Becker3136ede2018-08-17 15:28:19 +01008133 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008135 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008136
Hanno Becker78640902018-08-13 16:35:15 +01008137 if( transform == NULL )
8138 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140#if defined(MBEDTLS_ZLIB_SUPPORT)
8141 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8142 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008143#endif
8144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 case MBEDTLS_MODE_GCM:
8148 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008149 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008151 transform_expansion = transform->minlen;
8152 break;
8153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008155
8156 block_size = mbedtls_cipher_get_block_size(
8157 &transform->cipher_ctx_enc );
8158
Hanno Becker3136ede2018-08-17 15:28:19 +01008159 /* Expansion due to the addition of the MAC. */
8160 transform_expansion += transform->maclen;
8161
8162 /* Expansion due to the addition of CBC padding;
8163 * Theoretically up to 256 bytes, but we never use
8164 * more than the block size of the underlying cipher. */
8165 transform_expansion += block_size;
8166
8167 /* For TLS 1.1 or higher, an explicit IV is added
8168 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008169#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8170 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008171 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008172#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008173
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008174 break;
8175
8176 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008179 }
8180
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008181 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008182}
8183
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008184#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8185size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8186{
8187 size_t max_len;
8188
8189 /*
8190 * Assume mfl_code is correct since it was checked when set
8191 */
Angus Grattond8213d02016-05-25 20:56:48 +10008192 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008193
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008194 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008195 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008196 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008197 {
Angus Grattond8213d02016-05-25 20:56:48 +10008198 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008199 }
8200
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008201 /* During a handshake, use the value being negotiated */
8202 if( ssl->session_negotiate != NULL &&
8203 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8204 {
8205 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8206 }
8207
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008208 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008209}
8210#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8211
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008212#if defined(MBEDTLS_SSL_PROTO_DTLS)
8213static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8214{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008215 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8216 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8217 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8218 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8219 return ( 0 );
8220
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008221 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8222 return( ssl->mtu );
8223
8224 if( ssl->mtu == 0 )
8225 return( ssl->handshake->mtu );
8226
8227 return( ssl->mtu < ssl->handshake->mtu ?
8228 ssl->mtu : ssl->handshake->mtu );
8229}
8230#endif /* MBEDTLS_SSL_PROTO_DTLS */
8231
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008232int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8233{
8234 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8235
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008236#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8237 !defined(MBEDTLS_SSL_PROTO_DTLS)
8238 (void) ssl;
8239#endif
8240
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008241#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8242 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8243
8244 if( max_len > mfl )
8245 max_len = mfl;
8246#endif
8247
8248#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008249 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008250 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008251 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008252 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8253 const size_t overhead = (size_t) ret;
8254
8255 if( ret < 0 )
8256 return( ret );
8257
8258 if( mtu <= overhead )
8259 {
8260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8261 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8262 }
8263
8264 if( max_len > mtu - overhead )
8265 max_len = mtu - overhead;
8266 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008267#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008268
Hanno Becker0defedb2018-08-10 12:35:02 +01008269#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8270 !defined(MBEDTLS_SSL_PROTO_DTLS)
8271 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008272#endif
8273
8274 return( (int) max_len );
8275}
8276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277#if defined(MBEDTLS_X509_CRT_PARSE_C)
8278const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008279{
8280 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008281 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008282
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008283 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#if defined(MBEDTLS_SSL_CLI_C)
8288int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008289{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008290 if( ssl == NULL ||
8291 dst == NULL ||
8292 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008293 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008296 }
8297
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008298 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008301
Paul Bakker5121ce52009-01-03 21:22:43 +00008302/*
Paul Bakker1961b702013-01-25 14:49:24 +01008303 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008308
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008309 if( ssl == NULL || ssl->conf == NULL )
8310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008313 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008315#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008317 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008319#endif
8320
Paul Bakker1961b702013-01-25 14:49:24 +01008321 return( ret );
8322}
8323
8324/*
8325 * Perform the SSL handshake
8326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008328{
8329 int ret = 0;
8330
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008331 if( ssl == NULL || ssl->conf == NULL )
8332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008339
8340 if( ret != 0 )
8341 break;
8342 }
8343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008345
8346 return( ret );
8347}
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_RENEGOTIATION)
8350#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008351/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008352 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008355{
8356 int ret;
8357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008359
8360 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8362 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008363
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008364 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008365 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008367 return( ret );
8368 }
8369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008371
8372 return( 0 );
8373}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008375
8376/*
8377 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 * - any side: calling mbedtls_ssl_renegotiate(),
8379 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8380 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008381 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008382 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008386{
8387 int ret;
8388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008390
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008391 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8392 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008393
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008394 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8395 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008397 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008399 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008400 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008401 ssl->handshake->out_msg_seq = 1;
8402 else
8403 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008404 }
8405#endif
8406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8408 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008413 return( ret );
8414 }
8415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008417
8418 return( 0 );
8419}
8420
8421/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008422 * Renegotiate current connection on client,
8423 * or request renegotiation on server
8424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008428
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008429 if( ssl == NULL || ssl->conf == NULL )
8430 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008433 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008434 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008440
8441 /* Did we already try/start sending HelloRequest? */
8442 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008444
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008445 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008450 /*
8451 * On client, either start the renegotiation process or,
8452 * if already in progress, continue the handshake
8453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008458
8459 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008462 return( ret );
8463 }
8464 }
8465 else
8466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008470 return( ret );
8471 }
8472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008474
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008475 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008476}
8477
8478/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008479 * Check record counters and renegotiate if they're above the limit.
8480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008482{
Andres AG2196c7f2016-12-15 17:01:16 +00008483 size_t ep_len = ssl_ep_len( ssl );
8484 int in_ctr_cmp;
8485 int out_ctr_cmp;
8486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8488 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008489 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008490 {
8491 return( 0 );
8492 }
8493
Andres AG2196c7f2016-12-15 17:01:16 +00008494 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8495 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008496 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008497 ssl->conf->renego_period + ep_len, 8 - ep_len );
8498
8499 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008500 {
8501 return( 0 );
8502 }
8503
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008508
8509/*
8510 * Receive application data decrypted from the SSL layer
8511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008513{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008514 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008515 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008516
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008517 if( ssl == NULL || ssl->conf == NULL )
8518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008523 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008526 return( ret );
8527
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008528 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008530 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008531 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008532 return( ret );
8533 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008534 }
8535#endif
8536
Hanno Becker4a810fb2017-05-24 16:27:30 +01008537 /*
8538 * Check if renegotiation is necessary and/or handshake is
8539 * in process. If yes, perform/continue, and fall through
8540 * if an unexpected packet is received while the client
8541 * is waiting for the ServerHello.
8542 *
8543 * (There is no equivalent to the last condition on
8544 * the server-side as it is not treated as within
8545 * a handshake while waiting for the ClientHello
8546 * after a renegotiation request.)
8547 */
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008550 ret = ssl_check_ctr_renegotiate( ssl );
8551 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8552 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008555 return( ret );
8556 }
8557#endif
8558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008562 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8563 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008566 return( ret );
8567 }
8568 }
8569
Hanno Beckere41158b2017-10-23 13:30:32 +01008570 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008571 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008572 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008573 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008574 if( ssl->f_get_timer != NULL &&
8575 ssl->f_get_timer( ssl->p_timer ) == -1 )
8576 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008577 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008578 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008579
Hanno Becker327c93b2018-08-15 13:56:18 +01008580 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008581 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008582 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8583 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008584
Hanno Becker4a810fb2017-05-24 16:27:30 +01008585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8586 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008587 }
8588
8589 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008591 {
8592 /*
8593 * OpenSSL sends empty messages to randomize the IV
8594 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008595 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008598 return( 0 );
8599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008601 return( ret );
8602 }
8603 }
8604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008608
Hanno Becker4a810fb2017-05-24 16:27:30 +01008609 /*
8610 * - For client-side, expect SERVER_HELLO_REQUEST.
8611 * - For server-side, expect CLIENT_HELLO.
8612 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8613 */
8614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008616 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008618 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008621
8622 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008624 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008625 {
8626 continue;
8627 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008630 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008631#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008632
Hanno Becker4a810fb2017-05-24 16:27:30 +01008633#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008634 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008638
8639 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008640#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008641 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008642 {
8643 continue;
8644 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008647 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008648#endif /* MBEDTLS_SSL_SRV_C */
8649
Hanno Becker21df7f92017-10-17 11:03:26 +01008650#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008651 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008652 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8653 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8654 ssl->conf->allow_legacy_renegotiation ==
8655 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8656 {
8657 /*
8658 * Accept renegotiation request
8659 */
Paul Bakker48916f92012-09-16 19:57:18 +00008660
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008661 /* DTLS clients need to know renego is server-initiated */
8662#if defined(MBEDTLS_SSL_PROTO_DTLS)
8663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8664 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8665 {
8666 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8667 }
8668#endif
8669 ret = ssl_start_renegotiation( ssl );
8670 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8671 ret != 0 )
8672 {
8673 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8674 return( ret );
8675 }
8676 }
8677 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008678#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008679 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008680 /*
8681 * Refuse renegotiation
8682 */
8683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#if defined(MBEDTLS_SSL_PROTO_SSL3)
8687 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008688 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008689 /* SSLv3 does not have a "no_renegotiation" warning, so
8690 we send a fatal alert and abort the connection. */
8691 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8692 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8693 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008694 }
8695 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8697#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8698 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8699 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8702 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8703 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008704 {
8705 return( ret );
8706 }
Paul Bakker48916f92012-09-16 19:57:18 +00008707 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008708 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8710 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008714 }
Paul Bakker48916f92012-09-16 19:57:18 +00008715 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008716
Hanno Becker90333da2017-10-10 11:27:13 +01008717 /* At this point, we don't know whether the renegotiation has been
8718 * completed or not. The cases to consider are the following:
8719 * 1) The renegotiation is complete. In this case, no new record
8720 * has been read yet.
8721 * 2) The renegotiation is incomplete because the client received
8722 * an application data record while awaiting the ServerHello.
8723 * 3) The renegotiation is incomplete because the client received
8724 * a non-handshake, non-application data message while awaiting
8725 * the ServerHello.
8726 * In each of these case, looping will be the proper action:
8727 * - For 1), the next iteration will read a new record and check
8728 * if it's application data.
8729 * - For 2), the loop condition isn't satisfied as application data
8730 * is present, hence continue is the same as break
8731 * - For 3), the loop condition is satisfied and read_record
8732 * will re-deliver the message that was held back by the client
8733 * when expecting the ServerHello.
8734 */
8735 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008736 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008737#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008739 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008740 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008741 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008742 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008745 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008747 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008748 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008749 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8753 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008756 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008757 }
8758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8762 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008763 }
8764
8765 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008766
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008767 /* We're going to return something now, cancel timer,
8768 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008770 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008771
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008772#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008773 /* If we requested renego but received AppData, resend HelloRequest.
8774 * Do it now, after setting in_offt, to avoid taking this branch
8775 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008777 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008779 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008780 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008783 return( ret );
8784 }
8785 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008787#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008788 }
8789
8790 n = ( len < ssl->in_msglen )
8791 ? len : ssl->in_msglen;
8792
8793 memcpy( buf, ssl->in_offt, n );
8794 ssl->in_msglen -= n;
8795
8796 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008797 {
8798 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008799 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008800 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008801 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008802 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008803 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008804 /* more data available */
8805 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008806 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008809
Paul Bakker23986e52011-04-24 08:57:21 +00008810 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008811}
8812
8813/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008814 * Send application data to be encrypted by the SSL layer, taking care of max
8815 * fragment length and buffer size.
8816 *
8817 * According to RFC 5246 Section 6.2.1:
8818 *
8819 * Zero-length fragments of Application data MAY be sent as they are
8820 * potentially useful as a traffic analysis countermeasure.
8821 *
8822 * Therefore, it is possible that the input message length is 0 and the
8823 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008824 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008825static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008826 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008827{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008828 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8829 const size_t max_len = (size_t) ret;
8830
8831 if( ret < 0 )
8832 {
8833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8834 return( ret );
8835 }
8836
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008837 if( len > max_len )
8838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008840 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008843 "maximum fragment length: %d > %d",
8844 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008846 }
8847 else
8848#endif
8849 len = max_len;
8850 }
Paul Bakker887bd502011-06-08 13:10:54 +00008851
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 if( ssl->out_left != 0 )
8853 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008854 /*
8855 * The user has previously tried to send the data and
8856 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8857 * written. In this case, we expect the high-level write function
8858 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008863 return( ret );
8864 }
8865 }
Paul Bakker887bd502011-06-08 13:10:54 +00008866 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008867 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008868 /*
8869 * The user is trying to send a message the first time, so we need to
8870 * copy the data into the internal buffers and setup the data structure
8871 * to keep track of partial writes
8872 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008873 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008875 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008876
Hanno Becker67bc7c32018-08-06 11:33:50 +01008877 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008880 return( ret );
8881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008882 }
8883
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008884 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008885}
8886
8887/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008888 * Write application data, doing 1/n-1 splitting if necessary.
8889 *
8890 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008891 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008892 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008895static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008896 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008897{
8898 int ret;
8899
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008900 if( ssl->conf->cbc_record_splitting ==
8901 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008902 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8904 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8905 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008906 {
8907 return( ssl_write_real( ssl, buf, len ) );
8908 }
8909
8910 if( ssl->split_done == 0 )
8911 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008912 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008913 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008914 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008915 }
8916
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008917 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8918 return( ret );
8919 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008920
8921 return( ret + 1 );
8922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008924
8925/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008926 * Write application data (public-facing wrapper)
8927 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008928int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008929{
8930 int ret;
8931
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008933
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008934 if( ssl == NULL || ssl->conf == NULL )
8935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8936
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008937#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008938 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
8939 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008940 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008941 return( ret );
8942 }
8943#endif
8944
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008945 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008946 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008947 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008948 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02008949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008950 return( ret );
8951 }
8952 }
8953
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008954#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008955 ret = ssl_write_split( ssl, buf, len );
8956#else
8957 ret = ssl_write_real( ssl, buf, len );
8958#endif
8959
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008961
8962 return( ret );
8963}
8964
8965/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008966 * Notify the peer that the connection is being closed
8967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008969{
8970 int ret;
8971
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008972 if( ssl == NULL || ssl->conf == NULL )
8973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008976
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008977 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8983 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8984 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008987 return( ret );
8988 }
8989 }
8990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008992
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008993 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008994}
8995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008997{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008998 if( transform == NULL )
8999 return;
9000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009002 deflateEnd( &transform->ctx_deflate );
9003 inflateEnd( &transform->ctx_inflate );
9004#endif
9005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9007 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009 mbedtls_md_free( &transform->md_ctx_enc );
9010 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009011
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009012 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009013}
9014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015#if defined(MBEDTLS_X509_CRT_PARSE_C)
9016static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009019
9020 while( cur != NULL )
9021 {
9022 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009024 cur = next;
9025 }
9026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009028
Hanno Becker0271f962018-08-16 13:23:47 +01009029#if defined(MBEDTLS_SSL_PROTO_DTLS)
9030
9031static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9032{
9033 unsigned offset;
9034 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9035
9036 if( hs == NULL )
9037 return;
9038
Hanno Becker283f5ef2018-08-24 09:34:47 +01009039 ssl_free_buffered_record( ssl );
9040
Hanno Becker0271f962018-08-16 13:23:47 +01009041 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009042 ssl_buffering_free_slot( ssl, offset );
9043}
9044
9045static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9046 uint8_t slot )
9047{
9048 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9049 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009050
9051 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9052 return;
9053
Hanno Beckere605b192018-08-21 15:59:07 +01009054 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009055 {
Hanno Beckere605b192018-08-21 15:59:07 +01009056 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009057 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009058 mbedtls_free( hs_buf->data );
9059 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009060 }
9061}
9062
9063#endif /* MBEDTLS_SSL_PROTO_DTLS */
9064
Gilles Peskine9b562d52018-04-25 20:32:43 +02009065void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009066{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009067 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9068
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009069 if( handshake == NULL )
9070 return;
9071
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009072#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9073 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9074 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009075 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009076 handshake->async_in_progress = 0;
9077 }
9078#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9079
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009080#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9081 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9082 mbedtls_md5_free( &handshake->fin_md5 );
9083 mbedtls_sha1_free( &handshake->fin_sha1 );
9084#endif
9085#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9086#if defined(MBEDTLS_SHA256_C)
9087 mbedtls_sha256_free( &handshake->fin_sha256 );
9088#endif
9089#if defined(MBEDTLS_SHA512_C)
9090 mbedtls_sha512_free( &handshake->fin_sha512 );
9091#endif
9092#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#if defined(MBEDTLS_DHM_C)
9095 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097#if defined(MBEDTLS_ECDH_C)
9098 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009099#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009100#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009101 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009102#if defined(MBEDTLS_SSL_CLI_C)
9103 mbedtls_free( handshake->ecjpake_cache );
9104 handshake->ecjpake_cache = NULL;
9105 handshake->ecjpake_cache_len = 0;
9106#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009107#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009108
Janos Follath4ae5c292016-02-10 11:27:43 +00009109#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9110 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009111 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009113#endif
9114
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009115#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9116 if( handshake->psk != NULL )
9117 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009118 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009119 mbedtls_free( handshake->psk );
9120 }
9121#endif
9122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9124 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009125 /*
9126 * Free only the linked list wrapper, not the keys themselves
9127 * since the belong to the SNI callback
9128 */
9129 if( handshake->sni_key_cert != NULL )
9130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009132
9133 while( cur != NULL )
9134 {
9135 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009137 cur = next;
9138 }
9139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009141
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009142#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009143 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009144#endif
9145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146#if defined(MBEDTLS_SSL_PROTO_DTLS)
9147 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009148 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009149 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009150#endif
9151
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009152 mbedtls_platform_zeroize( handshake,
9153 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009154}
9155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009157{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009158 if( session == NULL )
9159 return;
9160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009162 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164 mbedtls_x509_crt_free( session->peer_cert );
9165 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009166 }
Paul Bakkered27a042013-04-18 22:46:23 +02009167#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009168
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009169#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009171#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009172
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009173 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009174}
9175
Paul Bakker5121ce52009-01-03 21:22:43 +00009176/*
9177 * Free an SSL context
9178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009180{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009181 if( ssl == NULL )
9182 return;
9183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009185
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009186 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009187 {
Angus Grattond8213d02016-05-25 20:56:48 +10009188 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009190 }
9191
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009192 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009193 {
Angus Grattond8213d02016-05-25 20:56:48 +10009194 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009196 }
9197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009199 if( ssl->compress_buf != NULL )
9200 {
Angus Grattond8213d02016-05-25 20:56:48 +10009201 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009203 }
9204#endif
9205
Paul Bakker48916f92012-09-16 19:57:18 +00009206 if( ssl->transform )
9207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208 mbedtls_ssl_transform_free( ssl->transform );
9209 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009210 }
9211
9212 if( ssl->handshake )
9213 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009214 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9216 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 mbedtls_free( ssl->handshake );
9219 mbedtls_free( ssl->transform_negotiate );
9220 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009221 }
9222
Paul Bakkerc0463502013-02-14 11:19:38 +01009223 if( ssl->session )
9224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225 mbedtls_ssl_session_free( ssl->session );
9226 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009227 }
9228
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009229#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009230 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009231 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009232 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009234 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9238 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9241 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009242 }
9243#endif
9244
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009245#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009247#endif
9248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009250
Paul Bakker86f04f42013-02-14 11:20:09 +01009251 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009252 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009253}
9254
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009255/*
9256 * Initialze mbedtls_ssl_config
9257 */
9258void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9259{
9260 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9261}
9262
Simon Butcherc97b6972015-12-27 23:48:17 +00009263#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009264static int ssl_preset_default_hashes[] = {
9265#if defined(MBEDTLS_SHA512_C)
9266 MBEDTLS_MD_SHA512,
9267 MBEDTLS_MD_SHA384,
9268#endif
9269#if defined(MBEDTLS_SHA256_C)
9270 MBEDTLS_MD_SHA256,
9271 MBEDTLS_MD_SHA224,
9272#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009273#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009274 MBEDTLS_MD_SHA1,
9275#endif
9276 MBEDTLS_MD_NONE
9277};
Simon Butcherc97b6972015-12-27 23:48:17 +00009278#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009279
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009280static int ssl_preset_suiteb_ciphersuites[] = {
9281 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9282 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9283 0
9284};
9285
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009286#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009287static int ssl_preset_suiteb_hashes[] = {
9288 MBEDTLS_MD_SHA256,
9289 MBEDTLS_MD_SHA384,
9290 MBEDTLS_MD_NONE
9291};
9292#endif
9293
9294#if defined(MBEDTLS_ECP_C)
9295static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9296 MBEDTLS_ECP_DP_SECP256R1,
9297 MBEDTLS_ECP_DP_SECP384R1,
9298 MBEDTLS_ECP_DP_NONE
9299};
9300#endif
9301
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009302/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009303 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009304 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009305int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009306 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009307{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009308#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009309 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009310#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009311
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009312 /* Use the functions here so that they are covered in tests,
9313 * but otherwise access member directly for efficiency */
9314 mbedtls_ssl_conf_endpoint( conf, endpoint );
9315 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009316
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009317 /*
9318 * Things that are common to all presets
9319 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009320#if defined(MBEDTLS_SSL_CLI_C)
9321 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9322 {
9323 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9324#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9325 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9326#endif
9327 }
9328#endif
9329
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009330#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009331 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009332#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009333
9334#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9335 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9336#endif
9337
9338#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9339 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9340#endif
9341
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009342#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9343 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9344#endif
9345
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009346#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009347 conf->f_cookie_write = ssl_cookie_write_dummy;
9348 conf->f_cookie_check = ssl_cookie_check_dummy;
9349#endif
9350
9351#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9352 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9353#endif
9354
Janos Follath088ce432017-04-10 12:42:31 +01009355#if defined(MBEDTLS_SSL_SRV_C)
9356 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9357#endif
9358
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009359#if defined(MBEDTLS_SSL_PROTO_DTLS)
9360 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9361 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9362#endif
9363
9364#if defined(MBEDTLS_SSL_RENEGOTIATION)
9365 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009366 memset( conf->renego_period, 0x00, 2 );
9367 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009368#endif
9369
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009370#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9371 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9372 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009373 const unsigned char dhm_p[] =
9374 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9375 const unsigned char dhm_g[] =
9376 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9377
Hanno Beckera90658f2017-10-04 15:29:08 +01009378 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9379 dhm_p, sizeof( dhm_p ),
9380 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009381 {
9382 return( ret );
9383 }
9384 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009385#endif
9386
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009387 /*
9388 * Preset-specific defaults
9389 */
9390 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009391 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009392 /*
9393 * NSA Suite B
9394 */
9395 case MBEDTLS_SSL_PRESET_SUITEB:
9396 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9397 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9398 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9399 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9400
9401 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9402 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9403 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9404 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9405 ssl_preset_suiteb_ciphersuites;
9406
9407#if defined(MBEDTLS_X509_CRT_PARSE_C)
9408 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009409#endif
9410
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009411#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009412 conf->sig_hashes = ssl_preset_suiteb_hashes;
9413#endif
9414
9415#if defined(MBEDTLS_ECP_C)
9416 conf->curve_list = ssl_preset_suiteb_curves;
9417#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009418 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009419
9420 /*
9421 * Default
9422 */
9423 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009424 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9425 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9426 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9427 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9428 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9429 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9430 MBEDTLS_SSL_MIN_MINOR_VERSION :
9431 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009432 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9433 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9434
9435#if defined(MBEDTLS_SSL_PROTO_DTLS)
9436 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9437 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9438#endif
9439
9440 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9441 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9442 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9443 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9444 mbedtls_ssl_list_ciphersuites();
9445
9446#if defined(MBEDTLS_X509_CRT_PARSE_C)
9447 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9448#endif
9449
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009450#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009451 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009452#endif
9453
9454#if defined(MBEDTLS_ECP_C)
9455 conf->curve_list = mbedtls_ecp_grp_id_list();
9456#endif
9457
9458#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9459 conf->dhm_min_bitlen = 1024;
9460#endif
9461 }
9462
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009463 return( 0 );
9464}
9465
9466/*
9467 * Free mbedtls_ssl_config
9468 */
9469void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9470{
9471#if defined(MBEDTLS_DHM_C)
9472 mbedtls_mpi_free( &conf->dhm_P );
9473 mbedtls_mpi_free( &conf->dhm_G );
9474#endif
9475
9476#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9477 if( conf->psk != NULL )
9478 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009479 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009480 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009481 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009482 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009483 }
9484
9485 if( conf->psk_identity != NULL )
9486 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009487 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009488 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009489 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009490 conf->psk_identity_len = 0;
9491 }
9492#endif
9493
9494#if defined(MBEDTLS_X509_CRT_PARSE_C)
9495 ssl_key_cert_free( conf->key_cert );
9496#endif
9497
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009498 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009499}
9500
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009501#if defined(MBEDTLS_PK_C) && \
9502 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009503/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#if defined(MBEDTLS_RSA_C)
9509 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9510 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_ECDSA_C)
9513 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9514 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009517}
9518
Hanno Becker7e5437a2017-04-28 17:15:26 +01009519unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9520{
9521 switch( type ) {
9522 case MBEDTLS_PK_RSA:
9523 return( MBEDTLS_SSL_SIG_RSA );
9524 case MBEDTLS_PK_ECDSA:
9525 case MBEDTLS_PK_ECKEY:
9526 return( MBEDTLS_SSL_SIG_ECDSA );
9527 default:
9528 return( MBEDTLS_SSL_SIG_ANON );
9529 }
9530}
9531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009533{
9534 switch( sig )
9535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536#if defined(MBEDTLS_RSA_C)
9537 case MBEDTLS_SSL_SIG_RSA:
9538 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540#if defined(MBEDTLS_ECDSA_C)
9541 case MBEDTLS_SSL_SIG_ECDSA:
9542 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009543#endif
9544 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009546 }
9547}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009548#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009549
Hanno Becker7e5437a2017-04-28 17:15:26 +01009550#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9551 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9552
9553/* Find an entry in a signature-hash set matching a given hash algorithm. */
9554mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9555 mbedtls_pk_type_t sig_alg )
9556{
9557 switch( sig_alg )
9558 {
9559 case MBEDTLS_PK_RSA:
9560 return( set->rsa );
9561 case MBEDTLS_PK_ECDSA:
9562 return( set->ecdsa );
9563 default:
9564 return( MBEDTLS_MD_NONE );
9565 }
9566}
9567
9568/* Add a signature-hash-pair to a signature-hash set */
9569void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9570 mbedtls_pk_type_t sig_alg,
9571 mbedtls_md_type_t md_alg )
9572{
9573 switch( sig_alg )
9574 {
9575 case MBEDTLS_PK_RSA:
9576 if( set->rsa == MBEDTLS_MD_NONE )
9577 set->rsa = md_alg;
9578 break;
9579
9580 case MBEDTLS_PK_ECDSA:
9581 if( set->ecdsa == MBEDTLS_MD_NONE )
9582 set->ecdsa = md_alg;
9583 break;
9584
9585 default:
9586 break;
9587 }
9588}
9589
9590/* Allow exactly one hash algorithm for each signature. */
9591void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9592 mbedtls_md_type_t md_alg )
9593{
9594 set->rsa = md_alg;
9595 set->ecdsa = md_alg;
9596}
9597
9598#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9599 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9600
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009601/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009602 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009605{
9606 switch( hash )
9607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608#if defined(MBEDTLS_MD5_C)
9609 case MBEDTLS_SSL_HASH_MD5:
9610 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009611#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612#if defined(MBEDTLS_SHA1_C)
9613 case MBEDTLS_SSL_HASH_SHA1:
9614 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#if defined(MBEDTLS_SHA256_C)
9617 case MBEDTLS_SSL_HASH_SHA224:
9618 return( MBEDTLS_MD_SHA224 );
9619 case MBEDTLS_SSL_HASH_SHA256:
9620 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622#if defined(MBEDTLS_SHA512_C)
9623 case MBEDTLS_SSL_HASH_SHA384:
9624 return( MBEDTLS_MD_SHA384 );
9625 case MBEDTLS_SSL_HASH_SHA512:
9626 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009627#endif
9628 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009630 }
9631}
9632
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009633/*
9634 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9635 */
9636unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9637{
9638 switch( md )
9639 {
9640#if defined(MBEDTLS_MD5_C)
9641 case MBEDTLS_MD_MD5:
9642 return( MBEDTLS_SSL_HASH_MD5 );
9643#endif
9644#if defined(MBEDTLS_SHA1_C)
9645 case MBEDTLS_MD_SHA1:
9646 return( MBEDTLS_SSL_HASH_SHA1 );
9647#endif
9648#if defined(MBEDTLS_SHA256_C)
9649 case MBEDTLS_MD_SHA224:
9650 return( MBEDTLS_SSL_HASH_SHA224 );
9651 case MBEDTLS_MD_SHA256:
9652 return( MBEDTLS_SSL_HASH_SHA256 );
9653#endif
9654#if defined(MBEDTLS_SHA512_C)
9655 case MBEDTLS_MD_SHA384:
9656 return( MBEDTLS_SSL_HASH_SHA384 );
9657 case MBEDTLS_MD_SHA512:
9658 return( MBEDTLS_SSL_HASH_SHA512 );
9659#endif
9660 default:
9661 return( MBEDTLS_SSL_HASH_NONE );
9662 }
9663}
9664
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009665#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009666/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009667 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009668 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009669 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009670int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009673
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009674 if( ssl->conf->curve_list == NULL )
9675 return( -1 );
9676
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009677 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009678 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009679 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009680
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009681 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009682}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009683#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009684
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009685#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009686/*
9687 * Check if a hash proposed by the peer is in our list.
9688 * Return 0 if we're willing to use it, -1 otherwise.
9689 */
9690int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9691 mbedtls_md_type_t md )
9692{
9693 const int *cur;
9694
9695 if( ssl->conf->sig_hashes == NULL )
9696 return( -1 );
9697
9698 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9699 if( *cur == (int) md )
9700 return( 0 );
9701
9702 return( -1 );
9703}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009704#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706#if defined(MBEDTLS_X509_CRT_PARSE_C)
9707int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9708 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009709 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009710 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009711{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009712 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009714 int usage = 0;
9715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009717 const char *ext_oid;
9718 size_t ext_len;
9719#endif
9720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9722 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009723 ((void) cert);
9724 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009725 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009726#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9729 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009730 {
9731 /* Server part of the key exchange */
9732 switch( ciphersuite->key_exchange )
9733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 case MBEDTLS_KEY_EXCHANGE_RSA:
9735 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009736 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009737 break;
9738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9740 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9741 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9742 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009743 break;
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9746 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009747 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009748 break;
9749
9750 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 case MBEDTLS_KEY_EXCHANGE_NONE:
9752 case MBEDTLS_KEY_EXCHANGE_PSK:
9753 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9754 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009755 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009756 usage = 0;
9757 }
9758 }
9759 else
9760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9762 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009763 }
9764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009766 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009767 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009768 ret = -1;
9769 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009770#else
9771 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9775 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9778 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009779 }
9780 else
9781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9783 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009784 }
9785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009787 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009788 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009789 ret = -1;
9790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009792
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009793 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009796
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009797/*
9798 * Convert version numbers to/from wire format
9799 * and, for DTLS, to/from TLS equivalent.
9800 *
9801 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009802 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009803 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9804 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009807 unsigned char ver[2] )
9808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809#if defined(MBEDTLS_SSL_PROTO_DTLS)
9810 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009813 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9814
9815 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9816 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9817 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009818 else
9819#else
9820 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009821#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009822 {
9823 ver[0] = (unsigned char) major;
9824 ver[1] = (unsigned char) minor;
9825 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009826}
9827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009829 const unsigned char ver[2] )
9830{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831#if defined(MBEDTLS_SSL_PROTO_DTLS)
9832 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009833 {
9834 *major = 255 - ver[0] + 2;
9835 *minor = 255 - ver[1] + 1;
9836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009838 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9839 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009840 else
9841#else
9842 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009843#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009844 {
9845 *major = ver[0];
9846 *minor = ver[1];
9847 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009848}
9849
Simon Butcher99000142016-10-13 17:21:01 +01009850int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9851{
9852#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9853 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9854 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9855
9856 switch( md )
9857 {
9858#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9859#if defined(MBEDTLS_MD5_C)
9860 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009861 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009862#endif
9863#if defined(MBEDTLS_SHA1_C)
9864 case MBEDTLS_SSL_HASH_SHA1:
9865 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9866 break;
9867#endif
9868#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9869#if defined(MBEDTLS_SHA512_C)
9870 case MBEDTLS_SSL_HASH_SHA384:
9871 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9872 break;
9873#endif
9874#if defined(MBEDTLS_SHA256_C)
9875 case MBEDTLS_SSL_HASH_SHA256:
9876 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9877 break;
9878#endif
9879 default:
9880 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9881 }
9882
9883 return 0;
9884#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9885 (void) ssl;
9886 (void) md;
9887
9888 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9890}
9891
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009892#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9893 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9894int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9895 unsigned char *output,
9896 unsigned char *data, size_t data_len )
9897{
9898 int ret = 0;
9899 mbedtls_md5_context mbedtls_md5;
9900 mbedtls_sha1_context mbedtls_sha1;
9901
9902 mbedtls_md5_init( &mbedtls_md5 );
9903 mbedtls_sha1_init( &mbedtls_sha1 );
9904
9905 /*
9906 * digitally-signed struct {
9907 * opaque md5_hash[16];
9908 * opaque sha_hash[20];
9909 * };
9910 *
9911 * md5_hash
9912 * MD5(ClientHello.random + ServerHello.random
9913 * + ServerParams);
9914 * sha_hash
9915 * SHA(ClientHello.random + ServerHello.random
9916 * + ServerParams);
9917 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009918 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009919 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009921 goto exit;
9922 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009923 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009924 ssl->handshake->randbytes, 64 ) ) != 0 )
9925 {
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_update_ret( &mbedtls_md5, data, data_len ) ) != 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_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009932 goto exit;
9933 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009934 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009935 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009937 goto exit;
9938 }
9939
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009940 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009941 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009943 goto exit;
9944 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009945 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009946 ssl->handshake->randbytes, 64 ) ) != 0 )
9947 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009949 goto exit;
9950 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009951 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009952 data_len ) ) != 0 )
9953 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009955 goto exit;
9956 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009957 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009958 output + 16 ) ) != 0 )
9959 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009961 goto exit;
9962 }
9963
9964exit:
9965 mbedtls_md5_free( &mbedtls_md5 );
9966 mbedtls_sha1_free( &mbedtls_sha1 );
9967
9968 if( ret != 0 )
9969 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9970 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9971
9972 return( ret );
9973
9974}
9975#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
9976 MBEDTLS_SSL_PROTO_TLS1_1 */
9977
9978#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9979 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05009980
9981#if defined(MBEDTLS_USE_PSA_CRYPTO)
9982int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
9983 unsigned char *hash, size_t *hashlen,
9984 unsigned char *data, size_t data_len,
9985 mbedtls_md_type_t md_alg )
9986{
Andrzej Kurek814feff2019-01-14 04:35:19 -05009987 psa_status_t status;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05009988 psa_hash_operation_t hash_operation;
9989 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
9990
Andrzej Kurek814feff2019-01-14 04:35:19 -05009991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Perform PSA-based computation of digest \
9992 of ServerKeyExchange" ) );
9993
9994 if( ( status = psa_hash_setup( &hash_operation,
9995 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05009996 {
Andrzej Kurek814feff2019-01-14 04:35:19 -05009997 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05009998 goto exit;
9999 }
10000
Andrzej Kurek814feff2019-01-14 04:35:19 -050010001 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10002 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010003 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010004 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010005 goto exit;
10006 }
10007
Andrzej Kurek814feff2019-01-14 04:35:19 -050010008 if( ( status = psa_hash_update( &hash_operation,
10009 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010010 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010011 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010012 goto exit;
10013 }
10014
Andrzej Kurek814feff2019-01-14 04:35:19 -050010015 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10016 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010017 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010018 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010019 goto exit;
10020 }
10021
10022exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010023 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010024 {
10025 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10026 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010027 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010028 {
10029 case PSA_ERROR_NOT_SUPPORTED:
10030 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010031 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010032 case PSA_ERROR_BUFFER_TOO_SMALL:
10033 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10034 case PSA_ERROR_INSUFFICIENT_MEMORY:
10035 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10036 default:
10037 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10038 }
10039 }
10040 return( 0 );
10041}
10042
10043#else
10044
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010045int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010046 unsigned char *hash, size_t *hashlen,
10047 unsigned char *data, size_t data_len,
10048 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010049{
10050 int ret = 0;
10051 mbedtls_md_context_t ctx;
10052 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010053 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010054
Andrzej Kurek814feff2019-01-14 04:35:19 -050010055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Perform mbedtls-based computation of digest \
10056 of ServerKeyExchange" ) );
10057
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010058 mbedtls_md_init( &ctx );
10059
10060 /*
10061 * digitally-signed struct {
10062 * opaque client_random[32];
10063 * opaque server_random[32];
10064 * ServerDHParams params;
10065 * };
10066 */
10067 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10068 {
10069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10070 goto exit;
10071 }
10072 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10073 {
10074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10075 goto exit;
10076 }
10077 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10078 {
10079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10080 goto exit;
10081 }
10082 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10083 {
10084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10085 goto exit;
10086 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010087 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010088 {
10089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10090 goto exit;
10091 }
10092
10093exit:
10094 mbedtls_md_free( &ctx );
10095
10096 if( ret != 0 )
10097 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10098 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10099
10100 return( ret );
10101}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010102#endif /* MBEDTLS_USE_PSA_CRYPTO */
10103
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010104#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10105 MBEDTLS_SSL_PROTO_TLS1_2 */
10106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107#endif /* MBEDTLS_SSL_TLS_C */