blob: 8819cf48c5790cac0bd200345d9b8dfae84e42ac [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Andrzej Kurekc929a822019-01-14 03:51:11 -050057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "mbedtls/psa_util.h"
59#endif
60
Hanno Becker2a43f6f2018-08-10 11:12:52 +010061static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010062static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010063
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010070#else
71 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010072#endif
73 return( 0 );
74}
75
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076/*
77 * Start a timer.
78 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020082 if( ssl->f_set_timer == NULL )
83 return;
84
85 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
86 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087}
88
89/*
90 * Return -1 is timer is expired, 0 if it isn't.
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020093{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020094 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020095 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096
97 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020098 {
99 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200101 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102
103 return( 0 );
104}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100106static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
107 mbedtls_ssl_transform *transform );
108static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
111#define SSL_DONT_FORCE_FLUSH 0
112#define SSL_FORCE_FLUSH 1
113
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100115
Hanno Beckerd5847772018-08-28 10:09:23 +0100116/* Forward declarations for functions related to message buffering. */
117static void ssl_buffering_free( mbedtls_ssl_context *ssl );
118static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
119 uint8_t slot );
120static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
121static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
122static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
123static int ssl_buffer_message( mbedtls_ssl_context *ssl );
124static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100125static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100126
Hanno Beckera67dee22018-08-22 10:05:20 +0100127static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100128static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100129{
Hanno Becker11682cc2018-08-22 14:41:02 +0100130 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100131
132 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100133 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134
135 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
136}
137
Hanno Becker67bc7c32018-08-06 11:33:50 +0100138static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
139{
Hanno Becker11682cc2018-08-22 14:41:02 +0100140 size_t const bytes_written = ssl->out_left;
141 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100142
143 /* Double-check that the write-index hasn't gone
144 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100146 {
147 /* Should never happen... */
148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
149 }
150
151 return( (int) ( mtu - bytes_written ) );
152}
153
154static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
155{
156 int ret;
157 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400158 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100159
160#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
161 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
162
163 if( max_len > mfl )
164 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100165
166 /* By the standard (RFC 6066 Sect. 4), the MFL extension
167 * only limits the maximum record payload size, so in theory
168 * we would be allowed to pack multiple records of payload size
169 * MFL into a single datagram. However, this would mean that there's
170 * no way to explicitly communicate MTU restrictions to the peer.
171 *
172 * The following reduction of max_len makes sure that we never
173 * write datagrams larger than MFL + Record Expansion Overhead.
174 */
175 if( max_len <= ssl->out_left )
176 return( 0 );
177
178 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100179#endif
180
181 ret = ssl_get_remaining_space_in_datagram( ssl );
182 if( ret < 0 )
183 return( ret );
184 remaining = (size_t) ret;
185
186 ret = mbedtls_ssl_get_record_expansion( ssl );
187 if( ret < 0 )
188 return( ret );
189 expansion = (size_t) ret;
190
191 if( remaining <= expansion )
192 return( 0 );
193
194 remaining -= expansion;
195 if( remaining >= max_len )
196 remaining = max_len;
197
198 return( (int) remaining );
199}
200
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200201/*
202 * Double the retransmit timeout value, within the allowed range,
203 * returning -1 if the maximum value has already been reached.
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206{
207 uint32_t new_timeout;
208
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200209 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200210 return( -1 );
211
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200212 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
213 * in the following way: after the initial transmission and a first
214 * retransmission, back off to a temporary estimated MTU of 508 bytes.
215 * This value is guaranteed to be deliverable (if not guaranteed to be
216 * delivered) of any compliant IPv4 (and IPv6) network, and should work
217 * on most non-IP stacks too. */
218 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400219 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200220 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
222 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200223
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200224 new_timeout = 2 * ssl->handshake->retransmit_timeout;
225
226 /* Avoid arithmetic overflow and range overflow */
227 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200228 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200230 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200231 }
232
233 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200235 ssl->handshake->retransmit_timeout ) );
236
237 return( 0 );
238}
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200241{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200242 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200244 ssl->handshake->retransmit_timeout ) );
245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200249/*
250 * Convert max_fragment_length codes to length.
251 * RFC 6066 says:
252 * enum{
253 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
254 * } MaxFragmentLength;
255 * and we add 0 -> extension unused
256 */
Angus Grattond8213d02016-05-25 20:56:48 +1000257static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200258{
Angus Grattond8213d02016-05-25 20:56:48 +1000259 switch( mfl )
260 {
261 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
262 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
263 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
264 return 512;
265 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
266 return 1024;
267 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
268 return 2048;
269 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
270 return 4096;
271 default:
272 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
273 }
274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200276
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200277#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_ssl_session_free( dst );
281 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284 if( src->peer_cert != NULL )
285 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200286 int ret;
287
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200288 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200289 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200290 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200295 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298 dst->peer_cert = NULL;
299 return( ret );
300 }
301 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200304#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 if( src->ticket != NULL )
306 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200307 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200308 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200309 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310
311 memcpy( dst->ticket, src->ticket, src->ticket_len );
312 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200313#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
315 return( 0 );
316}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200317#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
320int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200321 const unsigned char *key_enc, const unsigned char *key_dec,
322 size_t keylen,
323 const unsigned char *iv_enc, const unsigned char *iv_dec,
324 size_t ivlen,
325 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200326 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
328int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
329int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
330int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
331int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
332#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000333
Paul Bakker5121ce52009-01-03 21:22:43 +0000334/*
335 * Key material generation
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200338static int ssl3_prf( const unsigned char *secret, size_t slen,
339 const char *label,
340 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000341 unsigned char *dstbuf, size_t dlen )
342{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100343 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000344 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200345 mbedtls_md5_context md5;
346 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000347 unsigned char padding[16];
348 unsigned char sha1sum[20];
349 ((void)label);
350
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200351 mbedtls_md5_init( &md5 );
352 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200353
Paul Bakker5f70b252012-09-13 14:23:06 +0000354 /*
355 * SSLv3:
356 * block =
357 * MD5( secret + SHA1( 'A' + secret + random ) ) +
358 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
359 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
360 * ...
361 */
362 for( i = 0; i < dlen / 16; i++ )
363 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200364 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000365
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100366 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100367 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100368 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100369 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100370 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100371 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100372 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100373 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100374 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100375 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000376
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100377 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100378 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100379 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100380 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100381 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100382 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100383 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100384 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000385 }
386
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200388 mbedtls_md5_free( &md5 );
389 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000390
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500391 mbedtls_platform_zeroize( padding, sizeof( padding ) );
392 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000393
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200399static int tls1_prf( const unsigned char *secret, size_t slen,
400 const char *label,
401 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000402 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000403{
Paul Bakker23986e52011-04-24 08:57:21 +0000404 size_t nb, hs;
405 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200406 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 unsigned char tmp[128];
408 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 const mbedtls_md_info_t *md_info;
410 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100411 int ret;
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 hs = ( slen + 1 ) / 2;
419 S1 = secret;
420 S2 = secret + slen - hs;
421
422 nb = strlen( label );
423 memcpy( tmp + 20, label, nb );
424 memcpy( tmp + 20 + nb, random, rlen );
425 nb += rlen;
426
427 /*
428 * First compute P_md5(secret,label+random)[0..dlen]
429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
431 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100434 return( ret );
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
437 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
438 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 for( i = 0; i < dlen; i += 16 )
441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_md_hmac_reset ( &md_ctx );
443 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
444 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_md_hmac_reset ( &md_ctx );
447 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
448 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
451
452 for( j = 0; j < k; j++ )
453 dstbuf[i + j] = h_i[j];
454 }
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100457
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 /*
459 * XOR out with P_sha1(secret,label+random)[0..dlen]
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100465 return( ret );
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
468 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
469 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 for( i = 0; i < dlen; i += 20 )
472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_md_hmac_reset ( &md_ctx );
474 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
475 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_hmac_reset ( &md_ctx );
478 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
479 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
482
483 for( j = 0; j < k; j++ )
484 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
485 }
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500489 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
490 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500497#if defined(MBEDTLS_USE_PSA_CRYPTO)
498static int tls_prf_generic( mbedtls_md_type_t md_type,
499 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 )
503{
504 psa_status_t status;
505 psa_algorithm_t alg;
506 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500507 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500508 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
509
Andrzej Kurek2f760752019-01-28 08:08:15 -0500510 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500511 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500512 if( status != PSA_SUCCESS )
513 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
514 if( md_type == MBEDTLS_MD_SHA384 )
515 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
516 else
517 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
518
Andrzej Kurek2f760752019-01-28 08:08:15 -0500519 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500520 psa_key_policy_set_usage( &policy,
521 PSA_KEY_USAGE_DERIVE,
522 alg );
523 status = psa_set_key_policy( master_slot, &policy );
524 if( status != PSA_SUCCESS )
525 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
526
527 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
528 if( status != PSA_SUCCESS )
529 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
530
531 status = psa_key_derivation( &generator,
532 master_slot, alg,
533 random, rlen,
534 (unsigned char const *) label,
535 (size_t) strlen( label ),
536 dlen );
537 if( status != PSA_SUCCESS )
538 {
539 psa_generator_abort( &generator );
540 psa_destroy_key( master_slot );
541 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
542 }
543
544 status = psa_generator_read( &generator, dstbuf, dlen );
545 if( status != PSA_SUCCESS )
546 {
547 psa_generator_abort( &generator );
548 psa_destroy_key( master_slot );
549 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
550 }
551
552 status = psa_generator_abort( &generator );
553 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500554 {
555 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500556 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500557 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500558
559 status = psa_destroy_key( master_slot );
560 if( status != PSA_SUCCESS )
561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
562
Andrzej Kurek33171262019-01-15 03:25:18 -0500563 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500564}
565
566#else /* MBEDTLS_USE_PSA_CRYPTO */
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100569 const unsigned char *secret, size_t slen,
570 const char *label,
571 const unsigned char *random, size_t rlen,
572 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000573{
574 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100575 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000576 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
578 const mbedtls_md_info_t *md_info;
579 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100580 int ret;
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100588
589 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000591
592 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100593 memcpy( tmp + md_len, label, nb );
594 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000595 nb += rlen;
596
597 /*
598 * Compute P_<hash>(secret, label + random)[0..dlen]
599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100601 return( ret );
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
604 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
605 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100606
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_md_hmac_reset ( &md_ctx );
610 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
611 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_md_hmac_reset ( &md_ctx );
614 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
615 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000616
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100617 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000618
619 for( j = 0; j < k; j++ )
620 dstbuf[i + j] = h_i[j];
621 }
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100624
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500625 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
626 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000627
628 return( 0 );
629}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500630#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632static int tls_prf_sha256( const unsigned char *secret, size_t slen,
633 const char *label,
634 const unsigned char *random, size_t rlen,
635 unsigned char *dstbuf, size_t dlen )
636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100638 label, random, rlen, dstbuf, dlen ) );
639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200643static int tls_prf_sha384( const unsigned char *secret, size_t slen,
644 const char *label,
645 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646 unsigned char *dstbuf, size_t dlen )
647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100649 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_SHA512_C */
652#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
657 defined(MBEDTLS_SSL_PROTO_TLS1_1)
658static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200659#endif
Paul Bakker380da532012-04-18 16:10:25 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_SSL_PROTO_SSL3)
662static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
663static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200664#endif
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
667static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
668static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200669#endif
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
672#if defined(MBEDTLS_SHA256_C)
673static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
674static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
675static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200676#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SHA512_C)
679static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
680static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
681static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000684
Hanno Becker7d0a5692018-10-23 15:26:22 +0100685#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
686 defined(MBEDTLS_USE_PSA_CRYPTO)
687static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
688{
689 if( ssl->conf->f_psk != NULL )
690 {
691 /* If we've used a callback to select the PSK,
692 * the static configuration is irrelevant. */
693 if( ssl->handshake->psk_opaque != 0 )
694 return( 1 );
695
696 return( 0 );
697 }
698
699 if( ssl->conf->psk_opaque != 0 )
700 return( 1 );
701
702 return( 0 );
703}
704#endif /* MBEDTLS_USE_PSA_CRYPTO &&
705 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000708{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200709 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000710#if defined(MBEDTLS_USE_PSA_CRYPTO)
711 int psa_fallthrough;
712#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 unsigned char keyblk[256];
715 unsigned char *key1;
716 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100717 unsigned char *mac_enc;
718 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000719 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200720 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000721 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 const mbedtls_cipher_info_t *cipher_info;
723 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100724
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000725 /* cf. RFC 5246, Section 8.1:
726 * "The master secret is always exactly 48 bytes in length." */
727 size_t const master_secret_len = 48;
728
Hanno Becker35b23c72018-10-23 12:10:41 +0100729#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
730 unsigned char session_hash[48];
731#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_ssl_session *session = ssl->session_negotiate;
734 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
735 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100740 if( cipher_info == NULL )
741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100743 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100745 }
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100748 if( md_info == NULL )
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100751 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100753 }
754
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000756 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_PROTO_SSL3)
759 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000760 {
Paul Bakker48916f92012-09-16 19:57:18 +0000761 handshake->tls_prf = ssl3_prf;
762 handshake->calc_verify = ssl_calc_verify_ssl;
763 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000764 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200765 else
766#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
768 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000769 {
Paul Bakker48916f92012-09-16 19:57:18 +0000770 handshake->tls_prf = tls1_prf;
771 handshake->calc_verify = ssl_calc_verify_tls;
772 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000773 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200774 else
775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
777#if defined(MBEDTLS_SHA512_C)
778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
779 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000780 {
Paul Bakker48916f92012-09-16 19:57:18 +0000781 handshake->tls_prf = tls_prf_sha384;
782 handshake->calc_verify = ssl_calc_verify_tls_sha384;
783 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000784 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000785 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_SHA256_C)
788 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789 {
Paul Bakker48916f92012-09-16 19:57:18 +0000790 handshake->tls_prf = tls_prf_sha256;
791 handshake->calc_verify = ssl_calc_verify_tls_sha256;
792 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000793 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794 else
795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200800 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801
802 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000803 * SSLv3:
804 * master =
805 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
806 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
807 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200808 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200809 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 * master = PRF( premaster, "master secret", randbytes )[0..47]
811 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100812 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000813 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100814 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
815 }
816 else
817 {
818 /* The label for the KDF used for key expansion.
819 * This is either "master secret" or "extended master secret"
820 * depending on whether the Extended Master Secret extension
821 * is used. */
822 char const *lbl = "master secret";
823
824 /* The salt for the KDF used for key expansion.
825 * - If the Extended Master Secret extension is not used,
826 * this is ClientHello.Random + ServerHello.Random
827 * (see Sect. 8.1 in RFC 5246).
828 * - If the Extended Master Secret extension is used,
829 * this is the transcript of the handshake so far.
830 * (see Sect. 4 in RFC 7627). */
831 unsigned char const *salt = handshake->randbytes;
832 size_t salt_len = 64;
833
834#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
835 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
836 ssl->transform_negotiate->ciphersuite_info;
837 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
838#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
841 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200844
Hanno Becker35b23c72018-10-23 12:10:41 +0100845 lbl = "extended master secret";
846 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200847 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
849 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100852 if( md_type == MBEDTLS_MD_SHA384 )
853 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200854 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100855#endif /* MBEDTLS_SHA512_C */
856 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200857 }
858 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100860 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200861
Hanno Becker35b23c72018-10-23 12:10:41 +0100862 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200863 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100864#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
865
Hanno Becker7d0a5692018-10-23 15:26:22 +0100866#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
867 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
868 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
869 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
870 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100871 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100872 /* Perform PSK-to-MS expansion in a single step. */
873 psa_status_t status;
874 psa_algorithm_t alg;
875 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500876 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100877
878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
879
880 psk = ssl->conf->psk_opaque;
881 if( ssl->handshake->psk_opaque != 0 )
882 psk = ssl->handshake->psk_opaque;
883
884 if( md_type == MBEDTLS_MD_SHA384 )
885 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
886 else
887 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
888
889 status = psa_key_derivation( &generator, psk, alg,
890 salt, salt_len,
891 (unsigned char const *) lbl,
892 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000893 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100894 if( status != PSA_SUCCESS )
895 {
896 psa_generator_abort( &generator );
897 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
898 }
899
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000900 status = psa_generator_read( &generator, session->master,
901 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902 if( status != PSA_SUCCESS )
903 {
904 psa_generator_abort( &generator );
905 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
906 }
907
908 status = psa_generator_abort( &generator );
909 if( status != PSA_SUCCESS )
910 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100911 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100912 else
913#endif
914 {
915 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
916 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000917 session->master,
918 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100919 if( ret != 0 )
920 {
921 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
922 return( ret );
923 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200924
Hanno Becker7d0a5692018-10-23 15:26:22 +0100925 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
926 handshake->premaster,
927 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100928
Hanno Becker7d0a5692018-10-23 15:26:22 +0100929 mbedtls_platform_zeroize( handshake->premaster,
930 sizeof(handshake->premaster) );
931 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
934 /*
935 * Swap the client and server random values.
936 */
Paul Bakker48916f92012-09-16 19:57:18 +0000937 memcpy( tmp, handshake->randbytes, 64 );
938 memcpy( handshake->randbytes, tmp + 32, 32 );
939 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500940 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
942 /*
943 * SSLv3:
944 * key block =
945 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
946 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
947 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
948 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
949 * ...
950 *
951 * TLSv1:
952 * key block = PRF( master, "key expansion", randbytes )
953 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100954 ret = handshake->tls_prf( session->master, 48, "key expansion",
955 handshake->randbytes, 64, keyblk, 256 );
956 if( ret != 0 )
957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100959 return( ret );
960 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
963 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
964 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
965 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
966 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500968 mbedtls_platform_zeroize( handshake->randbytes,
969 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
971 /*
972 * Determine the appropriate key, IV and MAC length.
973 */
Paul Bakker68884e32013-01-07 18:20:04 +0100974
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200975 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200978 cipher_info->mode == MBEDTLS_MODE_CCM ||
979 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000981 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200982
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200983 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000984 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200985
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200986 /* All modes haves 96-bit IVs;
987 * GCM and CCM has 4 implicit and 8 explicit bytes
988 * ChachaPoly has all 12 bytes implicit
989 */
Paul Bakker68884e32013-01-07 18:20:04 +0100990 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200991 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
992 transform->fixed_ivlen = 12;
993 else
994 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200995
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200996 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
997 taglen = transform->ciphersuite_info->flags &
998 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
999
1000
1001 /* Minimum length of encrypted record */
1002 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1003 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001004 }
1005 else
1006 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001007 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1009 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001012 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001013 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001015 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001016 mac_key_len = mbedtls_md_get_size( md_info );
1017 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001020 /*
1021 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1022 * (rfc 6066 page 13 or rfc 2104 section 4),
1023 * so we only need to adjust the length here.
1024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001028
1029#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1030 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001031 * HMAC implementation which also truncates the key
1032 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001033 mac_key_len = transform->maclen;
1034#endif
1035 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001037
1038 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001039 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001041 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001043 transform->minlen = transform->maclen;
1044 else
Paul Bakker68884e32013-01-07 18:20:04 +01001045 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001046 /*
1047 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001048 * 1. if EtM is in use: one block plus MAC
1049 * otherwise: * first multiple of blocklen greater than maclen
1050 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1053 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001054 {
1055 transform->minlen = transform->maclen
1056 + cipher_info->block_size;
1057 }
1058 else
1059#endif
1060 {
1061 transform->minlen = transform->maclen
1062 + cipher_info->block_size
1063 - transform->maclen % cipher_info->block_size;
1064 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1067 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1068 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001069 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001070 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1073 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1074 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001075 {
1076 transform->minlen += transform->ivlen;
1077 }
1078 else
1079#endif
1080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001083 }
Paul Bakker68884e32013-01-07 18:20:04 +01001084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001088 transform->keylen, transform->minlen, transform->ivlen,
1089 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
1091 /*
1092 * Finally setup the cipher contexts, IVs and MAC secrets.
1093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001095 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001097 key1 = keyblk + mac_key_len * 2;
1098 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001099
Paul Bakker68884e32013-01-07 18:20:04 +01001100 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001101 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001103 /*
1104 * This is not used in TLS v1.1.
1105 */
Paul Bakker48916f92012-09-16 19:57:18 +00001106 iv_copy_len = ( transform->fixed_ivlen ) ?
1107 transform->fixed_ivlen : transform->ivlen;
1108 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1109 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001110 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 }
1112 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_SSL_CLI_C */
1114#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001117 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1118 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Hanno Becker81c7b182017-11-09 18:39:33 +00001120 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001121 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001123 /*
1124 * This is not used in TLS v1.1.
1125 */
Paul Bakker48916f92012-09-16 19:57:18 +00001126 iv_copy_len = ( transform->fixed_ivlen ) ?
1127 transform->fixed_ivlen : transform->ivlen;
1128 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1129 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001130 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001132 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#if defined(MBEDTLS_SSL_PROTO_SSL3)
1140 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001141 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001142 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001146 }
1147
Hanno Becker81c7b182017-11-09 18:39:33 +00001148 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1149 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001150 }
1151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1153#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1154 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1155 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001156 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001157 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1158 For AEAD-based ciphersuites, there is nothing to do here. */
1159 if( mac_key_len != 0 )
1160 {
1161 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1162 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1163 }
Paul Bakker68884e32013-01-07 18:20:04 +01001164 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001165 else
1166#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1169 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001170 }
Paul Bakker68884e32013-01-07 18:20:04 +01001171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1173 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001174 {
1175 int ret = 0;
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001180 transform->iv_enc, transform->iv_dec,
1181 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001182 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001183 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1186 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001187 }
1188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001190
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001191#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1192 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001193 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001194 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1195 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001196 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001197 iv_copy_len );
1198 }
1199#endif
1200
Hanno Beckerf704bef2018-11-16 15:21:18 +00001201#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001202
1203 /* Only use PSA-based ciphers for TLS-1.2.
1204 * That's relevant at least for TLS-1.0, where
1205 * we assume that mbedtls_cipher_crypt() updates
1206 * the structure field for the IV, which the PSA-based
1207 * implementation currently doesn't. */
1208#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1209 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001210 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001211 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1212 cipher_info, taglen );
1213 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1214 {
1215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1216 return( ret );
1217 }
1218
1219 if( ret == 0 )
1220 {
1221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1222 psa_fallthrough = 0;
1223 }
1224 else
1225 {
1226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1227 psa_fallthrough = 1;
1228 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001229 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001230 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001231 psa_fallthrough = 1;
1232#else
1233 psa_fallthrough = 1;
1234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001235
Hanno Beckercb1cc802018-11-17 22:27:38 +00001236 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001237#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001238 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001239 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001242 return( ret );
1243 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001244
Hanno Beckerf704bef2018-11-16 15:21:18 +00001245#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001246 /* Only use PSA-based ciphers for TLS-1.2.
1247 * That's relevant at least for TLS-1.0, where
1248 * we assume that mbedtls_cipher_crypt() updates
1249 * the structure field for the IV, which the PSA-based
1250 * implementation currently doesn't. */
1251#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1252 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001253 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001254 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1255 cipher_info, taglen );
1256 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1257 {
1258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1259 return( ret );
1260 }
1261
1262 if( ret == 0 )
1263 {
1264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1265 psa_fallthrough = 0;
1266 }
1267 else
1268 {
1269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1270 psa_fallthrough = 1;
1271 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001272 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001273 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001274 psa_fallthrough = 1;
1275#else
1276 psa_fallthrough = 1;
1277#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001278
Hanno Beckercb1cc802018-11-17 22:27:38 +00001279 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001280#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001281 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001282 cipher_info ) ) != 0 )
1283 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001285 return( ret );
1286 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001289 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001293 return( ret );
1294 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001297 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001301 return( ret );
1302 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#if defined(MBEDTLS_CIPHER_MODE_CBC)
1305 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1308 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001311 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001312 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1315 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318 return( ret );
1319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001323 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001326 // Initialize compression
1327 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001329 {
Paul Bakker16770332013-10-11 09:59:44 +02001330 if( ssl->compress_buf == NULL )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001333 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001334 if( ssl->compress_buf == NULL )
1335 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001337 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001338 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001339 }
1340 }
1341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001343
Paul Bakker48916f92012-09-16 19:57:18 +00001344 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1345 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001346
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001347 if( deflateInit( &transform->ctx_deflate,
1348 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001349 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1352 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001353 }
1354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 return( 0 );
1360}
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_SSL_PROTO_SSL3)
1363void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001365 mbedtls_md5_context md5;
1366 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 unsigned char pad_1[48];
1368 unsigned char pad_2[48];
1369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001372 mbedtls_md5_init( &md5 );
1373 mbedtls_sha1_init( &sha1 );
1374
1375 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1376 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Paul Bakker380da532012-04-18 16:10:25 +00001378 memset( pad_1, 0x36, 48 );
1379 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001381 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1382 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1383 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001385 mbedtls_md5_starts_ret( &md5 );
1386 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1387 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1388 mbedtls_md5_update_ret( &md5, hash, 16 );
1389 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001391 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1392 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1393 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001394
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001395 mbedtls_sha1_starts_ret( &sha1 );
1396 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1397 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1398 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1399 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001403
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001404 mbedtls_md5_free( &md5 );
1405 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001406
Paul Bakker380da532012-04-18 16:10:25 +00001407 return;
1408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1412void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001413{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001414 mbedtls_md5_context md5;
1415 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001418
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001419 mbedtls_md5_init( &md5 );
1420 mbedtls_sha1_init( &sha1 );
1421
1422 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1423 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001424
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001425 mbedtls_md5_finish_ret( &md5, hash );
1426 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001430
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001431 mbedtls_md5_free( &md5 );
1432 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001433
Paul Bakker380da532012-04-18 16:10:25 +00001434 return;
1435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1439#if defined(MBEDTLS_SHA256_C)
1440void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001441{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001442 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001443
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001444 mbedtls_sha256_init( &sha256 );
1445
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001448 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001449 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001453
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001454 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001455
Paul Bakker380da532012-04-18 16:10:25 +00001456 return;
1457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_SHA512_C)
1461void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001462{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001463 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001465 mbedtls_sha512_init( &sha512 );
1466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001468
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001469 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001470 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001475 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001476
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 return;
1478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_SHA512_C */
1480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1483int 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 +02001484{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001485 unsigned char *p = ssl->handshake->premaster;
1486 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001487 const unsigned char *psk = ssl->conf->psk;
1488 size_t psk_len = ssl->conf->psk_len;
1489
1490 /* If the psk callback was called, use its result */
1491 if( ssl->handshake->psk != NULL )
1492 {
1493 psk = ssl->handshake->psk;
1494 psk_len = ssl->handshake->psk_len;
1495 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001496
1497 /*
1498 * PMS = struct {
1499 * opaque other_secret<0..2^16-1>;
1500 * opaque psk<0..2^16-1>;
1501 * };
1502 * with "other_secret" depending on the particular key exchange
1503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1505 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001506 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001507 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001509
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001510 *(p++) = (unsigned char)( psk_len >> 8 );
1511 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001512
1513 if( end < p || (size_t)( end - p ) < psk_len )
1514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1515
1516 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001517 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001518 }
1519 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1521#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1522 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001523 {
1524 /*
1525 * other_secret already set by the ClientKeyExchange message,
1526 * and is 48 bytes long
1527 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001528 if( end - p < 2 )
1529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1530
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001531 *p++ = 0;
1532 *p++ = 48;
1533 p += 48;
1534 }
1535 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1537#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1538 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001539 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001540 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001541 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001542
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001543 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001545 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001546 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001549 return( ret );
1550 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001551 *(p++) = (unsigned char)( len >> 8 );
1552 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001553 p += len;
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001556 }
1557 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1559#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1560 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001561 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001562 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001563 size_t zlen;
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001566 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001567 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001570 return( ret );
1571 }
1572
1573 *(p++) = (unsigned char)( zlen >> 8 );
1574 *(p++) = (unsigned char)( zlen );
1575 p += zlen;
1576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001578 }
1579 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001584 }
1585
1586 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001587 if( end - p < 2 )
1588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001589
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001590 *(p++) = (unsigned char)( psk_len >> 8 );
1591 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001592
1593 if( end < p || (size_t)( end - p ) < psk_len )
1594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1595
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001596 memcpy( p, psk, psk_len );
1597 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598
1599 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1600
1601 return( 0 );
1602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001606/*
1607 * SSLv3.0 MAC functions
1608 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001609#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001610static void ssl_mac( mbedtls_md_context_t *md_ctx,
1611 const unsigned char *secret,
1612 const unsigned char *buf, size_t len,
1613 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001614 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001615{
1616 unsigned char header[11];
1617 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001618 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1620 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001621
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001622 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001624 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001625 else
Paul Bakker68884e32013-01-07 18:20:04 +01001626 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
1628 memcpy( header, ctr, 8 );
1629 header[ 8] = (unsigned char) type;
1630 header[ 9] = (unsigned char)( len >> 8 );
1631 header[10] = (unsigned char)( len );
1632
Paul Bakker68884e32013-01-07 18:20:04 +01001633 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 mbedtls_md_starts( md_ctx );
1635 mbedtls_md_update( md_ctx, secret, md_size );
1636 mbedtls_md_update( md_ctx, padding, padlen );
1637 mbedtls_md_update( md_ctx, header, 11 );
1638 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001639 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
Paul Bakker68884e32013-01-07 18:20:04 +01001641 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_md_starts( md_ctx );
1643 mbedtls_md_update( md_ctx, secret, md_size );
1644 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001645 mbedtls_md_update( md_ctx, out, md_size );
1646 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1651 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001652 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001653#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001654#endif
1655
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001656/* The function below is only used in the Lucky 13 counter-measure in
1657 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1658#if defined(SSL_SOME_MODES_USE_MAC) && \
1659 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1660 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1661 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1662/* This function makes sure every byte in the memory region is accessed
1663 * (in ascending addresses order) */
1664static void ssl_read_memory( unsigned char *p, size_t len )
1665{
1666 unsigned char acc = 0;
1667 volatile unsigned char force;
1668
1669 for( ; len != 0; p++, len-- )
1670 acc ^= *p;
1671
1672 force = acc;
1673 (void) force;
1674}
1675#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1676
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001677/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001683 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001686
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001687 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1690 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001691 }
1692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001696 ssl->out_msg, ssl->out_msglen );
1697
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001699 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001701#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 if( mode == MBEDTLS_MODE_STREAM ||
1703 ( mode == MBEDTLS_MODE_CBC
1704#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1705 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001706#endif
1707 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#if defined(MBEDTLS_SSL_PROTO_SSL3)
1710 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001712 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001713
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001714 ssl_mac( &ssl->transform_out->md_ctx_enc,
1715 ssl->transform_out->mac_enc,
1716 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001717 ssl->out_ctr, ssl->out_msgtype,
1718 mac );
1719
1720 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001721 }
1722 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1725 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1726 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001727 {
Hanno Becker992b6872017-11-09 18:57:39 +00001728 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1731 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1732 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1733 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001734 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001735 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001737
1738 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 }
1740 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001741#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001745 }
1746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001748 ssl->out_msg + ssl->out_msglen,
1749 ssl->transform_out->maclen );
1750
1751 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001752 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001753 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001754#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001756 /*
1757 * Encrypt
1758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1760 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001762 int ret;
1763 size_t olen = 0;
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 "including %d bytes of padding",
1767 ssl->out_msglen, 0 ) );
1768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001770 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001771 ssl->transform_out->ivlen,
1772 ssl->out_msg, ssl->out_msglen,
1773 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001776 return( ret );
1777 }
1778
1779 if( ssl->out_msglen != olen )
1780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1782 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001783 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 }
Paul Bakker68884e32013-01-07 18:20:04 +01001785 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001787#if defined(MBEDTLS_GCM_C) || \
1788 defined(MBEDTLS_CCM_C) || \
1789 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001791 mode == MBEDTLS_MODE_CCM ||
1792 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001793 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001794 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001795 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001796 unsigned char *enc_msg;
1797 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001798 unsigned char iv[12];
1799 mbedtls_ssl_transform *transform = ssl->transform_out;
1800 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001802 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001803
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001804 /*
1805 * Prepare additional authenticated data
1806 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001807 memcpy( add_data, ssl->out_ctr, 8 );
1808 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001810 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001811 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1812 add_data[12] = ssl->out_msglen & 0xFF;
1813
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001814 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001815
Paul Bakker68884e32013-01-07 18:20:04 +01001816 /*
1817 * Generate IV
1818 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001819 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1820 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001821 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001822 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1823 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1824 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1825
1826 }
1827 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1828 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001829 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001830 unsigned char i;
1831
1832 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1833
1834 for( i = 0; i < 8; i++ )
1835 iv[i+4] ^= ssl->out_ctr[i];
1836 }
1837 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001838 {
1839 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001842 }
1843
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001844 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1845 iv, transform->ivlen );
1846 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1847 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001848
Paul Bakker68884e32013-01-07 18:20:04 +01001849 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001850 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001851 */
1852 enc_msg = ssl->out_msg;
1853 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001854 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001857 "including 0 bytes of padding",
1858 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001859
Paul Bakker68884e32013-01-07 18:20:04 +01001860 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001861 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001862 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001863 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1864 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001865 add_data, 13,
1866 enc_msg, enc_msglen,
1867 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001868 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001871 return( ret );
1872 }
1873
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001874 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001878 }
1879
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001880 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001881 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001884 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1887#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001888 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001891 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001892 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001893 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001894
Paul Bakker48916f92012-09-16 19:57:18 +00001895 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1896 ssl->transform_out->ivlen;
1897 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 padlen = 0;
1899
1900 for( i = 0; i <= padlen; i++ )
1901 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1902
1903 ssl->out_msglen += padlen + 1;
1904
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001905 enc_msglen = ssl->out_msglen;
1906 enc_msg = ssl->out_msg;
1907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001909 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001910 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1911 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001914 {
1915 /*
1916 * Generate IV
1917 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001918 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001919 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001920 if( ret != 0 )
1921 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001922
Paul Bakker92be97b2013-01-02 17:30:03 +01001923 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001924 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001925
1926 /*
1927 * Fix pointer positions and message length with added IV
1928 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001929 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001930 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001931 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001932 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001936 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001937 ssl->out_msglen, ssl->transform_out->ivlen,
1938 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001941 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001942 ssl->transform_out->ivlen,
1943 enc_msg, enc_msglen,
1944 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001947 return( ret );
1948 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001949
Paul Bakkercca5b812013-08-31 17:40:26 +02001950 if( enc_msglen != olen )
1951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1953 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001954 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1957 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001958 {
1959 /*
1960 * Save IV in SSL3 and TLS1
1961 */
1962 memcpy( ssl->transform_out->iv_enc,
1963 ssl->transform_out->cipher_ctx_enc.iv,
1964 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001966#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001969 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001971 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1972
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001973 /*
1974 * MAC(MAC_write_key, seq_num +
1975 * TLSCipherText.type +
1976 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001977 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001978 * IV + // except for TLS 1.0
1979 * ENC(content + padding + padding_length));
1980 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001981 unsigned char pseudo_hdr[13];
1982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001984
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001985 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1986 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001987 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1988 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1993 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001994 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00001995 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001997
Hanno Becker3d8c9072018-01-05 16:24:22 +00001998 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1999 ssl->transform_out->maclen );
2000
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002001 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002002 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002006 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002008 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002014 /* Make extra sure authentication was performed, exactly once */
2015 if( auth_done != 1 )
2016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002019 }
2020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
2023 return( 0 );
2024}
2025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002029 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002030#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002031 size_t padlen = 0, correct = 1;
2032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002036 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002040 }
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002043
Paul Bakker48916f92012-09-16 19:57:18 +00002044 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002047 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
2050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2052 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002053 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002054 int ret;
2055 size_t olen = 0;
2056
Paul Bakker68884e32013-01-07 18:20:04 +01002057 padlen = 0;
2058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002060 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002061 ssl->transform_in->ivlen,
2062 ssl->in_msg, ssl->in_msglen,
2063 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002066 return( ret );
2067 }
2068
2069 if( ssl->in_msglen != olen )
2070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 }
Paul Bakker68884e32013-01-07 18:20:04 +01002075 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002077#if defined(MBEDTLS_GCM_C) || \
2078 defined(MBEDTLS_CCM_C) || \
2079 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002081 mode == MBEDTLS_MODE_CCM ||
2082 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002083 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002084 int ret;
2085 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002086 unsigned char *dec_msg;
2087 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002088 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002089 unsigned char iv[12];
2090 mbedtls_ssl_transform *transform = ssl->transform_in;
2091 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002093 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002094
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002095 /*
2096 * Compute and update sizes
2097 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002098 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002101 "+ taglen (%d)", ssl->in_msglen,
2102 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002104 }
2105 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2106
Paul Bakker68884e32013-01-07 18:20:04 +01002107 dec_msg = ssl->in_msg;
2108 dec_msg_result = ssl->in_msg;
2109 ssl->in_msglen = dec_msglen;
2110
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002111 /*
2112 * Prepare additional authenticated data
2113 */
Paul Bakker68884e32013-01-07 18:20:04 +01002114 memcpy( add_data, ssl->in_ctr, 8 );
2115 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002117 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002118 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2119 add_data[12] = ssl->in_msglen & 0xFF;
2120
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002121 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002122
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002123 /*
2124 * Prepare IV
2125 */
2126 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2127 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002128 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002129 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2130 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002131
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002132 }
2133 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2134 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002135 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002136 unsigned char i;
2137
2138 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2139
2140 for( i = 0; i < 8; i++ )
2141 iv[i+4] ^= ssl->in_ctr[i];
2142 }
2143 else
2144 {
2145 /* Reminder if we ever add an AEAD mode with a different size */
2146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2148 }
2149
2150 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002152
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002153 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002154 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002157 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002158 add_data, 13,
2159 dec_msg, dec_msglen,
2160 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002161 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2166 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002167
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002168 return( ret );
2169 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002170 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002171
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002172 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002176 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2180#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002181 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 {
Paul Bakker45829992013-01-03 14:52:21 +01002184 /*
2185 * Decrypt and check the padding
2186 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002187 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002188 unsigned char *dec_msg;
2189 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002190 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002191 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002192 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002193
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 /*
Paul Bakker45829992013-01-03 14:52:21 +01002195 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2198 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002199 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002200#endif
Paul Bakker45829992013-01-03 14:52:21 +01002201
2202 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2203 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002206 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2207 ssl->transform_in->ivlen,
2208 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002209 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002210 }
2211
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002212 dec_msglen = ssl->in_msglen;
2213 dec_msg = ssl->in_msg;
2214 dec_msg_result = ssl->in_msg;
2215
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002216 /*
2217 * Authenticate before decrypt if enabled
2218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2220 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221 {
Hanno Becker992b6872017-11-09 18:57:39 +00002222 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002223 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002226
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002227 dec_msglen -= ssl->transform_in->maclen;
2228 ssl->in_msglen -= ssl->transform_in->maclen;
2229
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002230 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2231 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2232 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2233 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2238 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002239 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002240 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002244 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002245 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002246 ssl->transform_in->maclen );
2247
Hanno Becker992b6872017-11-09 18:57:39 +00002248 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2249 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002254 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002255 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002256 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002258
2259 /*
2260 * Check length sanity
2261 */
2262 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002265 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002267 }
2268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002270 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002271 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002274 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002275 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002276 dec_msglen -= ssl->transform_in->ivlen;
2277 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002278
Paul Bakker48916f92012-09-16 19:57:18 +00002279 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002280 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002281 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002285 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002286 ssl->transform_in->ivlen,
2287 dec_msg, dec_msglen,
2288 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002291 return( ret );
2292 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002293
Paul Bakkercca5b812013-08-31 17:40:26 +02002294 if( dec_msglen != olen )
2295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002298 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2301 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002302 {
2303 /*
2304 * Save IV in SSL3 and TLS1
2305 */
2306 memcpy( ssl->transform_in->iv_dec,
2307 ssl->transform_in->cipher_ctx_dec.iv,
2308 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002311
2312 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002313
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002314 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002315 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_SSL_DEBUG_ALL)
2318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002319 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002320#endif
Paul Bakker45829992013-01-03 14:52:21 +01002321 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002322 correct = 0;
2323 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_SSL_PROTO_SSL3)
2326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 {
Paul Bakker48916f92012-09-16 19:57:18 +00002328 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_SSL_DEBUG_ALL)
2331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002333 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002334#endif
Paul Bakker45829992013-01-03 14:52:21 +01002335 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 }
2337 }
2338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2340#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2341 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2342 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 {
2344 /*
Paul Bakker45829992013-01-03 14:52:21 +01002345 * TLSv1+: always check the padding up to the first failure
2346 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002348 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002349 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002351
Paul Bakker956c9e02013-12-19 14:42:28 +01002352 /*
2353 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002354 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002355 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002356 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002357 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002358 *
2359 * In both cases we reset padding_idx to a safe value (0) to
2360 * prevent out-of-buffer reads.
2361 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002362 correct &= ( padlen <= ssl->in_msglen );
2363 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002364 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002365
2366 padding_idx *= correct;
2367
Angus Grattonb512bc12018-06-19 15:57:50 +10002368 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002369 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002370 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002371 pad_count += real_count *
2372 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2373 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002374
2375 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002378 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002380#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002381 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2385 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2388 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002389 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002390
2391 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002393 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002395 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002401#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
2406 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002407 * Authenticate if not done yet.
2408 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002410#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002411 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002412 {
Hanno Becker992b6872017-11-09 18:57:39 +00002413 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002414
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002415 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002417 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2418 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420#if defined(MBEDTLS_SSL_PROTO_SSL3)
2421 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002422 {
2423 ssl_mac( &ssl->transform_in->md_ctx_dec,
2424 ssl->transform_in->mac_dec,
2425 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002426 ssl->in_ctr, ssl->in_msgtype,
2427 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002428 }
2429 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2431#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2432 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2433 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002434 {
2435 /*
2436 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002437 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002438 *
2439 * Known timing attacks:
2440 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2441 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002442 * To compensate for different timings for the MAC calculation
2443 * depending on how much padding was removed (which is determined
2444 * by padlen), process extra_run more blocks through the hash
2445 * function.
2446 *
2447 * The formula in the paper is
2448 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2449 * where L1 is the size of the header plus the decrypted message
2450 * plus CBC padding and L2 is the size of the header plus the
2451 * decrypted message. This is for an underlying hash function
2452 * with 64-byte blocks.
2453 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2454 * correctly. We round down instead of up, so -56 is the correct
2455 * value for our calculations instead of -55.
2456 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002457 * Repeat the formula rather than defining a block_size variable.
2458 * This avoids requiring division by a variable at runtime
2459 * (which would be marginally less efficient and would require
2460 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002461 */
2462 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002463
2464 /*
2465 * The next two sizes are the minimum and maximum values of
2466 * in_msglen over all padlen values.
2467 *
2468 * They're independent of padlen, since we previously did
2469 * in_msglen -= padlen.
2470 *
2471 * Note that max_len + maclen is never more than the buffer
2472 * length, as we previously did in_msglen -= maclen too.
2473 */
2474 const size_t max_len = ssl->in_msglen + padlen;
2475 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2476
Gilles Peskine20b44082018-05-29 14:06:49 +02002477 switch( ssl->transform_in->ciphersuite_info->mac )
2478 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002479#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2480 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002481 case MBEDTLS_MD_MD5:
2482 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002483 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002484 /* 8 bytes of message size, 64-byte compression blocks */
2485 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2486 ( 13 + ssl->in_msglen + 8 ) / 64;
2487 break;
2488#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002489#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002490 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002491 /* 16 bytes of message size, 128-byte compression blocks */
2492 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2493 ( 13 + ssl->in_msglen + 16 ) / 128;
2494 break;
2495#endif
2496 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002498 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2499 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002500
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002501 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2504 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2505 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2506 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002507 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002508 /* Make sure we access everything even when padlen > 0. This
2509 * makes the synchronisation requirements for just-in-time
2510 * Prime+Probe attacks much tighter and hopefully impractical. */
2511 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002512 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002513
2514 /* Call mbedtls_md_process at least once due to cache attacks
2515 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002516 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002520
2521 /* Make sure we access all the memory that could contain the MAC,
2522 * before we check it in the next code block. This makes the
2523 * synchronisation requirements for just-in-time Prime+Probe
2524 * attacks much tighter and hopefully impractical. */
2525 ssl_read_memory( ssl->in_msg + min_len,
2526 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002527 }
2528 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2530 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002536#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002537 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2538 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2539 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Hanno Becker992b6872017-11-09 18:57:39 +00002542 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2543 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_SSL_DEBUG_ALL)
2546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002547#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002548 correct = 0;
2549 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002550 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002551 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002552
2553 /*
2554 * Finally check the correct flag
2555 */
2556 if( correct == 0 )
2557 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002558#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002559
2560 /* Make extra sure authentication was performed, exactly once */
2561 if( auth_done != 1 )
2562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
2567 if( ssl->in_msglen == 0 )
2568 {
Angus Gratton34817922018-06-19 15:58:22 +10002569#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2570 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2571 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2572 {
2573 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2575 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2576 }
2577#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2578
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 ssl->nb_zero++;
2580
2581 /*
2582 * Three or more empty messages may be a DoS attack
2583 * (excessive CPU consumption).
2584 */
2585 if( ssl->nb_zero > 3 )
2586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
2591 }
2592 else
2593 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002597 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002598 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002599 }
2600 else
2601#endif
2602 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002603 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002604 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2605 if( ++ssl->in_ctr[i - 1] != 0 )
2606 break;
2607
2608 /* The loop goes to its end iff the counter is wrapping */
2609 if( i == ssl_ep_len( ssl ) )
2610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2612 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002613 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002614 }
2615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
2618 return( 0 );
2619}
2620
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002621#undef MAC_NONE
2622#undef MAC_PLAINTEXT
2623#undef MAC_CIPHERTEXT
2624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002626/*
2627 * Compression/decompression functions
2628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002630{
2631 int ret;
2632 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002633 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002634 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002635 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002638
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002639 if( len_pre == 0 )
2640 return( 0 );
2641
Paul Bakker2770fbd2012-07-03 13:30:23 +00002642 memcpy( msg_pre, ssl->out_msg, len_pre );
2643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002645 ssl->out_msglen ) );
2646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002648 ssl->out_msg, ssl->out_msglen );
2649
Paul Bakker48916f92012-09-16 19:57:18 +00002650 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2651 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2652 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002653 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002654
Paul Bakker48916f92012-09-16 19:57:18 +00002655 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002656 if( ret != Z_OK )
2657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2659 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002660 }
2661
Angus Grattond8213d02016-05-25 20:56:48 +10002662 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002663 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002666 ssl->out_msglen ) );
2667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002669 ssl->out_msg, ssl->out_msglen );
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002672
2673 return( 0 );
2674}
2675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002677{
2678 int ret;
2679 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002680 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002681 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002682 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002685
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002686 if( len_pre == 0 )
2687 return( 0 );
2688
Paul Bakker2770fbd2012-07-03 13:30:23 +00002689 memcpy( msg_pre, ssl->in_msg, len_pre );
2690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002692 ssl->in_msglen ) );
2693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002695 ssl->in_msg, ssl->in_msglen );
2696
Paul Bakker48916f92012-09-16 19:57:18 +00002697 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2698 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2699 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002700 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002701 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002702
Paul Bakker48916f92012-09-16 19:57:18 +00002703 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002704 if( ret != Z_OK )
2705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2707 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002708 }
2709
Angus Grattond8213d02016-05-25 20:56:48 +10002710 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002711 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002714 ssl->in_msglen ) );
2715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002717 ssl->in_msg, ssl->in_msglen );
2718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720
2721 return( 0 );
2722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2726static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728#if defined(MBEDTLS_SSL_PROTO_DTLS)
2729static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002730{
2731 /* If renegotiation is not enforced, retransmit until we would reach max
2732 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002733 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002734 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002735 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002736 unsigned char doublings = 1;
2737
2738 while( ratio != 0 )
2739 {
2740 ++doublings;
2741 ratio >>= 1;
2742 }
2743
2744 if( ++ssl->renego_records_seen > doublings )
2745 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002747 return( 0 );
2748 }
2749 }
2750
2751 return( ssl_write_hello_request( ssl ) );
2752}
2753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002755
Paul Bakker5121ce52009-01-03 21:22:43 +00002756/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002757 * Fill the input message buffer by appending data to it.
2758 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002759 *
2760 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2761 * available (from this read and/or a previous one). Otherwise, an error code
2762 * is returned (possibly EOF or WANT_READ).
2763 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002764 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2765 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2766 * since we always read a whole datagram at once.
2767 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002768 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002769 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772{
Paul Bakker23986e52011-04-24 08:57:21 +00002773 int ret;
2774 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002778 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002781 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002783 }
2784
Angus Grattond8213d02016-05-25 20:56:48 +10002785 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002789 }
2790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002794 uint32_t timeout;
2795
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002796 /* Just to be sure */
2797 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2798 {
2799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2800 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2802 }
2803
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002804 /*
2805 * The point is, we need to always read a full datagram at once, so we
2806 * sometimes read more then requested, and handle the additional data.
2807 * It could be the rest of the current record (while fetching the
2808 * header) and/or some other records in the same datagram.
2809 */
2810
2811 /*
2812 * Move to the next record in the already read datagram if applicable
2813 */
2814 if( ssl->next_record_offset != 0 )
2815 {
2816 if( ssl->in_left < ssl->next_record_offset )
2817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2819 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002820 }
2821
2822 ssl->in_left -= ssl->next_record_offset;
2823
2824 if( ssl->in_left != 0 )
2825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002827 ssl->next_record_offset ) );
2828 memmove( ssl->in_hdr,
2829 ssl->in_hdr + ssl->next_record_offset,
2830 ssl->in_left );
2831 }
2832
2833 ssl->next_record_offset = 0;
2834 }
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002838
2839 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002840 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002841 */
2842 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002845 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002846 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002847
2848 /*
2849 * A record can't be split accross datagrams. If we need to read but
2850 * are not at the beginning of a new record, the caller did something
2851 * wrong.
2852 */
2853 if( ssl->in_left != 0 )
2854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002857 }
2858
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002859 /*
2860 * Don't even try to read if time's out already.
2861 * This avoids by-passing the timer when repeatedly receiving messages
2862 * that will end up being dropped.
2863 */
2864 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002865 {
2866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002867 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002868 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002869 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002870 {
Angus Grattond8213d02016-05-25 20:56:48 +10002871 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002874 timeout = ssl->handshake->retransmit_timeout;
2875 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002876 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002879
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002880 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002881 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2882 timeout );
2883 else
2884 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002887
2888 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002890 }
2891
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002892 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002895 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002898 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002899 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002902 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002903 }
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002908 return( ret );
2909 }
2910
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002911 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002912 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002914 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002916 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002917 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002920 return( ret );
2921 }
2922
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002923 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002924 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002926 }
2927
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 if( ret < 0 )
2929 return( ret );
2930
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002931 ssl->in_left = ret;
2932 }
2933 else
2934#endif
2935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002937 ssl->in_left, nb_want ) );
2938
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002939 while( ssl->in_left < nb_want )
2940 {
2941 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002942
2943 if( ssl_check_timer( ssl ) != 0 )
2944 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2945 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002946 {
2947 if( ssl->f_recv_timeout != NULL )
2948 {
2949 ret = ssl->f_recv_timeout( ssl->p_bio,
2950 ssl->in_hdr + ssl->in_left, len,
2951 ssl->conf->read_timeout );
2952 }
2953 else
2954 {
2955 ret = ssl->f_recv( ssl->p_bio,
2956 ssl->in_hdr + ssl->in_left, len );
2957 }
2958 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002961 ssl->in_left, nb_want ) );
2962 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002963
2964 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002966
2967 if( ret < 0 )
2968 return( ret );
2969
mohammad160352aecb92018-03-28 23:41:40 -07002970 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002971 {
Darryl Green11999bb2018-03-13 15:22:58 +00002972 MBEDTLS_SSL_DEBUG_MSG( 1,
2973 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002974 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2976 }
2977
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002978 ssl->in_left += ret;
2979 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 }
2981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002983
2984 return( 0 );
2985}
2986
2987/*
2988 * Flush any data not yet written
2989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002991{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002992 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002993 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002997 if( ssl->f_send == NULL )
2998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003000 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003002 }
3003
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003004 /* Avoid incrementing counter if data is flushed */
3005 if( ssl->out_left == 0 )
3006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003008 return( 0 );
3009 }
3010
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 while( ssl->out_left > 0 )
3012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3014 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
Hanno Becker2b1e3542018-08-06 11:19:13 +01003016 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
3021 if( ret <= 0 )
3022 return( ret );
3023
mohammad160352aecb92018-03-28 23:41:40 -07003024 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003025 {
Darryl Green11999bb2018-03-13 15:22:58 +00003026 MBEDTLS_SSL_DEBUG_MSG( 1,
3027 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003028 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3030 }
3031
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 ssl->out_left -= ret;
3033 }
3034
Hanno Becker2b1e3542018-08-06 11:19:13 +01003035#if defined(MBEDTLS_SSL_PROTO_DTLS)
3036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003037 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003038 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003039 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003040 else
3041#endif
3042 {
3043 ssl->out_hdr = ssl->out_buf + 8;
3044 }
3045 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
3049 return( 0 );
3050}
3051
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003052/*
3053 * Functions to handle the DTLS retransmission state machine
3054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003056/*
3057 * Append current handshake message to current outgoing flight
3058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3063 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3064 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003065
3066 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003067 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003068 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003071 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003072 }
3073
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003074 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003075 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003078 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003079 }
3080
3081 /* Copy current handshake message with headers */
3082 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3083 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003084 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003085 msg->next = NULL;
3086
3087 /* Append to the current flight */
3088 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003089 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003090 else
3091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003093 while( cur->next != NULL )
3094 cur = cur->next;
3095 cur->next = msg;
3096 }
3097
Hanno Becker3b235902018-08-06 09:54:53 +01003098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003099 return( 0 );
3100}
3101
3102/*
3103 * Free the current flight of handshake messages
3104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 mbedtls_ssl_flight_item *cur = flight;
3108 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003109
3110 while( cur != NULL )
3111 {
3112 next = cur->next;
3113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 mbedtls_free( cur->p );
3115 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003116
3117 cur = next;
3118 }
3119}
3120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3122static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003123#endif
3124
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003125/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003126 * Swap transform_out and out_ctr with the alternative ones
3127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003131 unsigned char tmp_out_ctr[8];
3132
3133 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003136 return;
3137 }
3138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003140
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003141 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003142 tmp_transform = ssl->transform_out;
3143 ssl->transform_out = ssl->handshake->alt_transform_out;
3144 ssl->handshake->alt_transform_out = tmp_transform;
3145
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003146 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003147 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3148 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003149 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003150
3151 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003152 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3155 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3160 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003161 }
3162 }
3163#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003164}
3165
3166/*
3167 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003168 */
3169int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3170{
3171 int ret = 0;
3172
3173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3174
3175 ret = mbedtls_ssl_flight_transmit( ssl );
3176
3177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3178
3179 return( ret );
3180}
3181
3182/*
3183 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003184 *
3185 * Need to remember the current message in case flush_output returns
3186 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003187 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003188 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003189int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003190{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003191 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003195 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003197
3198 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003199 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003200 ssl_swap_epochs( ssl );
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003203 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003204
3205 while( ssl->handshake->cur_msg != NULL )
3206 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003207 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003208 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003209
Hanno Beckere1dcb032018-08-17 16:47:58 +01003210 int const is_finished =
3211 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3212 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3213
Hanno Becker04da1892018-08-14 13:22:10 +01003214 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3215 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3216
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003217 /* Swap epochs before sending Finished: we can't do it after
3218 * sending ChangeCipherSpec, in case write returns WANT_READ.
3219 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003220 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003221 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003223 ssl_swap_epochs( ssl );
3224 }
3225
Hanno Becker67bc7c32018-08-06 11:33:50 +01003226 ret = ssl_get_remaining_payload_in_datagram( ssl );
3227 if( ret < 0 )
3228 return( ret );
3229 max_frag_len = (size_t) ret;
3230
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003231 /* CCS is copied as is, while HS messages may need fragmentation */
3232 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3233 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003234 if( max_frag_len == 0 )
3235 {
3236 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3237 return( ret );
3238
3239 continue;
3240 }
3241
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003242 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003243 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003244 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003245
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003246 /* Update position inside current message */
3247 ssl->handshake->cur_msg_p += cur->len;
3248 }
3249 else
3250 {
3251 const unsigned char * const p = ssl->handshake->cur_msg_p;
3252 const size_t hs_len = cur->len - 12;
3253 const size_t frag_off = p - ( cur->p + 12 );
3254 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003255 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003256
Hanno Beckere1dcb032018-08-17 16:47:58 +01003257 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003258 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003259 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003260 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003261
Hanno Becker67bc7c32018-08-06 11:33:50 +01003262 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3263 return( ret );
3264
3265 continue;
3266 }
3267 max_hs_frag_len = max_frag_len - 12;
3268
3269 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3270 max_hs_frag_len : rem_len;
3271
3272 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003273 {
3274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003275 (unsigned) cur_hs_frag_len,
3276 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003277 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003278
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003279 /* Messages are stored with handshake headers as if not fragmented,
3280 * copy beginning of headers then fill fragmentation fields.
3281 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3282 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003283
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003284 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3285 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3286 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3287
Hanno Becker67bc7c32018-08-06 11:33:50 +01003288 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3289 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3290 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003291
3292 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3293
Hanno Becker3f7b9732018-08-28 09:53:25 +01003294 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003295 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3296 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003297 ssl->out_msgtype = cur->type;
3298
3299 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003300 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003301 }
3302
3303 /* If done with the current message move to the next one if any */
3304 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3305 {
3306 if( cur->next != NULL )
3307 {
3308 ssl->handshake->cur_msg = cur->next;
3309 ssl->handshake->cur_msg_p = cur->next->p + 12;
3310 }
3311 else
3312 {
3313 ssl->handshake->cur_msg = NULL;
3314 ssl->handshake->cur_msg_p = NULL;
3315 }
3316 }
3317
3318 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003319 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003322 return( ret );
3323 }
3324 }
3325
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3327 return( ret );
3328
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003329 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3331 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003332 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003335 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3336 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003337
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339
3340 return( 0 );
3341}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003342
3343/*
3344 * To be called when the last message of an incoming flight is received.
3345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003347{
3348 /* We won't need to resend that one any more */
3349 ssl_flight_free( ssl->handshake->flight );
3350 ssl->handshake->flight = NULL;
3351 ssl->handshake->cur_msg = NULL;
3352
3353 /* The next incoming flight will start with this msg_seq */
3354 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3355
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003356 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003357 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003358
Hanno Becker0271f962018-08-16 13:23:47 +01003359 /* Clear future message buffering structure. */
3360 ssl_buffering_free( ssl );
3361
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003362 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003363 ssl_set_timer( ssl, 0 );
3364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3366 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003369 }
3370 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003373
3374/*
3375 * To be called when the last message of an outgoing flight is send.
3376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003378{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003379 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003380 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3383 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003386 }
3387 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003391
Paul Bakker5121ce52009-01-03 21:22:43 +00003392/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003393 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003395
3396/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003397 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003398 *
3399 * - fill in handshake headers
3400 * - update handshake checksum
3401 * - DTLS: save message for resending
3402 * - then pass to the record layer
3403 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003404 * DTLS: except for HelloRequest, messages are only queued, and will only be
3405 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003406 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003407 * Inputs:
3408 * - ssl->out_msglen: 4 + actual handshake message len
3409 * (4 is the size of handshake headers for TLS)
3410 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3411 * - ssl->out_msg + 4: the handshake message body
3412 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003413 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003414 * - ssl->out_msglen: the length of the record contents
3415 * (including handshake headers but excluding record headers)
3416 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003417 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003418int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003419{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003420 int ret;
3421 const size_t hs_len = ssl->out_msglen - 4;
3422 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3425
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003426 /*
3427 * Sanity checks
3428 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003429 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003430 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3431 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003432 /* In SSLv3, the client might send a NoCertificate alert. */
3433#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3434 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3435 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3436 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3437#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3438 {
3439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3441 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003443
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003444 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3445 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3446 ssl->handshake == NULL )
3447 {
3448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3449 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3450 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003454 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003456 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003459 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003460#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003461
Hanno Beckerb50a2532018-08-06 11:52:54 +01003462 /* Double-check that we did not exceed the bounds
3463 * of the outgoing record buffer.
3464 * This should never fail as the various message
3465 * writing functions must obey the bounds of the
3466 * outgoing record buffer, but better be safe.
3467 *
3468 * Note: We deliberately do not check for the MTU or MFL here.
3469 */
3470 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3471 {
3472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3473 "size %u, maximum %u",
3474 (unsigned) ssl->out_msglen,
3475 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3476 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3477 }
3478
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003479 /*
3480 * Fill handshake headers
3481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003484 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3485 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3486 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003488 /*
3489 * DTLS has additional fields in the Handshake layer,
3490 * between the length field and the actual payload:
3491 * uint16 message_seq;
3492 * uint24 fragment_offset;
3493 * uint24 fragment_length;
3494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003496 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003497 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003498 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003499 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003500 {
3501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3502 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003503 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003504 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3506 }
3507
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003508 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003509 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003510
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003511 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003512 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003513 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003514 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3515 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3516 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003517 }
3518 else
3519 {
3520 ssl->out_msg[4] = 0;
3521 ssl->out_msg[5] = 0;
3522 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003523
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003524 /* Handshake hashes are computed without fragmentation,
3525 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003526 memset( ssl->out_msg + 6, 0x00, 3 );
3527 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003530
Hanno Becker0207e532018-08-28 10:28:28 +01003531 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003532 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3533 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 }
3535
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003536 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Becker081bd812018-08-22 16:07:59 +01003539 ( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
3540 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003541 {
3542 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003545 return( ret );
3546 }
3547 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003548 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003549#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003550 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003551 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003552 {
3553 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3554 return( ret );
3555 }
3556 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003557
3558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3559
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003560 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003561}
3562
3563/*
3564 * Record layer functions
3565 */
3566
3567/*
3568 * Write current record.
3569 *
3570 * Uses:
3571 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3572 * - ssl->out_msglen: length of the record content (excl headers)
3573 * - ssl->out_msg: record content
3574 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003575int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003576{
3577 int ret, done = 0;
3578 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003579 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003580
3581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003584 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003586 {
3587 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590 return( ret );
3591 }
3592
3593 len = ssl->out_msglen;
3594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3598 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 ret = mbedtls_ssl_hw_record_write( ssl );
3603 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3606 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003607 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003608
3609 if( ret == 0 )
3610 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003613 if( !done )
3614 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003615 unsigned i;
3616 size_t protected_record_size;
3617
Paul Bakker05ef8352012-05-08 09:17:57 +00003618 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003620 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003621
Hanno Becker19859472018-08-06 09:40:20 +01003622 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003623 ssl->out_len[0] = (unsigned char)( len >> 8 );
3624 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003625
Paul Bakker48916f92012-09-16 19:57:18 +00003626 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003627 {
3628 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003631 return( ret );
3632 }
3633
3634 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003635 ssl->out_len[0] = (unsigned char)( len >> 8 );
3636 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003637 }
3638
Hanno Becker2b1e3542018-08-06 11:19:13 +01003639 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3640
3641#if defined(MBEDTLS_SSL_PROTO_DTLS)
3642 /* In case of DTLS, double-check that we don't exceed
3643 * the remaining space in the datagram. */
3644 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3645 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003646 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003647 if( ret < 0 )
3648 return( ret );
3649
3650 if( protected_record_size > (size_t) ret )
3651 {
3652 /* Should never happen */
3653 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3654 }
3655 }
3656#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003659 "version = [%d:%d], msglen = %d",
3660 ssl->out_hdr[0], ssl->out_hdr[1],
3661 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003664 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003665
3666 ssl->out_left += protected_record_size;
3667 ssl->out_hdr += protected_record_size;
3668 ssl_update_out_pointers( ssl, ssl->transform_out );
3669
Hanno Becker04484622018-08-06 09:49:38 +01003670 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3671 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3672 break;
3673
3674 /* The loop goes to its end iff the counter is wrapping */
3675 if( i == ssl_ep_len( ssl ) )
3676 {
3677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3678 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 }
3681
Hanno Becker67bc7c32018-08-06 11:33:50 +01003682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003683 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3684 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003685 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003686 size_t remaining;
3687 ret = ssl_get_remaining_payload_in_datagram( ssl );
3688 if( ret < 0 )
3689 {
3690 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3691 ret );
3692 return( ret );
3693 }
3694
3695 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003696 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003697 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003698 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003699 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003700 else
3701 {
Hanno Becker513815a2018-08-20 11:56:09 +01003702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003703 }
3704 }
3705#endif /* MBEDTLS_SSL_PROTO_DTLS */
3706
3707 if( ( flush == SSL_FORCE_FLUSH ) &&
3708 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 return( ret );
3712 }
3713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
3716 return( 0 );
3717}
3718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003720
3721static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3722{
3723 if( ssl->in_msglen < ssl->in_hslen ||
3724 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3725 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3726 {
3727 return( 1 );
3728 }
3729 return( 0 );
3730}
Hanno Becker44650b72018-08-16 12:51:11 +01003731
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003732static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003733{
3734 return( ( ssl->in_msg[9] << 16 ) |
3735 ( ssl->in_msg[10] << 8 ) |
3736 ssl->in_msg[11] );
3737}
3738
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003739static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003740{
3741 return( ( ssl->in_msg[6] << 16 ) |
3742 ( ssl->in_msg[7] << 8 ) |
3743 ssl->in_msg[8] );
3744}
3745
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003746static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003747{
3748 uint32_t msg_len, frag_off, frag_len;
3749
3750 msg_len = ssl_get_hs_total_len( ssl );
3751 frag_off = ssl_get_hs_frag_off( ssl );
3752 frag_len = ssl_get_hs_frag_len( ssl );
3753
3754 if( frag_off > msg_len )
3755 return( -1 );
3756
3757 if( frag_len > msg_len - frag_off )
3758 return( -1 );
3759
3760 if( frag_len + 12 > ssl->in_msglen )
3761 return( -1 );
3762
3763 return( 0 );
3764}
3765
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003766/*
3767 * Mark bits in bitmask (used for DTLS HS reassembly)
3768 */
3769static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3770{
3771 unsigned int start_bits, end_bits;
3772
3773 start_bits = 8 - ( offset % 8 );
3774 if( start_bits != 8 )
3775 {
3776 size_t first_byte_idx = offset / 8;
3777
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003778 /* Special case */
3779 if( len <= start_bits )
3780 {
3781 for( ; len != 0; len-- )
3782 mask[first_byte_idx] |= 1 << ( start_bits - len );
3783
3784 /* Avoid potential issues with offset or len becoming invalid */
3785 return;
3786 }
3787
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003788 offset += start_bits; /* Now offset % 8 == 0 */
3789 len -= start_bits;
3790
3791 for( ; start_bits != 0; start_bits-- )
3792 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3793 }
3794
3795 end_bits = len % 8;
3796 if( end_bits != 0 )
3797 {
3798 size_t last_byte_idx = ( offset + len ) / 8;
3799
3800 len -= end_bits; /* Now len % 8 == 0 */
3801
3802 for( ; end_bits != 0; end_bits-- )
3803 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3804 }
3805
3806 memset( mask + offset / 8, 0xFF, len / 8 );
3807}
3808
3809/*
3810 * Check that bitmask is full
3811 */
3812static int ssl_bitmask_check( unsigned char *mask, size_t len )
3813{
3814 size_t i;
3815
3816 for( i = 0; i < len / 8; i++ )
3817 if( mask[i] != 0xFF )
3818 return( -1 );
3819
3820 for( i = 0; i < len % 8; i++ )
3821 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3822 return( -1 );
3823
3824 return( 0 );
3825}
3826
Hanno Becker56e205e2018-08-16 09:06:12 +01003827/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003828static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003829 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003830{
Hanno Becker56e205e2018-08-16 09:06:12 +01003831 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003832
Hanno Becker56e205e2018-08-16 09:06:12 +01003833 alloc_len = 12; /* Handshake header */
3834 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003835
Hanno Beckerd07df862018-08-16 09:14:58 +01003836 if( add_bitmap )
3837 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003838
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003839 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003840}
Hanno Becker56e205e2018-08-16 09:06:12 +01003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003843
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003844static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003845{
3846 return( ( ssl->in_msg[1] << 16 ) |
3847 ( ssl->in_msg[2] << 8 ) |
3848 ssl->in_msg[3] );
3849}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003850
Simon Butcher99000142016-10-13 17:21:01 +01003851int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003856 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003858 }
3859
Hanno Becker12555c62018-08-16 12:47:53 +01003860 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003863 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003864 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003867 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003868 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003869 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003870 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003871
Hanno Becker44650b72018-08-16 12:51:11 +01003872 if( ssl_check_hs_header( ssl ) != 0 )
3873 {
3874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3875 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3876 }
3877
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003878 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003879 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3880 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3881 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3882 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003883 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003884 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3885 {
3886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3887 recv_msg_seq,
3888 ssl->handshake->in_msg_seq ) );
3889 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3890 }
3891
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003892 /* Retransmit only on last message from previous flight, to avoid
3893 * too many retransmissions.
3894 * Besides, No sane server ever retransmits HelloVerifyRequest */
3895 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003899 "message_seq = %d, start_of_flight = %d",
3900 recv_msg_seq,
3901 ssl->handshake->in_flight_start_seq ) );
3902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003906 return( ret );
3907 }
3908 }
3909 else
3910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003912 "message_seq = %d, expected = %d",
3913 recv_msg_seq,
3914 ssl->handshake->in_msg_seq ) );
3915 }
3916
Hanno Becker90333da2017-10-10 11:27:13 +01003917 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003918 }
3919 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003920
Hanno Becker6d97ef52018-08-16 13:09:04 +01003921 /* Message reassembly is handled alongside buffering of future
3922 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003923 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003924 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003925 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003928 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003929 }
3930 }
3931 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003933 /* With TLS we don't handle fragmentation (for now) */
3934 if( ssl->in_msglen < ssl->in_hslen )
3935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3937 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003938 }
3939
Simon Butcher99000142016-10-13 17:21:01 +01003940 return( 0 );
3941}
3942
3943void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3944{
Hanno Becker0271f962018-08-16 13:23:47 +01003945 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003946
Hanno Becker0271f962018-08-16 13:23:47 +01003947 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003948 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003949 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003950 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003951
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003952 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003954 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003955 ssl->handshake != NULL )
3956 {
Hanno Becker0271f962018-08-16 13:23:47 +01003957 unsigned offset;
3958 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003959
Hanno Becker0271f962018-08-16 13:23:47 +01003960 /* Increment handshake sequence number */
3961 hs->in_msg_seq++;
3962
3963 /*
3964 * Clear up handshake buffering and reassembly structure.
3965 */
3966
3967 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003968 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003969
3970 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003971 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3972 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003973 offset++, hs_buf++ )
3974 {
3975 *hs_buf = *(hs_buf + 1);
3976 }
3977
3978 /* Create a fresh last entry */
3979 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003980 }
3981#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003982}
3983
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003984/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003985 * DTLS anti-replay: RFC 6347 4.1.2.6
3986 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003987 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3988 * Bit n is set iff record number in_window_top - n has been seen.
3989 *
3990 * Usually, in_window_top is the last record number seen and the lsb of
3991 * in_window is set. The only exception is the initial state (record number 0
3992 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3995static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003996{
3997 ssl->in_window_top = 0;
3998 ssl->in_window = 0;
3999}
4000
4001static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4002{
4003 return( ( (uint64_t) buf[0] << 40 ) |
4004 ( (uint64_t) buf[1] << 32 ) |
4005 ( (uint64_t) buf[2] << 24 ) |
4006 ( (uint64_t) buf[3] << 16 ) |
4007 ( (uint64_t) buf[4] << 8 ) |
4008 ( (uint64_t) buf[5] ) );
4009}
4010
4011/*
4012 * Return 0 if sequence number is acceptable, -1 otherwise
4013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004015{
4016 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4017 uint64_t bit;
4018
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004019 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004020 return( 0 );
4021
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004022 if( rec_seqnum > ssl->in_window_top )
4023 return( 0 );
4024
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004025 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004026
4027 if( bit >= 64 )
4028 return( -1 );
4029
4030 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4031 return( -1 );
4032
4033 return( 0 );
4034}
4035
4036/*
4037 * Update replay window on new validated record
4038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004040{
4041 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4042
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004043 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004044 return;
4045
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004046 if( rec_seqnum > ssl->in_window_top )
4047 {
4048 /* Update window_top and the contents of the window */
4049 uint64_t shift = rec_seqnum - ssl->in_window_top;
4050
4051 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004052 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004053 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004054 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004055 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004056 ssl->in_window |= 1;
4057 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004058
4059 ssl->in_window_top = rec_seqnum;
4060 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004061 else
4062 {
4063 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004064 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004065
4066 if( bit < 64 ) /* Always true, but be extra sure */
4067 ssl->in_window |= (uint64_t) 1 << bit;
4068 }
4069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004071
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004072#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004073/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004074static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4075
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004076/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004077 * Without any SSL context, check if a datagram looks like a ClientHello with
4078 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004079 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004080 *
4081 * - if cookie is valid, return 0
4082 * - if ClientHello looks superficially valid but cookie is not,
4083 * fill obuf and set olen, then
4084 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4085 * - otherwise return a specific error code
4086 */
4087static int ssl_check_dtls_clihlo_cookie(
4088 mbedtls_ssl_cookie_write_t *f_cookie_write,
4089 mbedtls_ssl_cookie_check_t *f_cookie_check,
4090 void *p_cookie,
4091 const unsigned char *cli_id, size_t cli_id_len,
4092 const unsigned char *in, size_t in_len,
4093 unsigned char *obuf, size_t buf_len, size_t *olen )
4094{
4095 size_t sid_len, cookie_len;
4096 unsigned char *p;
4097
4098 if( f_cookie_write == NULL || f_cookie_check == NULL )
4099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4100
4101 /*
4102 * Structure of ClientHello with record and handshake headers,
4103 * and expected values. We don't need to check a lot, more checks will be
4104 * done when actually parsing the ClientHello - skipping those checks
4105 * avoids code duplication and does not make cookie forging any easier.
4106 *
4107 * 0-0 ContentType type; copied, must be handshake
4108 * 1-2 ProtocolVersion version; copied
4109 * 3-4 uint16 epoch; copied, must be 0
4110 * 5-10 uint48 sequence_number; copied
4111 * 11-12 uint16 length; (ignored)
4112 *
4113 * 13-13 HandshakeType msg_type; (ignored)
4114 * 14-16 uint24 length; (ignored)
4115 * 17-18 uint16 message_seq; copied
4116 * 19-21 uint24 fragment_offset; copied, must be 0
4117 * 22-24 uint24 fragment_length; (ignored)
4118 *
4119 * 25-26 ProtocolVersion client_version; (ignored)
4120 * 27-58 Random random; (ignored)
4121 * 59-xx SessionID session_id; 1 byte len + sid_len content
4122 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4123 * ...
4124 *
4125 * Minimum length is 61 bytes.
4126 */
4127 if( in_len < 61 ||
4128 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4129 in[3] != 0 || in[4] != 0 ||
4130 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4131 {
4132 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4133 }
4134
4135 sid_len = in[59];
4136 if( sid_len > in_len - 61 )
4137 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4138
4139 cookie_len = in[60 + sid_len];
4140 if( cookie_len > in_len - 60 )
4141 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4142
4143 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4144 cli_id, cli_id_len ) == 0 )
4145 {
4146 /* Valid cookie */
4147 return( 0 );
4148 }
4149
4150 /*
4151 * If we get here, we've got an invalid cookie, let's prepare HVR.
4152 *
4153 * 0-0 ContentType type; copied
4154 * 1-2 ProtocolVersion version; copied
4155 * 3-4 uint16 epoch; copied
4156 * 5-10 uint48 sequence_number; copied
4157 * 11-12 uint16 length; olen - 13
4158 *
4159 * 13-13 HandshakeType msg_type; hello_verify_request
4160 * 14-16 uint24 length; olen - 25
4161 * 17-18 uint16 message_seq; copied
4162 * 19-21 uint24 fragment_offset; copied
4163 * 22-24 uint24 fragment_length; olen - 25
4164 *
4165 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4166 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4167 *
4168 * Minimum length is 28.
4169 */
4170 if( buf_len < 28 )
4171 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4172
4173 /* Copy most fields and adapt others */
4174 memcpy( obuf, in, 25 );
4175 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4176 obuf[25] = 0xfe;
4177 obuf[26] = 0xff;
4178
4179 /* Generate and write actual cookie */
4180 p = obuf + 28;
4181 if( f_cookie_write( p_cookie,
4182 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4183 {
4184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4185 }
4186
4187 *olen = p - obuf;
4188
4189 /* Go back and fill length fields */
4190 obuf[27] = (unsigned char)( *olen - 28 );
4191
4192 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4193 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4194 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4195
4196 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4197 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4198
4199 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4200}
4201
4202/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004203 * Handle possible client reconnect with the same UDP quadruplet
4204 * (RFC 6347 Section 4.2.8).
4205 *
4206 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4207 * that looks like a ClientHello.
4208 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004209 * - if the input looks like a ClientHello without cookies,
4210 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004211 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004212 * - if the input looks like a ClientHello with a valid cookie,
4213 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004214 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004215 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004216 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004217 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004218 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4219 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004220 */
4221static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4222{
4223 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004224 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004225
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004226 ret = ssl_check_dtls_clihlo_cookie(
4227 ssl->conf->f_cookie_write,
4228 ssl->conf->f_cookie_check,
4229 ssl->conf->p_cookie,
4230 ssl->cli_id, ssl->cli_id_len,
4231 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004232 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004233
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004234 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4235
4236 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004237 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004238 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004239 * If the error is permanent we'll catch it later,
4240 * if it's not, then hopefully it'll work next time. */
4241 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4242
4243 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004244 }
4245
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004246 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004247 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004248 /* Got a valid cookie, partially reset context */
4249 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4250 {
4251 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4252 return( ret );
4253 }
4254
4255 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004256 }
4257
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004258 return( ret );
4259}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004260#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004261
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004262/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004263 * ContentType type;
4264 * ProtocolVersion version;
4265 * uint16 epoch; // DTLS only
4266 * uint48 sequence_number; // DTLS only
4267 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004268 *
4269 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004270 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004271 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4272 *
4273 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004274 * 1. proceed with the record if this function returns 0
4275 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4276 * 3. return CLIENT_RECONNECT if this function return that value
4277 * 4. drop the whole datagram if this function returns anything else.
4278 * Point 2 is needed when the peer is resending, and we have already received
4279 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004282{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004283 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285 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 +02004286
Paul Bakker5121ce52009-01-03 21:22:43 +00004287 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004288 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004289 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004292 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004293 ssl->in_msgtype,
4294 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004296 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4298 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4299 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4300 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004303
4304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004305 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4306 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004307 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4308#endif /* MBEDTLS_SSL_PROTO_DTLS */
4309 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4310 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004313 }
4314
4315 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004316 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4319 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004320 }
4321
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004322 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4325 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004326 }
4327
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004328 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004329 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004330 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4333 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004334 }
4335
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004336 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004337 * DTLS-related tests.
4338 * Check epoch before checking length constraint because
4339 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4340 * message gets duplicated before the corresponding Finished message,
4341 * the second ChangeCipherSpec should be discarded because it belongs
4342 * to an old epoch, but not because its length is shorter than
4343 * the minimum record length for packets using the new record transform.
4344 * Note that these two kinds of failures are handled differently,
4345 * as an unexpected record is silently skipped but an invalid
4346 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004347 */
4348#if defined(MBEDTLS_SSL_PROTO_DTLS)
4349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4350 {
4351 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4352
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004353 /* Check epoch (and sequence number) with DTLS */
4354 if( rec_epoch != ssl->in_epoch )
4355 {
4356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4357 "expected %d, received %d",
4358 ssl->in_epoch, rec_epoch ) );
4359
4360#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4361 /*
4362 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4363 * access the first byte of record content (handshake type), as we
4364 * have an active transform (possibly iv_len != 0), so use the
4365 * fact that the record header len is 13 instead.
4366 */
4367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4368 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4369 rec_epoch == 0 &&
4370 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4371 ssl->in_left > 13 &&
4372 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4373 {
4374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4375 "from the same port" ) );
4376 return( ssl_handle_possible_reconnect( ssl ) );
4377 }
4378 else
4379#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004380 {
4381 /* Consider buffering the record. */
4382 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4383 {
4384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4385 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4386 }
4387
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004388 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004389 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004390 }
4391
4392#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4393 /* Replay detection only works for the current epoch */
4394 if( rec_epoch == ssl->in_epoch &&
4395 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4396 {
4397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4398 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4399 }
4400#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004401
Hanno Becker52c6dc62017-05-26 16:07:36 +01004402 /* Drop unexpected ApplicationData records,
4403 * except at the beginning of renegotiations */
4404 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4405 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4406#if defined(MBEDTLS_SSL_RENEGOTIATION)
4407 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4408 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4409#endif
4410 )
4411 {
4412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4413 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4414 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004415 }
4416#endif /* MBEDTLS_SSL_PROTO_DTLS */
4417
Hanno Becker52c6dc62017-05-26 16:07:36 +01004418
4419 /* Check length against bounds of the current transform and version */
4420 if( ssl->transform_in == NULL )
4421 {
4422 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004423 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004424 {
4425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4426 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4427 }
4428 }
4429 else
4430 {
4431 if( ssl->in_msglen < ssl->transform_in->minlen )
4432 {
4433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4435 }
4436
4437#if defined(MBEDTLS_SSL_PROTO_SSL3)
4438 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004439 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004440 {
4441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4443 }
4444#endif
4445#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4446 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4447 /*
4448 * TLS encrypted messages can have up to 256 bytes of padding
4449 */
4450 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4451 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004452 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004453 {
4454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4455 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4456 }
4457#endif
4458 }
4459
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004460 return( 0 );
4461}
Paul Bakker5121ce52009-01-03 21:22:43 +00004462
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004463/*
4464 * If applicable, decrypt (and decompress) record content
4465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004467{
4468 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4471 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4474 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 ret = mbedtls_ssl_hw_record_read( ssl );
4479 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4482 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004483 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004484
4485 if( ret == 0 )
4486 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004489 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 {
4491 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 return( ret );
4495 }
4496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 ssl->in_msg, ssl->in_msglen );
4499
Angus Grattond8213d02016-05-25 20:56:48 +10004500 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4503 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 }
4505 }
4506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004508 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004510 {
4511 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004514 return( ret );
4515 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004516 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004523 }
4524#endif
4525
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004526 return( 0 );
4527}
4528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004530
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004531/*
4532 * Read a record.
4533 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004534 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4535 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4536 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004537 */
Hanno Becker1097b342018-08-15 14:09:41 +01004538
4539/* Helper functions for mbedtls_ssl_read_record(). */
4540static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004541static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4542static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004543
Hanno Becker327c93b2018-08-15 13:56:18 +01004544int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004545 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004546{
4547 int ret;
4548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004550
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004551 if( ssl->keep_current_message == 0 )
4552 {
4553 do {
Simon Butcher99000142016-10-13 17:21:01 +01004554
Hanno Becker26994592018-08-15 14:14:59 +01004555 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004556 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004557 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004558
Hanno Beckere74d5562018-08-15 14:26:08 +01004559 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004560 {
Hanno Becker40f50842018-08-15 14:48:01 +01004561#if defined(MBEDTLS_SSL_PROTO_DTLS)
4562 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004563
Hanno Becker40f50842018-08-15 14:48:01 +01004564 /* We only check for buffered messages if the
4565 * current datagram is fully consumed. */
4566 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004567 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004568 {
Hanno Becker40f50842018-08-15 14:48:01 +01004569 if( ssl_load_buffered_message( ssl ) == 0 )
4570 have_buffered = 1;
4571 }
4572
4573 if( have_buffered == 0 )
4574#endif /* MBEDTLS_SSL_PROTO_DTLS */
4575 {
4576 ret = ssl_get_next_record( ssl );
4577 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4578 continue;
4579
4580 if( ret != 0 )
4581 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004582 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004583 return( ret );
4584 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004585 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004586 }
4587
4588 ret = mbedtls_ssl_handle_message_type( ssl );
4589
Hanno Becker40f50842018-08-15 14:48:01 +01004590#if defined(MBEDTLS_SSL_PROTO_DTLS)
4591 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4592 {
4593 /* Buffer future message */
4594 ret = ssl_buffer_message( ssl );
4595 if( ret != 0 )
4596 return( ret );
4597
4598 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4599 }
4600#endif /* MBEDTLS_SSL_PROTO_DTLS */
4601
Hanno Becker90333da2017-10-10 11:27:13 +01004602 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4603 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004604
4605 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004606 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004607 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004608 return( ret );
4609 }
4610
Hanno Becker327c93b2018-08-15 13:56:18 +01004611 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004612 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004613 {
4614 mbedtls_ssl_update_handshake_status( ssl );
4615 }
Simon Butcher99000142016-10-13 17:21:01 +01004616 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004617 else
Simon Butcher99000142016-10-13 17:21:01 +01004618 {
Hanno Becker02f59072018-08-15 14:00:24 +01004619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004620 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004621 }
4622
4623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4624
4625 return( 0 );
4626}
4627
Hanno Becker40f50842018-08-15 14:48:01 +01004628#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004629static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004630{
Hanno Becker40f50842018-08-15 14:48:01 +01004631 if( ssl->in_left > ssl->next_record_offset )
4632 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004633
Hanno Becker40f50842018-08-15 14:48:01 +01004634 return( 0 );
4635}
4636
4637static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4638{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004639 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004640 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004641 int ret = 0;
4642
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004643 if( hs == NULL )
4644 return( -1 );
4645
Hanno Beckere00ae372018-08-20 09:39:42 +01004646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4647
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004648 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4649 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4650 {
4651 /* Check if we have seen a ChangeCipherSpec before.
4652 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004653 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004654 {
4655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4656 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004657 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004658 }
4659
Hanno Becker39b8bc92018-08-28 17:17:13 +01004660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004661 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4662 ssl->in_msglen = 1;
4663 ssl->in_msg[0] = 1;
4664
4665 /* As long as they are equal, the exact value doesn't matter. */
4666 ssl->in_left = 0;
4667 ssl->next_record_offset = 0;
4668
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004669 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004670 goto exit;
4671 }
Hanno Becker37f95322018-08-16 13:55:32 +01004672
Hanno Beckerb8f50142018-08-28 10:01:34 +01004673#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004674 /* Debug only */
4675 {
4676 unsigned offset;
4677 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4678 {
4679 hs_buf = &hs->buffering.hs[offset];
4680 if( hs_buf->is_valid == 1 )
4681 {
4682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4683 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004684 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004685 }
4686 }
4687 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004688#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004689
4690 /* Check if we have buffered and/or fully reassembled the
4691 * next handshake message. */
4692 hs_buf = &hs->buffering.hs[0];
4693 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4694 {
4695 /* Synthesize a record containing the buffered HS message. */
4696 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4697 ( hs_buf->data[2] << 8 ) |
4698 hs_buf->data[3];
4699
4700 /* Double-check that we haven't accidentally buffered
4701 * a message that doesn't fit into the input buffer. */
4702 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4703 {
4704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4706 }
4707
4708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4709 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4710 hs_buf->data, msg_len + 12 );
4711
4712 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4713 ssl->in_hslen = msg_len + 12;
4714 ssl->in_msglen = msg_len + 12;
4715 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4716
4717 ret = 0;
4718 goto exit;
4719 }
4720 else
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4723 hs->in_msg_seq ) );
4724 }
4725
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004726 ret = -1;
4727
4728exit:
4729
4730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4731 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004732}
4733
Hanno Beckera02b0b42018-08-21 17:20:27 +01004734static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4735 size_t desired )
4736{
4737 int offset;
4738 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4740 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004741
Hanno Becker01315ea2018-08-21 17:22:17 +01004742 /* Get rid of future records epoch first, if such exist. */
4743 ssl_free_buffered_record( ssl );
4744
4745 /* Check if we have enough space available now. */
4746 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4747 hs->buffering.total_bytes_buffered ) )
4748 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004750 return( 0 );
4751 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004752
Hanno Becker4f432ad2018-08-28 10:02:32 +01004753 /* We don't have enough space to buffer the next expected handshake
4754 * message. Remove buffers used for future messages to gain space,
4755 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004756 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4757 offset >= 0; offset-- )
4758 {
4759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4760 offset ) );
4761
Hanno Beckerb309b922018-08-23 13:18:05 +01004762 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004763
4764 /* Check if we have enough space available now. */
4765 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4766 hs->buffering.total_bytes_buffered ) )
4767 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004769 return( 0 );
4770 }
4771 }
4772
4773 return( -1 );
4774}
4775
Hanno Becker40f50842018-08-15 14:48:01 +01004776static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4777{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004778 int ret = 0;
4779 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4780
4781 if( hs == NULL )
4782 return( 0 );
4783
4784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4785
4786 switch( ssl->in_msgtype )
4787 {
4788 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004790
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004791 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004792 break;
4793
4794 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004795 {
4796 unsigned recv_msg_seq_offset;
4797 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4798 mbedtls_ssl_hs_buffer *hs_buf;
4799 size_t msg_len = ssl->in_hslen - 12;
4800
4801 /* We should never receive an old handshake
4802 * message - double-check nonetheless. */
4803 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4804 {
4805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4807 }
4808
4809 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4810 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4811 {
4812 /* Silently ignore -- message too far in the future */
4813 MBEDTLS_SSL_DEBUG_MSG( 2,
4814 ( "Ignore future HS message with sequence number %u, "
4815 "buffering window %u - %u",
4816 recv_msg_seq, ssl->handshake->in_msg_seq,
4817 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4818
4819 goto exit;
4820 }
4821
4822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4823 recv_msg_seq, recv_msg_seq_offset ) );
4824
4825 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4826
4827 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004828 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004829 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004830 size_t reassembly_buf_sz;
4831
Hanno Becker37f95322018-08-16 13:55:32 +01004832 hs_buf->is_fragmented =
4833 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4834
4835 /* We copy the message back into the input buffer
4836 * after reassembly, so check that it's not too large.
4837 * This is an implementation-specific limitation
4838 * and not one from the standard, hence it is not
4839 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004840 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004841 {
4842 /* Ignore message */
4843 goto exit;
4844 }
4845
Hanno Beckere0b150f2018-08-21 15:51:03 +01004846 /* Check if we have enough space to buffer the message. */
4847 if( hs->buffering.total_bytes_buffered >
4848 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4849 {
4850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4851 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4852 }
4853
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004854 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4855 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004856
4857 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4858 hs->buffering.total_bytes_buffered ) )
4859 {
4860 if( recv_msg_seq_offset > 0 )
4861 {
4862 /* If we can't buffer a future message because
4863 * of space limitations -- ignore. */
4864 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",
4865 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4866 (unsigned) hs->buffering.total_bytes_buffered ) );
4867 goto exit;
4868 }
Hanno Beckere1801392018-08-21 16:51:05 +01004869 else
4870 {
4871 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",
4872 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4873 (unsigned) hs->buffering.total_bytes_buffered ) );
4874 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004875
Hanno Beckera02b0b42018-08-21 17:20:27 +01004876 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004877 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004878 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",
4879 (unsigned) msg_len,
4880 (unsigned) reassembly_buf_sz,
4881 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004882 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004883 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4884 goto exit;
4885 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004886 }
4887
4888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4889 msg_len ) );
4890
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004891 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4892 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004893 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004894 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004895 goto exit;
4896 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004897 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004898
4899 /* Prepare final header: copy msg_type, length and message_seq,
4900 * then add standardised fragment_offset and fragment_length */
4901 memcpy( hs_buf->data, ssl->in_msg, 6 );
4902 memset( hs_buf->data + 6, 0, 3 );
4903 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4904
4905 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004906
4907 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004908 }
4909 else
4910 {
4911 /* Make sure msg_type and length are consistent */
4912 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4913 {
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4915 /* Ignore */
4916 goto exit;
4917 }
4918 }
4919
Hanno Becker4422bbb2018-08-20 09:40:19 +01004920 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004921 {
4922 size_t frag_len, frag_off;
4923 unsigned char * const msg = hs_buf->data + 12;
4924
4925 /*
4926 * Check and copy current fragment
4927 */
4928
4929 /* Validation of header fields already done in
4930 * mbedtls_ssl_prepare_handshake_record(). */
4931 frag_off = ssl_get_hs_frag_off( ssl );
4932 frag_len = ssl_get_hs_frag_len( ssl );
4933
4934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4935 frag_off, frag_len ) );
4936 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4937
4938 if( hs_buf->is_fragmented )
4939 {
4940 unsigned char * const bitmask = msg + msg_len;
4941 ssl_bitmask_set( bitmask, frag_off, frag_len );
4942 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4943 msg_len ) == 0 );
4944 }
4945 else
4946 {
4947 hs_buf->is_complete = 1;
4948 }
4949
4950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4951 hs_buf->is_complete ? "" : "not yet " ) );
4952 }
4953
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004954 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004955 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004956
4957 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004958 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004959 break;
4960 }
4961
4962exit:
4963
4964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4965 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004966}
4967#endif /* MBEDTLS_SSL_PROTO_DTLS */
4968
Hanno Becker1097b342018-08-15 14:09:41 +01004969static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004970{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004971 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004972 * Consume last content-layer message and potentially
4973 * update in_msglen which keeps track of the contents'
4974 * consumption state.
4975 *
4976 * (1) Handshake messages:
4977 * Remove last handshake message, move content
4978 * and adapt in_msglen.
4979 *
4980 * (2) Alert messages:
4981 * Consume whole record content, in_msglen = 0.
4982 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004983 * (3) Change cipher spec:
4984 * Consume whole record content, in_msglen = 0.
4985 *
4986 * (4) Application data:
4987 * Don't do anything - the record layer provides
4988 * the application data as a stream transport
4989 * and consumes through mbedtls_ssl_read only.
4990 *
4991 */
4992
4993 /* Case (1): Handshake messages */
4994 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004995 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004996 /* Hard assertion to be sure that no application data
4997 * is in flight, as corrupting ssl->in_msglen during
4998 * ssl->in_offt != NULL is fatal. */
4999 if( ssl->in_offt != NULL )
5000 {
5001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5003 }
5004
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005005 /*
5006 * Get next Handshake message in the current record
5007 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005008
Hanno Becker4a810fb2017-05-24 16:27:30 +01005009 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005010 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005011 * current handshake content: If DTLS handshake
5012 * fragmentation is used, that's the fragment
5013 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005014 * size here is faulty and should be changed at
5015 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005016 * (2) While it doesn't seem to cause problems, one
5017 * has to be very careful not to assume that in_hslen
5018 * is always <= in_msglen in a sensible communication.
5019 * Again, it's wrong for DTLS handshake fragmentation.
5020 * The following check is therefore mandatory, and
5021 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005022 * Additionally, ssl->in_hslen might be arbitrarily out of
5023 * bounds after handling a DTLS message with an unexpected
5024 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005025 */
5026 if( ssl->in_hslen < ssl->in_msglen )
5027 {
5028 ssl->in_msglen -= ssl->in_hslen;
5029 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5030 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005031
Hanno Becker4a810fb2017-05-24 16:27:30 +01005032 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5033 ssl->in_msg, ssl->in_msglen );
5034 }
5035 else
5036 {
5037 ssl->in_msglen = 0;
5038 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005039
Hanno Becker4a810fb2017-05-24 16:27:30 +01005040 ssl->in_hslen = 0;
5041 }
5042 /* Case (4): Application data */
5043 else if( ssl->in_offt != NULL )
5044 {
5045 return( 0 );
5046 }
5047 /* Everything else (CCS & Alerts) */
5048 else
5049 {
5050 ssl->in_msglen = 0;
5051 }
5052
Hanno Becker1097b342018-08-15 14:09:41 +01005053 return( 0 );
5054}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005055
Hanno Beckere74d5562018-08-15 14:26:08 +01005056static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5057{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005058 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005059 return( 1 );
5060
5061 return( 0 );
5062}
5063
Hanno Becker5f066e72018-08-16 14:56:31 +01005064#if defined(MBEDTLS_SSL_PROTO_DTLS)
5065
5066static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5067{
5068 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5069 if( hs == NULL )
5070 return;
5071
Hanno Becker01315ea2018-08-21 17:22:17 +01005072 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005073 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005074 hs->buffering.total_bytes_buffered -=
5075 hs->buffering.future_record.len;
5076
5077 mbedtls_free( hs->buffering.future_record.data );
5078 hs->buffering.future_record.data = NULL;
5079 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005080}
5081
5082static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5083{
5084 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5085 unsigned char * rec;
5086 size_t rec_len;
5087 unsigned rec_epoch;
5088
5089 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5090 return( 0 );
5091
5092 if( hs == NULL )
5093 return( 0 );
5094
Hanno Becker5f066e72018-08-16 14:56:31 +01005095 rec = hs->buffering.future_record.data;
5096 rec_len = hs->buffering.future_record.len;
5097 rec_epoch = hs->buffering.future_record.epoch;
5098
5099 if( rec == NULL )
5100 return( 0 );
5101
Hanno Becker4cb782d2018-08-20 11:19:05 +01005102 /* Only consider loading future records if the
5103 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005104 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005105 return( 0 );
5106
Hanno Becker5f066e72018-08-16 14:56:31 +01005107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5108
5109 if( rec_epoch != ssl->in_epoch )
5110 {
5111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5112 goto exit;
5113 }
5114
5115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5116
5117 /* Double-check that the record is not too large */
5118 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5119 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5120 {
5121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5123 }
5124
5125 memcpy( ssl->in_hdr, rec, rec_len );
5126 ssl->in_left = rec_len;
5127 ssl->next_record_offset = 0;
5128
5129 ssl_free_buffered_record( ssl );
5130
5131exit:
5132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5133 return( 0 );
5134}
5135
5136static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5137{
5138 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5139 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005140 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005141
5142 /* Don't buffer future records outside handshakes. */
5143 if( hs == NULL )
5144 return( 0 );
5145
5146 /* Only buffer handshake records (we are only interested
5147 * in Finished messages). */
5148 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5149 return( 0 );
5150
5151 /* Don't buffer more than one future epoch record. */
5152 if( hs->buffering.future_record.data != NULL )
5153 return( 0 );
5154
Hanno Becker01315ea2018-08-21 17:22:17 +01005155 /* Don't buffer record if there's not enough buffering space remaining. */
5156 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5157 hs->buffering.total_bytes_buffered ) )
5158 {
5159 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",
5160 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5161 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005162 return( 0 );
5163 }
5164
Hanno Becker5f066e72018-08-16 14:56:31 +01005165 /* Buffer record */
5166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5167 ssl->in_epoch + 1 ) );
5168 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5169 rec_hdr_len + ssl->in_msglen );
5170
5171 /* ssl_parse_record_header() only considers records
5172 * of the next epoch as candidates for buffering. */
5173 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005174 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005175
5176 hs->buffering.future_record.data =
5177 mbedtls_calloc( 1, hs->buffering.future_record.len );
5178 if( hs->buffering.future_record.data == NULL )
5179 {
5180 /* If we run out of RAM trying to buffer a
5181 * record from the next epoch, just ignore. */
5182 return( 0 );
5183 }
5184
Hanno Becker01315ea2018-08-21 17:22:17 +01005185 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005186
Hanno Becker01315ea2018-08-21 17:22:17 +01005187 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005188 return( 0 );
5189}
5190
5191#endif /* MBEDTLS_SSL_PROTO_DTLS */
5192
Hanno Beckere74d5562018-08-15 14:26:08 +01005193static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005194{
5195 int ret;
5196
Hanno Becker5f066e72018-08-16 14:56:31 +01005197#if defined(MBEDTLS_SSL_PROTO_DTLS)
5198 /* We might have buffered a future record; if so,
5199 * and if the epoch matches now, load it.
5200 * On success, this call will set ssl->in_left to
5201 * the length of the buffered record, so that
5202 * the calls to ssl_fetch_input() below will
5203 * essentially be no-ops. */
5204 ret = ssl_load_buffered_record( ssl );
5205 if( ret != 0 )
5206 return( ret );
5207#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005212 return( ret );
5213 }
5214
5215 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5219 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005220 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005221 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5222 {
5223 ret = ssl_buffer_future_record( ssl );
5224 if( ret != 0 )
5225 return( ret );
5226
5227 /* Fall through to handling of unexpected records */
5228 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5229 }
5230
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005231 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5232 {
5233 /* Skip unexpected record (but not whole datagram) */
5234 ssl->next_record_offset = ssl->in_msglen
5235 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005236
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5238 "(header)" ) );
5239 }
5240 else
5241 {
5242 /* Skip invalid record and the rest of the datagram */
5243 ssl->next_record_offset = 0;
5244 ssl->in_left = 0;
5245
5246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5247 "(header)" ) );
5248 }
5249
5250 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005251 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005252 }
5253#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005254 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005255 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005256
5257 /*
5258 * Read and optionally decrypt the message contents
5259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5261 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005264 return( ret );
5265 }
5266
5267 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005272 if( ssl->next_record_offset < ssl->in_left )
5273 {
5274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5275 }
5276 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005277 else
5278#endif
5279 ssl->in_left = 0;
5280
5281 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005285 {
5286 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5288 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005289 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005290 /* Except when waiting for Finished as a bad mac here
5291 * probably means something went wrong in the handshake
5292 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5293 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5294 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5295 {
5296#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5297 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5298 {
5299 mbedtls_ssl_send_alert_message( ssl,
5300 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5301 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5302 }
5303#endif
5304 return( ret );
5305 }
5306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005308 if( ssl->conf->badmac_limit != 0 &&
5309 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5312 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005313 }
5314#endif
5315
Hanno Becker4a810fb2017-05-24 16:27:30 +01005316 /* As above, invalid records cause
5317 * dismissal of the whole datagram. */
5318
5319 ssl->next_record_offset = 0;
5320 ssl->in_left = 0;
5321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005323 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005324 }
5325
5326 return( ret );
5327 }
5328 else
5329#endif
5330 {
5331 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5333 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335 mbedtls_ssl_send_alert_message( ssl,
5336 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5337 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005338 }
5339#endif
5340 return( ret );
5341 }
5342 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005343
Simon Butcher99000142016-10-13 17:21:01 +01005344 return( 0 );
5345}
5346
5347int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5348{
5349 int ret;
5350
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005351 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005352 * Handle particular types of records
5353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005355 {
Simon Butcher99000142016-10-13 17:21:01 +01005356 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5357 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005358 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005359 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005360 }
5361
Hanno Beckere678eaa2018-08-21 14:57:46 +01005362 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005363 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005364 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005365 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5367 ssl->in_msglen ) );
5368 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005369 }
5370
Hanno Beckere678eaa2018-08-21 14:57:46 +01005371 if( ssl->in_msg[0] != 1 )
5372 {
5373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5374 ssl->in_msg[0] ) );
5375 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5376 }
5377
5378#if defined(MBEDTLS_SSL_PROTO_DTLS)
5379 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5380 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5381 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5382 {
5383 if( ssl->handshake == NULL )
5384 {
5385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5386 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5387 }
5388
5389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5390 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5391 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005392#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005393 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005397 if( ssl->in_msglen != 2 )
5398 {
5399 /* Note: Standard allows for more than one 2 byte alert
5400 to be packed in a single message, but Mbed TLS doesn't
5401 currently support this. */
5402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5403 ssl->in_msglen ) );
5404 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5405 }
5406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005408 ssl->in_msg[0], ssl->in_msg[1] ) );
5409
5410 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005411 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005416 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005418 }
5419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5421 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5424 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005425 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005426
5427#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5428 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5429 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5430 {
Hanno Becker90333da2017-10-10 11:27:13 +01005431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005432 /* Will be handled when trying to parse ServerHello */
5433 return( 0 );
5434 }
5435#endif
5436
5437#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5438 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5439 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5440 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5441 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5442 {
5443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5444 /* Will be handled in mbedtls_ssl_parse_certificate() */
5445 return( 0 );
5446 }
5447#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5448
5449 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005450 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 }
5452
Hanno Beckerc76c6192017-06-06 10:03:17 +01005453#if defined(MBEDTLS_SSL_PROTO_DTLS)
5454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5455 ssl->handshake != NULL &&
5456 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5457 {
5458 ssl_handshake_wrapup_free_hs_transform( ssl );
5459 }
5460#endif
5461
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 return( 0 );
5463}
5464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005466{
5467 int ret;
5468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5470 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5471 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005472 {
5473 return( ret );
5474 }
5475
5476 return( 0 );
5477}
5478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005480 unsigned char level,
5481 unsigned char message )
5482{
5483 int ret;
5484
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005485 if( ssl == NULL || ssl->conf == NULL )
5486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005492 ssl->out_msglen = 2;
5493 ssl->out_msg[0] = level;
5494 ssl->out_msg[1] = message;
5495
Hanno Becker67bc7c32018-08-06 11:33:50 +01005496 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005499 return( ret );
5500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005502
5503 return( 0 );
5504}
5505
Paul Bakker5121ce52009-01-03 21:22:43 +00005506/*
5507 * Handshake functions
5508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5510 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5511 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5512 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5513 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5514 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5515 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005516/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5524 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005525 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5526 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005529 ssl->state++;
5530 return( 0 );
5531 }
5532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005535}
5536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5544 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005545 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5546 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005549 ssl->state++;
5550 return( 0 );
5551 }
5552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005555}
Gilles Peskinef9828522017-05-03 12:28:43 +02005556
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005557#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005558/* Some certificate support -> implement write and parse */
5559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005563 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 const mbedtls_x509_crt *crt;
5565 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5570 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005571 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5572 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005575 ssl->state++;
5576 return( 0 );
5577 }
5578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005580 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 {
5582 if( ssl->client_auth == 0 )
5583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005585 ssl->state++;
5586 return( 0 );
5587 }
5588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 /*
5591 * If using SSLv3 and got no cert, send an Alert message
5592 * (otherwise an empty Certificate message will be sent).
5593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5595 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005596 {
5597 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5599 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5600 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 goto write_msg;
5604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607#endif /* MBEDTLS_SSL_CLI_C */
5608#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005609 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5614 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005615 }
5616 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005617#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005620
5621 /*
5622 * 0 . 0 handshake type
5623 * 1 . 3 handshake length
5624 * 4 . 6 length of all certs
5625 * 7 . 9 length of cert. 1
5626 * 10 . n-1 peer certificate
5627 * n . n+2 length of cert. 2
5628 * n+3 . ... upper level cert, etc.
5629 */
5630 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005632
Paul Bakker29087132010-03-21 21:03:34 +00005633 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 {
5635 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005636 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005639 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 }
5642
5643 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5644 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5645 ssl->out_msg[i + 2] = (unsigned char)( n );
5646
5647 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5648 i += n; crt = crt->next;
5649 }
5650
5651 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5652 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5653 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5654
5655 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5657 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005658
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005659#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005660write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005661#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005662
5663 ssl->state++;
5664
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005665 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 return( ret );
5669 }
5670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005672
Paul Bakkered27a042013-04-18 22:46:23 +02005673 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005674}
5675
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005676/*
5677 * Once the certificate message is read, parse it into a cert chain and
5678 * perform basic checks, but leave actual verification to the caller
5679 */
5680static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005681{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005682 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00005683 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005684 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686#if defined(MBEDTLS_SSL_SRV_C)
5687#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 /*
5689 * Check if the client sent an empty certificate
5690 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005691 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005693 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00005694 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5696 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5697 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005700
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005701 /* The client was asked for a certificate but didn't send
5702 one. The client should know what's going on, so we
5703 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005704 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005705 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005706 }
5707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5711 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005712 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5716 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5717 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5718 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005721
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005722 /* The client was asked for a certificate but didn't send
5723 one. The client should know what's going on, so we
5724 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005725 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005726 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005727 }
5728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5730 MBEDTLS_SSL_PROTO_TLS1_2 */
5731#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005736 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5737 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005739 }
5740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5742 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005745 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5746 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005748 }
5749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005751
Paul Bakker5121ce52009-01-03 21:22:43 +00005752 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005754 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005755 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005756
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005757 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005761 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5762 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005764 }
5765
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005766 /* In case we tried to reuse a session but it failed */
5767 if( ssl->session_negotiate->peer_cert != NULL )
5768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5770 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005771 }
5772
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005773 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005775 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005778 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5779 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005780 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005781 }
5782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005784
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005785 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005786
5787 while( i < ssl->in_hslen )
5788 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005789 if ( i + 3 > ssl->in_hslen ) {
5790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5792 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5793 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005795 if( ssl->in_msg[i] != 0 )
5796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005798 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5799 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005801 }
5802
5803 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5804 | (unsigned int) ssl->in_msg[i + 2];
5805 i += 3;
5806
5807 if( n < 128 || i + n > ssl->in_hslen )
5808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005810 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5811 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005813 }
5814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005816 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005817 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005818 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005819 case 0: /*ok*/
5820 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5821 /* Ignore certificate with an unknown algorithm: maybe a
5822 prior certificate was already trusted. */
5823 break;
5824
5825 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5826 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5827 goto crt_parse_der_failed;
5828
5829 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5830 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5831 goto crt_parse_der_failed;
5832
5833 default:
5834 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5835 crt_parse_der_failed:
5836 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005838 return( ret );
5839 }
5840
5841 i += n;
5842 }
5843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005845
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005846 /*
5847 * On client, make sure the server cert doesn't change during renego to
5848 * avoid "triple handshake" attack: https://secure-resumption.com/
5849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005851 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005853 {
5854 if( ssl->session->peer_cert == NULL )
5855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005857 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5858 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005860 }
5861
5862 if( ssl->session->peer_cert->raw.len !=
5863 ssl->session_negotiate->peer_cert->raw.len ||
5864 memcmp( ssl->session->peer_cert->raw.p,
5865 ssl->session_negotiate->peer_cert->raw.p,
5866 ssl->session->peer_cert->raw.len ) != 0 )
5867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005869 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5870 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005872 }
5873 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005875
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005876 return( 0 );
5877}
5878
5879int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5880{
5881 int ret;
5882 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5883 ssl->transform_negotiate->ciphersuite_info;
5884#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5885 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5886 ? ssl->handshake->sni_authmode
5887 : ssl->conf->authmode;
5888#else
5889 const int authmode = ssl->conf->authmode;
5890#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005891 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005892
5893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5894
5895 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5896 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5897 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5898 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5899 {
5900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5901 ssl->state++;
5902 return( 0 );
5903 }
5904
5905#if defined(MBEDTLS_SSL_SRV_C)
5906 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5907 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5908 {
5909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5910 ssl->state++;
5911 return( 0 );
5912 }
5913
5914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5915 authmode == MBEDTLS_SSL_VERIFY_NONE )
5916 {
5917 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005919
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005920 ssl->state++;
5921 return( 0 );
5922 }
5923#endif
5924
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005925#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5926 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005927 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005928 {
5929 goto crt_verify;
5930 }
5931#endif
5932
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02005933 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005934 {
5935 /* mbedtls_ssl_read_record may have sent an alert already. We
5936 let it decide whether to alert. */
5937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5938 return( ret );
5939 }
5940
5941 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
5942 {
5943#if defined(MBEDTLS_SSL_SRV_C)
5944 if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE &&
5945 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
5946 {
5947 ret = 0;
5948 }
5949#endif
5950
5951 ssl->state++;
5952 return( ret );
5953 }
5954
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005955#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5956 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02005957 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005958
5959crt_verify:
5960 if( ssl->handshake->ecrs_enabled)
5961 rs_ctx = &ssl->handshake->ecrs_ctx;
5962#endif
5963
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005964 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005965 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005966 mbedtls_x509_crt *ca_chain;
5967 mbedtls_x509_crl *ca_crl;
5968
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005969#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005970 if( ssl->handshake->sni_ca_chain != NULL )
5971 {
5972 ca_chain = ssl->handshake->sni_ca_chain;
5973 ca_crl = ssl->handshake->sni_ca_crl;
5974 }
5975 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005976#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005977 {
5978 ca_chain = ssl->conf->ca_chain;
5979 ca_crl = ssl->conf->ca_crl;
5980 }
5981
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005982 /*
5983 * Main check: verify certificate
5984 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005985 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005986 ssl->session_negotiate->peer_cert,
5987 ca_chain, ca_crl,
5988 ssl->conf->cert_profile,
5989 ssl->hostname,
5990 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005991 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00005992
5993 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005996 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005997
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005998#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
5999 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006000 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006001#endif
6002
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006003 /*
6004 * Secondary checks: always done, but change 'ret' only if it was 0
6005 */
6006
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006007#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006010
6011 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006013 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006014 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006015 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006018 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006020 }
6021 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006022#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006025 ciphersuite_info,
6026 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006027 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006030 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006032 }
6033
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006034 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6035 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6036 * with details encoded in the verification flags. All other kinds
6037 * of error codes, including those from the user provided f_vrfy
6038 * functions, are treated as fatal and lead to a failure of
6039 * ssl_parse_certificate even if verification was optional. */
6040 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6041 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6042 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6043 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006044 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006045 }
6046
6047 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6048 {
6049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6050 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6051 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006052
6053 if( ret != 0 )
6054 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006055 uint8_t alert;
6056
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006057 /* The certificate may have been rejected for several reasons.
6058 Pick one and send the corresponding alert. Which alert to send
6059 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006060 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6061 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6062 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6063 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6064 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6065 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6066 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6067 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6068 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6069 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6070 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6071 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6072 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6073 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6074 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6075 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6076 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6077 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6078 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6079 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006080 else
6081 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006082 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6083 alert );
6084 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006085
Hanno Beckere6706e62017-05-15 16:05:15 +01006086#if defined(MBEDTLS_DEBUG_C)
6087 if( ssl->session_negotiate->verify_result != 0 )
6088 {
6089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6090 ssl->session_negotiate->verify_result ) );
6091 }
6092 else
6093 {
6094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6095 }
6096#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006097 }
6098
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006099 ssl->state++;
6100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006102
6103 return( ret );
6104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6106 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6107 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6108 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6109 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6110 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6111 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006114{
6115 int ret;
6116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006120 ssl->out_msglen = 1;
6121 ssl->out_msg[0] = 1;
6122
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 ssl->state++;
6124
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006125 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 return( ret );
6129 }
6130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006132
6133 return( 0 );
6134}
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006137{
6138 int ret;
6139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006141
Hanno Becker327c93b2018-08-15 13:56:18 +01006142 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 return( ret );
6146 }
6147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006151 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6152 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006154 }
6155
Hanno Beckere678eaa2018-08-21 14:57:46 +01006156 /* CCS records are only accepted if they have length 1 and content '1',
6157 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006158
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006159 /*
6160 * Switch to our negotiated transform and session parameters for inbound
6161 * data.
6162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006164 ssl->transform_in = ssl->transform_negotiate;
6165 ssl->session_in = ssl->session_negotiate;
6166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006171 ssl_dtls_replay_reset( ssl );
6172#endif
6173
6174 /* Increment epoch */
6175 if( ++ssl->in_epoch == 0 )
6176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006178 /* This is highly unlikely to happen for legitimate reasons, so
6179 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006181 }
6182 }
6183 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006185 memset( ssl->in_ctr, 0, 8 );
6186
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006187 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6190 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006195 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6196 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006198 }
6199 }
6200#endif
6201
Paul Bakker5121ce52009-01-03 21:22:43 +00006202 ssl->state++;
6203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006205
6206 return( 0 );
6207}
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6210 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006211{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006212 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6215 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6216 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006217 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006218 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6221#if defined(MBEDTLS_SHA512_C)
6222 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006223 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6224 else
6225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226#if defined(MBEDTLS_SHA256_C)
6227 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006228 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006229 else
6230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006234 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006235 }
Paul Bakker380da532012-04-18 16:10:25 +00006236}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6241 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006242 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6243 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6246#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006247 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006250 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006253}
6254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006256 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6259 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006260 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6261 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6264#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006265 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006268 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006271}
6272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6274 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6275static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006276 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006277{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006278 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6279 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006280}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006281#endif
Paul Bakker380da532012-04-18 16:10:25 +00006282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6284#if defined(MBEDTLS_SHA256_C)
6285static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006286 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006287{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006288 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006289}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006290#endif
Paul Bakker380da532012-04-18 16:10:25 +00006291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292#if defined(MBEDTLS_SHA512_C)
6293static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006294 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006295{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006296 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006297}
Paul Bakker769075d2012-11-24 11:26:46 +01006298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006302static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006304{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006305 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006306 mbedtls_md5_context md5;
6307 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006308
Paul Bakker5121ce52009-01-03 21:22:43 +00006309 unsigned char padbuf[48];
6310 unsigned char md5sum[16];
6311 unsigned char sha1sum[20];
6312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006314 if( !session )
6315 session = ssl->session;
6316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006318
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006319 mbedtls_md5_init( &md5 );
6320 mbedtls_sha1_init( &sha1 );
6321
6322 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6323 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006324
6325 /*
6326 * SSLv3:
6327 * hash =
6328 * MD5( master + pad2 +
6329 * MD5( handshake + sender + master + pad1 ) )
6330 * + SHA1( master + pad2 +
6331 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006332 */
6333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006335 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6336 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006340 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6341 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006342#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006345 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006346
Paul Bakker1ef83d62012-04-11 12:09:53 +00006347 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006348
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006349 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6350 mbedtls_md5_update_ret( &md5, session->master, 48 );
6351 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6352 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006353
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006354 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6355 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6356 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6357 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006358
Paul Bakker1ef83d62012-04-11 12:09:53 +00006359 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006361 mbedtls_md5_starts_ret( &md5 );
6362 mbedtls_md5_update_ret( &md5, session->master, 48 );
6363 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6364 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6365 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006366
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006367 mbedtls_sha1_starts_ret( &sha1 );
6368 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6369 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6370 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6371 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006374
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006375 mbedtls_md5_free( &md5 );
6376 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006377
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006378 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6379 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6380 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006387static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006389{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006390 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006391 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006392 mbedtls_md5_context md5;
6393 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006394 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006397 if( !session )
6398 session = ssl->session;
6399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006401
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006402 mbedtls_md5_init( &md5 );
6403 mbedtls_sha1_init( &sha1 );
6404
6405 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6406 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006407
Paul Bakker1ef83d62012-04-11 12:09:53 +00006408 /*
6409 * TLSv1:
6410 * hash = PRF( master, finished_label,
6411 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6412 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006415 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6416 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006417#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006420 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6421 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006422#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006425 ? "client finished"
6426 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006427
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006428 mbedtls_md5_finish_ret( &md5, padbuf );
6429 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006430
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006431 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006432 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006435
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006436 mbedtls_md5_free( &md5 );
6437 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006438
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006439 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6446#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006447static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006449{
6450 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006451 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006452 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006453 unsigned char padbuf[32];
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006456 if( !session )
6457 session = ssl->session;
6458
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006459 mbedtls_sha256_init( &sha256 );
6460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006462
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006463 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006464
6465 /*
6466 * TLSv1.2:
6467 * hash = PRF( master, finished_label,
6468 * Hash( handshake ) )[0.11]
6469 */
6470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471#if !defined(MBEDTLS_SHA256_ALT)
6472 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006473 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006474#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006477 ? "client finished"
6478 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006479
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006480 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006481
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006482 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006483 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006486
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006487 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006489 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006496static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006498{
6499 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006500 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006501 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006502 unsigned char padbuf[48];
6503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006505 if( !session )
6506 session = ssl->session;
6507
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006508 mbedtls_sha512_init( &sha512 );
6509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006512 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006513
6514 /*
6515 * TLSv1.2:
6516 * hash = PRF( master, finished_label,
6517 * Hash( handshake ) )[0.11]
6518 */
6519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006521 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6522 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006523#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006526 ? "client finished"
6527 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00006528
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006529 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006530
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006531 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006532 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006535
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006536 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006537
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006538 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542#endif /* MBEDTLS_SHA512_C */
6543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006546{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006548
6549 /*
6550 * Free our handshake params
6551 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006552 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006554 ssl->handshake = NULL;
6555
6556 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006557 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006558 */
6559 if( ssl->transform )
6560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561 mbedtls_ssl_transform_free( ssl->transform );
6562 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006563 }
6564 ssl->transform = ssl->transform_negotiate;
6565 ssl->transform_negotiate = NULL;
6566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006568}
6569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006571{
6572 int resume = ssl->handshake->resume;
6573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576#if defined(MBEDTLS_SSL_RENEGOTIATION)
6577 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006580 ssl->renego_records_seen = 0;
6581 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006582#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006583
6584 /*
6585 * Free the previous session and switch in the current one
6586 */
Paul Bakker0a597072012-09-25 21:55:46 +00006587 if( ssl->session )
6588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006590 /* RFC 7366 3.1: keep the EtM state */
6591 ssl->session_negotiate->encrypt_then_mac =
6592 ssl->session->encrypt_then_mac;
6593#endif
6594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595 mbedtls_ssl_session_free( ssl->session );
6596 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006597 }
6598 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006599 ssl->session_negotiate = NULL;
6600
Paul Bakker0a597072012-09-25 21:55:46 +00006601 /*
6602 * Add cache entry
6603 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006604 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006605 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006606 resume == 0 )
6607 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006608 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006610 }
Paul Bakker0a597072012-09-25 21:55:46 +00006611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006614 ssl->handshake->flight != NULL )
6615 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006616 /* Cancel handshake timer */
6617 ssl_set_timer( ssl, 0 );
6618
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006619 /* Keep last flight around in case we need to resend it:
6620 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006622 }
6623 else
6624#endif
6625 ssl_handshake_wrapup_free_hs_transform( ssl );
6626
Paul Bakker48916f92012-09-16 19:57:18 +00006627 ssl->state++;
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006630}
6631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006633{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006634 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006637
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006638 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006639
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006640 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006641
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006642 /*
6643 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6644 * may define some other value. Currently (early 2016), no defined
6645 * ciphersuite does this (and this is unlikely to change as activity has
6646 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006651 ssl->verify_data_len = hash_len;
6652 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006653#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006654
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6657 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006658
6659 /*
6660 * In case of session resuming, invert the client and server
6661 * ChangeCipherSpec messages order.
6662 */
Paul Bakker0a597072012-09-25 21:55:46 +00006663 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006666 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006668#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006670 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 }
6674 else
6675 ssl->state++;
6676
Paul Bakker48916f92012-09-16 19:57:18 +00006677 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006678 * Switch to our negotiated transform and session parameters for outbound
6679 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006685 {
6686 unsigned char i;
6687
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006688 /* Remember current epoch settings for resending */
6689 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006690 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006691
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006692 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006693 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006694
6695 /* Increment epoch */
6696 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006697 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006698 break;
6699
6700 /* The loop goes to its end iff the counter is wrapping */
6701 if( i == 0 )
6702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6704 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006705 }
6706 }
6707 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006709 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006710
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006711 ssl->transform_out = ssl->transform_negotiate;
6712 ssl->session_out = ssl->session_negotiate;
6713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6715 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6720 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006721 }
6722 }
6723#endif
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006726 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006728#endif
6729
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006730 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006731 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006733 return( ret );
6734 }
6735
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006736#if defined(MBEDTLS_SSL_PROTO_DTLS)
6737 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6738 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6739 {
6740 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6741 return( ret );
6742 }
6743#endif
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006746
6747 return( 0 );
6748}
6749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006751#define SSL_MAX_HASH_LEN 36
6752#else
6753#define SSL_MAX_HASH_LEN 12
6754#endif
6755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006757{
Paul Bakker23986e52011-04-24 08:57:21 +00006758 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006759 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006760 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006763
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006764 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006765
Hanno Becker327c93b2018-08-15 13:56:18 +01006766 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006769 return( ret );
6770 }
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006775 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6776 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 }
6779
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006780 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#if defined(MBEDTLS_SSL_PROTO_SSL3)
6782 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006783 hash_len = 36;
6784 else
6785#endif
6786 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6789 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006795 }
6796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006798 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006801 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6802 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 }
6805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006807 ssl->verify_data_len = hash_len;
6808 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006809#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006810
Paul Bakker0a597072012-09-25 21:55:46 +00006811 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006814 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006816#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006818 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006820#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006821 }
6822 else
6823 ssl->state++;
6824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006826 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006828#endif
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006831
6832 return( 0 );
6833}
6834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6840 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6841 mbedtls_md5_init( &handshake->fin_md5 );
6842 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006843 mbedtls_md5_starts_ret( &handshake->fin_md5 );
6844 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6847#if defined(MBEDTLS_SHA256_C)
6848 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006849 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#if defined(MBEDTLS_SHA512_C)
6852 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006853 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006854#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006856
6857 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006858
6859#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
6860 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
6861 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6862#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864#if defined(MBEDTLS_DHM_C)
6865 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867#if defined(MBEDTLS_ECDH_C)
6868 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006869#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006870#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006871 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006872#if defined(MBEDTLS_SSL_CLI_C)
6873 handshake->ecjpake_cache = NULL;
6874 handshake->ecjpake_cache_len = 0;
6875#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006876#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006877
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006878#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02006879 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006880#endif
6881
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006882#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6883 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6884#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006885}
6886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6892 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894 mbedtls_md_init( &transform->md_ctx_enc );
6895 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006896}
6897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006901}
6902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006904{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006905 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006906 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006908 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006910 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006911 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006912
6913 /*
6914 * Either the pointers are now NULL or cleared properly and can be freed.
6915 * Now allocate missing structures.
6916 */
6917 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006918 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006919 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006920 }
Paul Bakker48916f92012-09-16 19:57:18 +00006921
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006922 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006923 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006924 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006925 }
Paul Bakker48916f92012-09-16 19:57:18 +00006926
Paul Bakker82788fb2014-10-20 13:59:19 +02006927 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006928 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006929 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006930 }
Paul Bakker48916f92012-09-16 19:57:18 +00006931
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006932 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006933 if( ssl->handshake == NULL ||
6934 ssl->transform_negotiate == NULL ||
6935 ssl->session_negotiate == NULL )
6936 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 mbedtls_free( ssl->handshake );
6940 mbedtls_free( ssl->transform_negotiate );
6941 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006942
6943 ssl->handshake = NULL;
6944 ssl->transform_negotiate = NULL;
6945 ssl->session_negotiate = NULL;
6946
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006947 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006948 }
6949
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006950 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006952 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006953 ssl_handshake_params_init( ssl->handshake );
6954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6957 {
6958 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006959
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006960 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6961 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6962 else
6963 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006964
6965 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006966 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006967#endif
6968
Paul Bakker48916f92012-09-16 19:57:18 +00006969 return( 0 );
6970}
6971
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006972#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006973/* Dummy cookie callbacks for defaults */
6974static int ssl_cookie_write_dummy( void *ctx,
6975 unsigned char **p, unsigned char *end,
6976 const unsigned char *cli_id, size_t cli_id_len )
6977{
6978 ((void) ctx);
6979 ((void) p);
6980 ((void) end);
6981 ((void) cli_id);
6982 ((void) cli_id_len);
6983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006985}
6986
6987static int ssl_cookie_check_dummy( void *ctx,
6988 const unsigned char *cookie, size_t cookie_len,
6989 const unsigned char *cli_id, size_t cli_id_len )
6990{
6991 ((void) ctx);
6992 ((void) cookie);
6993 ((void) cookie_len);
6994 ((void) cli_id);
6995 ((void) cli_id_len);
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006998}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006999#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007000
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007001/* Once ssl->out_hdr as the address of the beginning of the
7002 * next outgoing record is set, deduce the other pointers.
7003 *
7004 * Note: For TLS, we save the implicit record sequence number
7005 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7006 * and the caller has to make sure there's space for this.
7007 */
7008
7009static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7010 mbedtls_ssl_transform *transform )
7011{
7012#if defined(MBEDTLS_SSL_PROTO_DTLS)
7013 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7014 {
7015 ssl->out_ctr = ssl->out_hdr + 3;
7016 ssl->out_len = ssl->out_hdr + 11;
7017 ssl->out_iv = ssl->out_hdr + 13;
7018 }
7019 else
7020#endif
7021 {
7022 ssl->out_ctr = ssl->out_hdr - 8;
7023 ssl->out_len = ssl->out_hdr + 3;
7024 ssl->out_iv = ssl->out_hdr + 5;
7025 }
7026
7027 /* Adjust out_msg to make space for explicit IV, if used. */
7028 if( transform != NULL &&
7029 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7030 {
7031 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7032 }
7033 else
7034 ssl->out_msg = ssl->out_iv;
7035}
7036
7037/* Once ssl->in_hdr as the address of the beginning of the
7038 * next incoming record is set, deduce the other pointers.
7039 *
7040 * Note: For TLS, we save the implicit record sequence number
7041 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7042 * and the caller has to make sure there's space for this.
7043 */
7044
7045static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7046 mbedtls_ssl_transform *transform )
7047{
7048#if defined(MBEDTLS_SSL_PROTO_DTLS)
7049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7050 {
7051 ssl->in_ctr = ssl->in_hdr + 3;
7052 ssl->in_len = ssl->in_hdr + 11;
7053 ssl->in_iv = ssl->in_hdr + 13;
7054 }
7055 else
7056#endif
7057 {
7058 ssl->in_ctr = ssl->in_hdr - 8;
7059 ssl->in_len = ssl->in_hdr + 3;
7060 ssl->in_iv = ssl->in_hdr + 5;
7061 }
7062
7063 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7064 if( transform != NULL &&
7065 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7066 {
7067 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7068 }
7069 else
7070 ssl->in_msg = ssl->in_iv;
7071}
7072
Paul Bakker5121ce52009-01-03 21:22:43 +00007073/*
7074 * Initialize an SSL context
7075 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007076void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7077{
7078 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7079}
7080
7081/*
7082 * Setup an SSL context
7083 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007084
7085static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7086{
7087 /* Set the incoming and outgoing record pointers. */
7088#if defined(MBEDTLS_SSL_PROTO_DTLS)
7089 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7090 {
7091 ssl->out_hdr = ssl->out_buf;
7092 ssl->in_hdr = ssl->in_buf;
7093 }
7094 else
7095#endif /* MBEDTLS_SSL_PROTO_DTLS */
7096 {
7097 ssl->out_hdr = ssl->out_buf + 8;
7098 ssl->in_hdr = ssl->in_buf + 8;
7099 }
7100
7101 /* Derive other internal pointers. */
7102 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7103 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7104}
7105
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007106int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007107 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108{
Paul Bakker48916f92012-09-16 19:57:18 +00007109 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007110
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007111 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007112
7113 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007114 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007115 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007116
7117 /* Set to NULL in case of an error condition */
7118 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007119
Angus Grattond8213d02016-05-25 20:56:48 +10007120 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7121 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007122 {
Angus Grattond8213d02016-05-25 20:56:48 +10007123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007124 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007125 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007126 }
7127
7128 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7129 if( ssl->out_buf == NULL )
7130 {
7131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007132 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007133 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007134 }
7135
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007136 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007137
Paul Bakker48916f92012-09-16 19:57:18 +00007138 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007139 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007140
7141 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007142
7143error:
7144 mbedtls_free( ssl->in_buf );
7145 mbedtls_free( ssl->out_buf );
7146
7147 ssl->conf = NULL;
7148
7149 ssl->in_buf = NULL;
7150 ssl->out_buf = NULL;
7151
7152 ssl->in_hdr = NULL;
7153 ssl->in_ctr = NULL;
7154 ssl->in_len = NULL;
7155 ssl->in_iv = NULL;
7156 ssl->in_msg = NULL;
7157
7158 ssl->out_hdr = NULL;
7159 ssl->out_ctr = NULL;
7160 ssl->out_len = NULL;
7161 ssl->out_iv = NULL;
7162 ssl->out_msg = NULL;
7163
k-stachowiak9f7798e2018-07-31 16:52:32 +02007164 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007165}
7166
7167/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007168 * Reset an initialized and used SSL context for re-use while retaining
7169 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007170 *
7171 * If partial is non-zero, keep data in the input buffer and client ID.
7172 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007173 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007174static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007175{
Paul Bakker48916f92012-09-16 19:57:18 +00007176 int ret;
7177
Hanno Becker7e772132018-08-10 12:38:21 +01007178#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7179 !defined(MBEDTLS_SSL_SRV_C)
7180 ((void) partial);
7181#endif
7182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007184
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007185 /* Cancel any possibly running timer */
7186 ssl_set_timer( ssl, 0 );
7187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SSL_RENEGOTIATION)
7189 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007190 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007191
7192 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7194 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007197
Paul Bakker7eb013f2011-10-06 12:37:39 +00007198 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007199 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007200
7201 ssl->in_msgtype = 0;
7202 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007204 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007205 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007208 ssl_dtls_replay_reset( ssl );
7209#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007210
7211 ssl->in_hslen = 0;
7212 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007213
7214 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007215
7216 ssl->out_msgtype = 0;
7217 ssl->out_msglen = 0;
7218 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7220 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007221 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007222#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007223
Hanno Becker19859472018-08-06 09:40:20 +01007224 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7225
Paul Bakker48916f92012-09-16 19:57:18 +00007226 ssl->transform_in = NULL;
7227 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007228
Hanno Becker78640902018-08-13 16:35:15 +01007229 ssl->session_in = NULL;
7230 ssl->session_out = NULL;
7231
Angus Grattond8213d02016-05-25 20:56:48 +10007232 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007233
7234#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007235 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007236#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7237 {
7238 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007239 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007240 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7243 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7246 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7249 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007250 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007251 }
7252#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007253
Paul Bakker48916f92012-09-16 19:57:18 +00007254 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 mbedtls_ssl_transform_free( ssl->transform );
7257 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007258 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007259 }
Paul Bakker48916f92012-09-16 19:57:18 +00007260
Paul Bakkerc0463502013-02-14 11:19:38 +01007261 if( ssl->session )
7262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 mbedtls_ssl_session_free( ssl->session );
7264 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007265 ssl->session = NULL;
7266 }
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007269 ssl->alpn_chosen = NULL;
7270#endif
7271
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007272#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007273#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007274 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007275#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007276 {
7277 mbedtls_free( ssl->cli_id );
7278 ssl->cli_id = NULL;
7279 ssl->cli_id_len = 0;
7280 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007281#endif
7282
Paul Bakker48916f92012-09-16 19:57:18 +00007283 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7284 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007285
7286 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007287}
7288
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007289/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007290 * Reset an initialized and used SSL context for re-use while retaining
7291 * all application-set variables, function pointers and data.
7292 */
7293int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7294{
7295 return( ssl_session_reset_int( ssl, 0 ) );
7296}
7297
7298/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007299 * SSL set accessors
7300 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007301void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007302{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007303 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007304}
7305
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007306void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007307{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007308 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007309}
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007312void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007313{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007314 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007315}
7316#endif
7317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007319void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007321 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007322}
7323#endif
7324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007326
Hanno Becker1841b0a2018-08-24 11:13:57 +01007327void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7328 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007329{
7330 ssl->disable_datagram_packing = !allow_packing;
7331}
7332
7333void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7334 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007335{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007336 conf->hs_timeout_min = min;
7337 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007338}
7339#endif
7340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007341void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007342{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007343 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007344}
7345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007347void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007348 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007349 void *p_vrfy )
7350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007351 conf->f_vrfy = f_vrfy;
7352 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007355
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007356void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007357 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007358 void *p_rng )
7359{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007360 conf->f_rng = f_rng;
7361 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007362}
7363
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007364void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007365 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007366 void *p_dbg )
7367{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007368 conf->f_dbg = f_dbg;
7369 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007370}
7371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007373 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007374 mbedtls_ssl_send_t *f_send,
7375 mbedtls_ssl_recv_t *f_recv,
7376 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007377{
7378 ssl->p_bio = p_bio;
7379 ssl->f_send = f_send;
7380 ssl->f_recv = f_recv;
7381 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007382}
7383
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007384#if defined(MBEDTLS_SSL_PROTO_DTLS)
7385void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7386{
7387 ssl->mtu = mtu;
7388}
7389#endif
7390
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007391void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007392{
7393 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007394}
7395
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007396void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7397 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007398 mbedtls_ssl_set_timer_t *f_set_timer,
7399 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007400{
7401 ssl->p_timer = p_timer;
7402 ssl->f_set_timer = f_set_timer;
7403 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007404
7405 /* Make sure we start with no timer running */
7406 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007407}
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007410void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007411 void *p_cache,
7412 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7413 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007414{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007415 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007416 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007417 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421#if defined(MBEDTLS_SSL_CLI_C)
7422int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007423{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007424 int ret;
7425
7426 if( ssl == NULL ||
7427 session == NULL ||
7428 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007429 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007432 }
7433
7434 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7435 return( ret );
7436
Paul Bakker0a597072012-09-25 21:55:46 +00007437 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007438
7439 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007442
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007443void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007444 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007446 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7447 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7448 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7449 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007450}
7451
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007452void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007453 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007454 int major, int minor )
7455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007457 return;
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007460 return;
7461
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007462 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007463}
7464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007466void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007467 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007468{
7469 conf->cert_profile = profile;
7470}
7471
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007472/* Append a new keycert entry to a (possibly empty) list */
7473static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7474 mbedtls_x509_crt *cert,
7475 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007476{
niisato8ee24222018-06-25 19:05:48 +09007477 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007478
niisato8ee24222018-06-25 19:05:48 +09007479 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7480 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007481 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007482
niisato8ee24222018-06-25 19:05:48 +09007483 new_cert->cert = cert;
7484 new_cert->key = key;
7485 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007486
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007487 /* Update head is the list was null, else add to the end */
7488 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007489 {
niisato8ee24222018-06-25 19:05:48 +09007490 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007491 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007492 else
7493 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007494 mbedtls_ssl_key_cert *cur = *head;
7495 while( cur->next != NULL )
7496 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007497 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007498 }
7499
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007500 return( 0 );
7501}
7502
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007503int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007504 mbedtls_x509_crt *own_cert,
7505 mbedtls_pk_context *pk_key )
7506{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007507 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007508}
7509
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007510void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007511 mbedtls_x509_crt *ca_chain,
7512 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007513{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007514 conf->ca_chain = ca_chain;
7515 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007518
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007519#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7520int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7521 mbedtls_x509_crt *own_cert,
7522 mbedtls_pk_context *pk_key )
7523{
7524 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7525 own_cert, pk_key ) );
7526}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007527
7528void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7529 mbedtls_x509_crt *ca_chain,
7530 mbedtls_x509_crl *ca_crl )
7531{
7532 ssl->handshake->sni_ca_chain = ca_chain;
7533 ssl->handshake->sni_ca_crl = ca_crl;
7534}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007535
7536void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7537 int authmode )
7538{
7539 ssl->handshake->sni_authmode = authmode;
7540}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007541#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7542
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007543#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007544/*
7545 * Set EC J-PAKE password for current handshake
7546 */
7547int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7548 const unsigned char *pw,
7549 size_t pw_len )
7550{
7551 mbedtls_ecjpake_role role;
7552
Janos Follath8eb64132016-06-03 15:40:57 +01007553 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7555
7556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7557 role = MBEDTLS_ECJPAKE_SERVER;
7558 else
7559 role = MBEDTLS_ECJPAKE_CLIENT;
7560
7561 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7562 role,
7563 MBEDTLS_MD_SHA256,
7564 MBEDTLS_ECP_DP_SECP256R1,
7565 pw, pw_len ) );
7566}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007567#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007570
7571static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7572{
7573 /* Remove reference to existing PSK, if any. */
7574#if defined(MBEDTLS_USE_PSA_CRYPTO)
7575 if( conf->psk_opaque != 0 )
7576 {
7577 /* The maintenance of the PSK key slot is the
7578 * user's responsibility. */
7579 conf->psk_opaque = 0;
7580 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007581 /* This and the following branch should never
7582 * be taken simultaenously as we maintain the
7583 * invariant that raw and opaque PSKs are never
7584 * configured simultaneously. As a safeguard,
7585 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007586#endif /* MBEDTLS_USE_PSA_CRYPTO */
7587 if( conf->psk != NULL )
7588 {
7589 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7590
7591 mbedtls_free( conf->psk );
7592 conf->psk = NULL;
7593 conf->psk_len = 0;
7594 }
7595
7596 /* Remove reference to PSK identity, if any. */
7597 if( conf->psk_identity != NULL )
7598 {
7599 mbedtls_free( conf->psk_identity );
7600 conf->psk_identity = NULL;
7601 conf->psk_identity_len = 0;
7602 }
7603}
7604
Hanno Becker7390c712018-11-15 13:33:04 +00007605/* This function assumes that PSK identity in the SSL config is unset.
7606 * It checks that the provided identity is well-formed and attempts
7607 * to make a copy of it in the SSL config.
7608 * On failure, the PSK identity in the config remains unset. */
7609static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7610 unsigned char const *psk_identity,
7611 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007612{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007613 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007614 if( psk_identity == NULL ||
7615 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007616 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007617 {
7618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7619 }
7620
Hanno Becker7390c712018-11-15 13:33:04 +00007621 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7622 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007623 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007624
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007625 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007626 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007627
7628 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007629}
7630
Hanno Becker7390c712018-11-15 13:33:04 +00007631int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7632 const unsigned char *psk, size_t psk_len,
7633 const unsigned char *psk_identity, size_t psk_identity_len )
7634{
7635 int ret;
7636 /* Remove opaque/raw PSK + PSK Identity */
7637 ssl_conf_remove_psk( conf );
7638
7639 /* Check and set raw PSK */
7640 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7642 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7643 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7644 conf->psk_len = psk_len;
7645 memcpy( conf->psk, psk, conf->psk_len );
7646
7647 /* Check and set PSK Identity */
7648 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7649 if( ret != 0 )
7650 ssl_conf_remove_psk( conf );
7651
7652 return( ret );
7653}
7654
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007655static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7656{
7657#if defined(MBEDTLS_USE_PSA_CRYPTO)
7658 if( ssl->handshake->psk_opaque != 0 )
7659 {
7660 ssl->handshake->psk_opaque = 0;
7661 }
7662 else
7663#endif /* MBEDTLS_USE_PSA_CRYPTO */
7664 if( ssl->handshake->psk != NULL )
7665 {
7666 mbedtls_platform_zeroize( ssl->handshake->psk,
7667 ssl->handshake->psk_len );
7668 mbedtls_free( ssl->handshake->psk );
7669 ssl->handshake->psk_len = 0;
7670 }
7671}
7672
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007673int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7674 const unsigned char *psk, size_t psk_len )
7675{
7676 if( psk == NULL || ssl->handshake == NULL )
7677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7678
7679 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7681
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007682 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007683
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007684 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007685 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007686
7687 ssl->handshake->psk_len = psk_len;
7688 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7689
7690 return( 0 );
7691}
7692
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007693#if defined(MBEDTLS_USE_PSA_CRYPTO)
7694int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007695 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007696 const unsigned char *psk_identity,
7697 size_t psk_identity_len )
7698{
Hanno Becker7390c712018-11-15 13:33:04 +00007699 int ret;
7700 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007701 ssl_conf_remove_psk( conf );
7702
Hanno Becker7390c712018-11-15 13:33:04 +00007703 /* Check and set opaque PSK */
7704 if( psk_slot == 0 )
7705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007706 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007707
7708 /* Check and set PSK Identity */
7709 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7710 psk_identity_len );
7711 if( ret != 0 )
7712 ssl_conf_remove_psk( conf );
7713
7714 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007715}
7716
7717int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007718 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007719{
7720 if( psk_slot == 0 || ssl->handshake == NULL )
7721 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7722
7723 ssl_remove_psk( ssl );
7724 ssl->handshake->psk_opaque = psk_slot;
7725 return( 0 );
7726}
7727#endif /* MBEDTLS_USE_PSA_CRYPTO */
7728
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007729void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007731 size_t),
7732 void *p_psk )
7733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007734 conf->f_psk = f_psk;
7735 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007738
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007739#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007740
7741#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007742int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007743{
7744 int ret;
7745
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007746 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7747 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7748 {
7749 mbedtls_mpi_free( &conf->dhm_P );
7750 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007751 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007752 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007753
7754 return( 0 );
7755}
Hanno Becker470a8c42017-10-04 15:28:46 +01007756#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007757
Hanno Beckera90658f2017-10-04 15:29:08 +01007758int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7759 const unsigned char *dhm_P, size_t P_len,
7760 const unsigned char *dhm_G, size_t G_len )
7761{
7762 int ret;
7763
7764 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7765 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7766 {
7767 mbedtls_mpi_free( &conf->dhm_P );
7768 mbedtls_mpi_free( &conf->dhm_G );
7769 return( ret );
7770 }
7771
7772 return( 0 );
7773}
Paul Bakker5121ce52009-01-03 21:22:43 +00007774
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007775int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007776{
7777 int ret;
7778
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007779 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7780 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7781 {
7782 mbedtls_mpi_free( &conf->dhm_P );
7783 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007784 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007785 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007786
7787 return( 0 );
7788}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007789#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007790
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007791#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7792/*
7793 * Set the minimum length for Diffie-Hellman parameters
7794 */
7795void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7796 unsigned int bitlen )
7797{
7798 conf->dhm_min_bitlen = bitlen;
7799}
7800#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7801
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007802#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007803/*
7804 * Set allowed/preferred hashes for handshake signatures
7805 */
7806void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7807 const int *hashes )
7808{
7809 conf->sig_hashes = hashes;
7810}
Hanno Becker947194e2017-04-07 13:25:49 +01007811#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007812
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007813#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007814/*
7815 * Set the allowed elliptic curves
7816 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007817void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007818 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007820 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007821}
Hanno Becker947194e2017-04-07 13:25:49 +01007822#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007823
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007824#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00007826{
Hanno Becker947194e2017-04-07 13:25:49 +01007827 /* Initialize to suppress unnecessary compiler warning */
7828 size_t hostname_len = 0;
7829
7830 /* Check if new hostname is valid before
7831 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01007832 if( hostname != NULL )
7833 {
7834 hostname_len = strlen( hostname );
7835
7836 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
7837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7838 }
7839
7840 /* Now it's clear that we will overwrite the old hostname,
7841 * so we can free it safely */
7842
7843 if( ssl->hostname != NULL )
7844 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007845 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01007846 mbedtls_free( ssl->hostname );
7847 }
7848
7849 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007850
Paul Bakker5121ce52009-01-03 21:22:43 +00007851 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01007852 {
7853 ssl->hostname = NULL;
7854 }
7855 else
7856 {
7857 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01007858 if( ssl->hostname == NULL )
7859 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007860
Hanno Becker947194e2017-04-07 13:25:49 +01007861 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02007862
Hanno Becker947194e2017-04-07 13:25:49 +01007863 ssl->hostname[hostname_len] = '\0';
7864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007865
7866 return( 0 );
7867}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01007868#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007869
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007870#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007871void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00007873 const unsigned char *, size_t),
7874 void *p_sni )
7875{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007876 conf->f_sni = f_sni;
7877 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00007878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00007880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007882int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007883{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007884 size_t cur_len, tot_len;
7885 const char **p;
7886
7887 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08007888 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
7889 * MUST NOT be truncated."
7890 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007891 */
7892 tot_len = 0;
7893 for( p = protos; *p != NULL; p++ )
7894 {
7895 cur_len = strlen( *p );
7896 tot_len += cur_len;
7897
7898 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007900 }
7901
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007902 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02007903
7904 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007905}
7906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007908{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007909 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007912
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007913void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00007914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007915 conf->max_major_ver = major;
7916 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00007917}
7918
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007919void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00007920{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007921 conf->min_major_ver = major;
7922 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00007923}
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007926void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007927{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01007928 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02007929}
7930#endif
7931
Janos Follath088ce432017-04-10 12:42:31 +01007932#if defined(MBEDTLS_SSL_SRV_C)
7933void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
7934 char cert_req_ca_list )
7935{
7936 conf->cert_req_ca_list = cert_req_ca_list;
7937}
7938#endif
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007941void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007942{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007943 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01007944}
7945#endif
7946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007948void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007949{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007950 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02007951}
7952#endif
7953
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007954#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007955void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007956{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007957 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007958}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007959#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01007960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007962int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10007965 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007968 }
7969
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01007970 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007971
7972 return( 0 );
7973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02007975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007977void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007979 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02007982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007984void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007985{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007986 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007987}
7988#endif
7989
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007990void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00007991{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007992 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00007993}
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007996void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007997{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007998 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007999}
8000
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008001void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008002{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008003 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008004}
8005
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008006void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008007 const unsigned char period[8] )
8008{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008009 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008014#if defined(MBEDTLS_SSL_CLI_C)
8015void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008016{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008017 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008018}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008019#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008020
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008021#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008022void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8023 mbedtls_ssl_ticket_write_t *f_ticket_write,
8024 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8025 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008026{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008027 conf->f_ticket_write = f_ticket_write;
8028 conf->f_ticket_parse = f_ticket_parse;
8029 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008030}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008033
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008034#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8035void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8036 mbedtls_ssl_export_keys_t *f_export_keys,
8037 void *p_export_keys )
8038{
8039 conf->f_export_keys = f_export_keys;
8040 conf->p_export_keys = p_export_keys;
8041}
8042#endif
8043
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008044#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008045void mbedtls_ssl_conf_async_private_cb(
8046 mbedtls_ssl_config *conf,
8047 mbedtls_ssl_async_sign_t *f_async_sign,
8048 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8049 mbedtls_ssl_async_resume_t *f_async_resume,
8050 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008051 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008052{
8053 conf->f_async_sign_start = f_async_sign;
8054 conf->f_async_decrypt_start = f_async_decrypt;
8055 conf->f_async_resume = f_async_resume;
8056 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008057 conf->p_async_config_data = async_config_data;
8058}
8059
Gilles Peskine8f97af72018-04-26 11:46:10 +02008060void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8061{
8062 return( conf->p_async_config_data );
8063}
8064
Gilles Peskine1febfef2018-04-30 11:54:39 +02008065void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008066{
8067 if( ssl->handshake == NULL )
8068 return( NULL );
8069 else
8070 return( ssl->handshake->user_async_ctx );
8071}
8072
Gilles Peskine1febfef2018-04-30 11:54:39 +02008073void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008074 void *ctx )
8075{
8076 if( ssl->handshake != NULL )
8077 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008078}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008079#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008080
Paul Bakker5121ce52009-01-03 21:22:43 +00008081/*
8082 * SSL get accessors
8083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008085{
8086 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8087}
8088
Hanno Becker8b170a02017-10-10 11:51:19 +01008089int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8090{
8091 /*
8092 * Case A: We're currently holding back
8093 * a message for further processing.
8094 */
8095
8096 if( ssl->keep_current_message == 1 )
8097 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008099 return( 1 );
8100 }
8101
8102 /*
8103 * Case B: Further records are pending in the current datagram.
8104 */
8105
8106#if defined(MBEDTLS_SSL_PROTO_DTLS)
8107 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8108 ssl->in_left > ssl->next_record_offset )
8109 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008111 return( 1 );
8112 }
8113#endif /* MBEDTLS_SSL_PROTO_DTLS */
8114
8115 /*
8116 * Case C: A handshake message is being processed.
8117 */
8118
Hanno Becker8b170a02017-10-10 11:51:19 +01008119 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8120 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008122 return( 1 );
8123 }
8124
8125 /*
8126 * Case D: An application data message is being processed
8127 */
8128 if( ssl->in_offt != NULL )
8129 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008131 return( 1 );
8132 }
8133
8134 /*
8135 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008136 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008137 * we implement support for multiple alerts in single records.
8138 */
8139
8140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8141 return( 0 );
8142}
8143
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008144uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008145{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008146 if( ssl->session != NULL )
8147 return( ssl->session->verify_result );
8148
8149 if( ssl->session_negotiate != NULL )
8150 return( ssl->session_negotiate->verify_result );
8151
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008152 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008153}
8154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008156{
Paul Bakker926c8e42013-03-06 10:23:34 +01008157 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008158 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008161}
8162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008166 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008167 {
8168 switch( ssl->minor_ver )
8169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008171 return( "DTLSv1.0" );
8172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008174 return( "DTLSv1.2" );
8175
8176 default:
8177 return( "unknown (DTLS)" );
8178 }
8179 }
8180#endif
8181
Paul Bakker43ca69c2011-01-15 17:35:19 +00008182 switch( ssl->minor_ver )
8183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008185 return( "SSLv3.0" );
8186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008188 return( "TLSv1.0" );
8189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008191 return( "TLSv1.1" );
8192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008194 return( "TLSv1.2" );
8195
Paul Bakker43ca69c2011-01-15 17:35:19 +00008196 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008197 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008198 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008199}
8200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008202{
Hanno Becker3136ede2018-08-17 15:28:19 +01008203 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008205 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008206
Hanno Becker78640902018-08-13 16:35:15 +01008207 if( transform == NULL )
8208 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#if defined(MBEDTLS_ZLIB_SUPPORT)
8211 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8212 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008213#endif
8214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 case MBEDTLS_MODE_GCM:
8218 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008219 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008221 transform_expansion = transform->minlen;
8222 break;
8223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008224 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008225
8226 block_size = mbedtls_cipher_get_block_size(
8227 &transform->cipher_ctx_enc );
8228
Hanno Becker3136ede2018-08-17 15:28:19 +01008229 /* Expansion due to the addition of the MAC. */
8230 transform_expansion += transform->maclen;
8231
8232 /* Expansion due to the addition of CBC padding;
8233 * Theoretically up to 256 bytes, but we never use
8234 * more than the block size of the underlying cipher. */
8235 transform_expansion += block_size;
8236
8237 /* For TLS 1.1 or higher, an explicit IV is added
8238 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008239#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8240 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008241 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008242#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008243
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008244 break;
8245
8246 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008249 }
8250
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008251 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008252}
8253
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8255size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8256{
8257 size_t max_len;
8258
8259 /*
8260 * Assume mfl_code is correct since it was checked when set
8261 */
Angus Grattond8213d02016-05-25 20:56:48 +10008262 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008263
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008264 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008265 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008266 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008267 {
Angus Grattond8213d02016-05-25 20:56:48 +10008268 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008269 }
8270
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008271 /* During a handshake, use the value being negotiated */
8272 if( ssl->session_negotiate != NULL &&
8273 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8274 {
8275 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8276 }
8277
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008278 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008279}
8280#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8281
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008282#if defined(MBEDTLS_SSL_PROTO_DTLS)
8283static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8284{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008285 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8286 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8287 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8288 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8289 return ( 0 );
8290
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008291 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8292 return( ssl->mtu );
8293
8294 if( ssl->mtu == 0 )
8295 return( ssl->handshake->mtu );
8296
8297 return( ssl->mtu < ssl->handshake->mtu ?
8298 ssl->mtu : ssl->handshake->mtu );
8299}
8300#endif /* MBEDTLS_SSL_PROTO_DTLS */
8301
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008302int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8303{
8304 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8305
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008306#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8307 !defined(MBEDTLS_SSL_PROTO_DTLS)
8308 (void) ssl;
8309#endif
8310
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008311#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8312 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8313
8314 if( max_len > mfl )
8315 max_len = mfl;
8316#endif
8317
8318#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008319 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008320 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008321 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008322 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8323 const size_t overhead = (size_t) ret;
8324
8325 if( ret < 0 )
8326 return( ret );
8327
8328 if( mtu <= overhead )
8329 {
8330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8331 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8332 }
8333
8334 if( max_len > mtu - overhead )
8335 max_len = mtu - overhead;
8336 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008337#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008338
Hanno Becker0defedb2018-08-10 12:35:02 +01008339#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8340 !defined(MBEDTLS_SSL_PROTO_DTLS)
8341 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008342#endif
8343
8344 return( (int) max_len );
8345}
8346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_X509_CRT_PARSE_C)
8348const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008349{
8350 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008351 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008352
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008353 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#if defined(MBEDTLS_SSL_CLI_C)
8358int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008359{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008360 if( ssl == NULL ||
8361 dst == NULL ||
8362 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008363 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008366 }
8367
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008368 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008371
Paul Bakker5121ce52009-01-03 21:22:43 +00008372/*
Paul Bakker1961b702013-01-25 14:49:24 +01008373 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008378
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008379 if( ssl == NULL || ssl->conf == NULL )
8380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008383 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008387 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008389#endif
8390
Paul Bakker1961b702013-01-25 14:49:24 +01008391 return( ret );
8392}
8393
8394/*
8395 * Perform the SSL handshake
8396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008398{
8399 int ret = 0;
8400
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008401 if( ssl == NULL || ssl->conf == NULL )
8402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008409
8410 if( ret != 0 )
8411 break;
8412 }
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008415
8416 return( ret );
8417}
8418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_SSL_RENEGOTIATION)
8420#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008421/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008422 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008425{
8426 int ret;
8427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008429
8430 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8432 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008433
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008434 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008435 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008437 return( ret );
8438 }
8439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008441
8442 return( 0 );
8443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008445
8446/*
8447 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448 * - any side: calling mbedtls_ssl_renegotiate(),
8449 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8450 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008451 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008452 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008456{
8457 int ret;
8458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008460
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008461 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8462 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008463
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008464 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8465 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008469 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008470 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008471 ssl->handshake->out_msg_seq = 1;
8472 else
8473 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008474 }
8475#endif
8476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8478 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008483 return( ret );
8484 }
8485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008487
8488 return( 0 );
8489}
8490
8491/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008492 * Renegotiate current connection on client,
8493 * or request renegotiation on server
8494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008497 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008498
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008499 if( ssl == NULL || ssl->conf == NULL )
8500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008503 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008504 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008510
8511 /* Did we already try/start sending HelloRequest? */
8512 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008514
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008515 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008516 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008519#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008520 /*
8521 * On client, either start the renegotiation process or,
8522 * if already in progress, continue the handshake
8523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008528
8529 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008532 return( ret );
8533 }
8534 }
8535 else
8536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008540 return( ret );
8541 }
8542 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008544
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008545 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008546}
8547
8548/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008549 * Check record counters and renegotiate if they're above the limit.
8550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008551static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008552{
Andres AG2196c7f2016-12-15 17:01:16 +00008553 size_t ep_len = ssl_ep_len( ssl );
8554 int in_ctr_cmp;
8555 int out_ctr_cmp;
8556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8558 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008559 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008560 {
8561 return( 0 );
8562 }
8563
Andres AG2196c7f2016-12-15 17:01:16 +00008564 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8565 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008566 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008567 ssl->conf->renego_period + ep_len, 8 - ep_len );
8568
8569 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008570 {
8571 return( 0 );
8572 }
8573
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008578
8579/*
8580 * Receive application data decrypted from the SSL layer
8581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008583{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008584 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008585 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008586
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008587 if( ssl == NULL || ssl->conf == NULL )
8588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008593 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008596 return( ret );
8597
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008598 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008599 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008600 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008601 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008602 return( ret );
8603 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008604 }
8605#endif
8606
Hanno Becker4a810fb2017-05-24 16:27:30 +01008607 /*
8608 * Check if renegotiation is necessary and/or handshake is
8609 * in process. If yes, perform/continue, and fall through
8610 * if an unexpected packet is received while the client
8611 * is waiting for the ServerHello.
8612 *
8613 * (There is no equivalent to the last condition on
8614 * the server-side as it is not treated as within
8615 * a handshake while waiting for the ClientHello
8616 * after a renegotiation request.)
8617 */
8618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008620 ret = ssl_check_ctr_renegotiate( ssl );
8621 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8622 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008625 return( ret );
8626 }
8627#endif
8628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008632 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8633 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008636 return( ret );
8637 }
8638 }
8639
Hanno Beckere41158b2017-10-23 13:30:32 +01008640 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008641 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008642 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008643 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008644 if( ssl->f_get_timer != NULL &&
8645 ssl->f_get_timer( ssl->p_timer ) == -1 )
8646 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008647 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008648 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008649
Hanno Becker327c93b2018-08-15 13:56:18 +01008650 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008651 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008652 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8653 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008654
Hanno Becker4a810fb2017-05-24 16:27:30 +01008655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8656 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008657 }
8658
8659 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008661 {
8662 /*
8663 * OpenSSL sends empty messages to randomize the IV
8664 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008665 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008668 return( 0 );
8669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008671 return( ret );
8672 }
8673 }
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008678
Hanno Becker4a810fb2017-05-24 16:27:30 +01008679 /*
8680 * - For client-side, expect SERVER_HELLO_REQUEST.
8681 * - For server-side, expect CLIENT_HELLO.
8682 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8683 */
8684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008688 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008691
8692 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008695 {
8696 continue;
8697 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008700 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008701#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008702
Hanno Becker4a810fb2017-05-24 16:27:30 +01008703#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008704 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008708
8709 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008712 {
8713 continue;
8714 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008717 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008718#endif /* MBEDTLS_SSL_SRV_C */
8719
Hanno Becker21df7f92017-10-17 11:03:26 +01008720#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008721 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008722 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8723 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8724 ssl->conf->allow_legacy_renegotiation ==
8725 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8726 {
8727 /*
8728 * Accept renegotiation request
8729 */
Paul Bakker48916f92012-09-16 19:57:18 +00008730
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008731 /* DTLS clients need to know renego is server-initiated */
8732#if defined(MBEDTLS_SSL_PROTO_DTLS)
8733 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8734 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8735 {
8736 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8737 }
8738#endif
8739 ret = ssl_start_renegotiation( ssl );
8740 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8741 ret != 0 )
8742 {
8743 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8744 return( ret );
8745 }
8746 }
8747 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008748#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008749 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008750 /*
8751 * Refuse renegotiation
8752 */
8753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#if defined(MBEDTLS_SSL_PROTO_SSL3)
8757 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008758 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008759 /* SSLv3 does not have a "no_renegotiation" warning, so
8760 we send a fatal alert and abort the connection. */
8761 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8762 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8763 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008764 }
8765 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8767#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8768 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8769 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8772 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8773 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008774 {
8775 return( ret );
8776 }
Paul Bakker48916f92012-09-16 19:57:18 +00008777 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008778 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8780 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008784 }
Paul Bakker48916f92012-09-16 19:57:18 +00008785 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008786
Hanno Becker90333da2017-10-10 11:27:13 +01008787 /* At this point, we don't know whether the renegotiation has been
8788 * completed or not. The cases to consider are the following:
8789 * 1) The renegotiation is complete. In this case, no new record
8790 * has been read yet.
8791 * 2) The renegotiation is incomplete because the client received
8792 * an application data record while awaiting the ServerHello.
8793 * 3) The renegotiation is incomplete because the client received
8794 * a non-handshake, non-application data message while awaiting
8795 * the ServerHello.
8796 * In each of these case, looping will be the proper action:
8797 * - For 1), the next iteration will read a new record and check
8798 * if it's application data.
8799 * - For 2), the loop condition isn't satisfied as application data
8800 * is present, hence continue is the same as break
8801 * - For 3), the loop condition is satisfied and read_record
8802 * will re-deliver the message that was held back by the client
8803 * when expecting the ServerHello.
8804 */
8805 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008806 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008807#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008809 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008810 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008811 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008812 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008815 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008817 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008818 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
8823 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01008826 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008827 }
8828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
8832 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008833 }
8834
8835 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008836
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008837 /* We're going to return something now, cancel timer,
8838 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02008840 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008841
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008843 /* If we requested renego but received AppData, resend HelloRequest.
8844 * Do it now, after setting in_offt, to avoid taking this branch
8845 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008847 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008848 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008849 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008850 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008853 return( ret );
8854 }
8855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01008857#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00008858 }
8859
8860 n = ( len < ssl->in_msglen )
8861 ? len : ssl->in_msglen;
8862
8863 memcpy( buf, ssl->in_offt, n );
8864 ssl->in_msglen -= n;
8865
8866 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01008867 {
8868 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00008869 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01008870 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008871 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008872 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01008873 {
Paul Bakker5121ce52009-01-03 21:22:43 +00008874 /* more data available */
8875 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01008876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008879
Paul Bakker23986e52011-04-24 08:57:21 +00008880 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00008881}
8882
8883/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008884 * Send application data to be encrypted by the SSL layer, taking care of max
8885 * fragment length and buffer size.
8886 *
8887 * According to RFC 5246 Section 6.2.1:
8888 *
8889 * Zero-length fragments of Application data MAY be sent as they are
8890 * potentially useful as a traffic analysis countermeasure.
8891 *
8892 * Therefore, it is possible that the input message length is 0 and the
8893 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00008894 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008895static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008896 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008897{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008898 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
8899 const size_t max_len = (size_t) ret;
8900
8901 if( ret < 0 )
8902 {
8903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
8904 return( ret );
8905 }
8906
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008907 if( len > max_len )
8908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008913 "maximum fragment length: %d > %d",
8914 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008916 }
8917 else
8918#endif
8919 len = max_len;
8920 }
Paul Bakker887bd502011-06-08 13:10:54 +00008921
Paul Bakker5121ce52009-01-03 21:22:43 +00008922 if( ssl->out_left != 0 )
8923 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008924 /*
8925 * The user has previously tried to send the data and
8926 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
8927 * written. In this case, we expect the high-level write function
8928 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
8929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008933 return( ret );
8934 }
8935 }
Paul Bakker887bd502011-06-08 13:10:54 +00008936 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00008937 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01008938 /*
8939 * The user is trying to send a message the first time, so we need to
8940 * copy the data into the internal buffers and setup the data structure
8941 * to keep track of partial writes
8942 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008943 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008945 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00008946
Hanno Becker67bc7c32018-08-06 11:33:50 +01008947 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00008948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00008950 return( ret );
8951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008952 }
8953
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02008954 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00008955}
8956
8957/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008958 * Write application data, doing 1/n-1 splitting if necessary.
8959 *
8960 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008961 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01008962 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008965static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008966 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008967{
8968 int ret;
8969
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008970 if( ssl->conf->cbc_record_splitting ==
8971 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008972 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
8974 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
8975 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008976 {
8977 return( ssl_write_real( ssl, buf, len ) );
8978 }
8979
8980 if( ssl->split_done == 0 )
8981 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008982 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008983 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008984 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008985 }
8986
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01008987 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
8988 return( ret );
8989 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008990
8991 return( ret + 1 );
8992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008994
8995/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008996 * Write application data (public-facing wrapper)
8997 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008998int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008999{
9000 int ret;
9001
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009003
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009004 if( ssl == NULL || ssl->conf == NULL )
9005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9006
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009007#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009008 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9009 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009010 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009011 return( ret );
9012 }
9013#endif
9014
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009015 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009016 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009017 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009018 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009020 return( ret );
9021 }
9022 }
9023
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009024#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009025 ret = ssl_write_split( ssl, buf, len );
9026#else
9027 ret = ssl_write_real( ssl, buf, len );
9028#endif
9029
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009031
9032 return( ret );
9033}
9034
9035/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009036 * Notify the peer that the connection is being closed
9037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009039{
9040 int ret;
9041
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009042 if( ssl == NULL || ssl->conf == NULL )
9043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009046
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009047 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9053 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9054 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009057 return( ret );
9058 }
9059 }
9060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009062
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009063 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009064}
9065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009067{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009068 if( transform == NULL )
9069 return;
9070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009072 deflateEnd( &transform->ctx_deflate );
9073 inflateEnd( &transform->ctx_inflate );
9074#endif
9075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9077 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079 mbedtls_md_free( &transform->md_ctx_enc );
9080 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009081
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009082 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009083}
9084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085#if defined(MBEDTLS_X509_CRT_PARSE_C)
9086static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009089
9090 while( cur != NULL )
9091 {
9092 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009094 cur = next;
9095 }
9096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009098
Hanno Becker0271f962018-08-16 13:23:47 +01009099#if defined(MBEDTLS_SSL_PROTO_DTLS)
9100
9101static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9102{
9103 unsigned offset;
9104 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9105
9106 if( hs == NULL )
9107 return;
9108
Hanno Becker283f5ef2018-08-24 09:34:47 +01009109 ssl_free_buffered_record( ssl );
9110
Hanno Becker0271f962018-08-16 13:23:47 +01009111 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009112 ssl_buffering_free_slot( ssl, offset );
9113}
9114
9115static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9116 uint8_t slot )
9117{
9118 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9119 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009120
9121 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9122 return;
9123
Hanno Beckere605b192018-08-21 15:59:07 +01009124 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009125 {
Hanno Beckere605b192018-08-21 15:59:07 +01009126 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009127 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009128 mbedtls_free( hs_buf->data );
9129 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009130 }
9131}
9132
9133#endif /* MBEDTLS_SSL_PROTO_DTLS */
9134
Gilles Peskine9b562d52018-04-25 20:32:43 +02009135void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009136{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009137 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9138
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009139 if( handshake == NULL )
9140 return;
9141
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009142#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9143 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9144 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009145 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009146 handshake->async_in_progress = 0;
9147 }
9148#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9149
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009150#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9151 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9152 mbedtls_md5_free( &handshake->fin_md5 );
9153 mbedtls_sha1_free( &handshake->fin_sha1 );
9154#endif
9155#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9156#if defined(MBEDTLS_SHA256_C)
9157 mbedtls_sha256_free( &handshake->fin_sha256 );
9158#endif
9159#if defined(MBEDTLS_SHA512_C)
9160 mbedtls_sha512_free( &handshake->fin_sha512 );
9161#endif
9162#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164#if defined(MBEDTLS_DHM_C)
9165 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167#if defined(MBEDTLS_ECDH_C)
9168 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009169#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009170#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009171 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009172#if defined(MBEDTLS_SSL_CLI_C)
9173 mbedtls_free( handshake->ecjpake_cache );
9174 handshake->ecjpake_cache = NULL;
9175 handshake->ecjpake_cache_len = 0;
9176#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009177#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009178
Janos Follath4ae5c292016-02-10 11:27:43 +00009179#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9180 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009181 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009183#endif
9184
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009185#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9186 if( handshake->psk != NULL )
9187 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009188 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009189 mbedtls_free( handshake->psk );
9190 }
9191#endif
9192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9194 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009195 /*
9196 * Free only the linked list wrapper, not the keys themselves
9197 * since the belong to the SNI callback
9198 */
9199 if( handshake->sni_key_cert != NULL )
9200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009202
9203 while( cur != NULL )
9204 {
9205 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009207 cur = next;
9208 }
9209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009211
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009212#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009213 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009214#endif
9215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009216#if defined(MBEDTLS_SSL_PROTO_DTLS)
9217 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009218 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009219 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009220#endif
9221
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009222 mbedtls_platform_zeroize( handshake,
9223 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009224}
9225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009226void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009227{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009228 if( session == NULL )
9229 return;
9230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00009232 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00009233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234 mbedtls_x509_crt_free( session->peer_cert );
9235 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00009236 }
Paul Bakkered27a042013-04-18 22:46:23 +02009237#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009238
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009239#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009240 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009241#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009242
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009243 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009244}
9245
Paul Bakker5121ce52009-01-03 21:22:43 +00009246/*
9247 * Free an SSL context
9248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009250{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009251 if( ssl == NULL )
9252 return;
9253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009255
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009256 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009257 {
Angus Grattond8213d02016-05-25 20:56:48 +10009258 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009260 }
9261
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009262 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009263 {
Angus Grattond8213d02016-05-25 20:56:48 +10009264 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009266 }
9267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009269 if( ssl->compress_buf != NULL )
9270 {
Angus Grattond8213d02016-05-25 20:56:48 +10009271 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009273 }
9274#endif
9275
Paul Bakker48916f92012-09-16 19:57:18 +00009276 if( ssl->transform )
9277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278 mbedtls_ssl_transform_free( ssl->transform );
9279 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009280 }
9281
9282 if( ssl->handshake )
9283 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009284 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009285 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9286 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288 mbedtls_free( ssl->handshake );
9289 mbedtls_free( ssl->transform_negotiate );
9290 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009291 }
9292
Paul Bakkerc0463502013-02-14 11:19:38 +01009293 if( ssl->session )
9294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 mbedtls_ssl_session_free( ssl->session );
9296 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009297 }
9298
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009299#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009300 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009301 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009302 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009304 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9308 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9311 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009312 }
9313#endif
9314
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009315#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009317#endif
9318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009320
Paul Bakker86f04f42013-02-14 11:20:09 +01009321 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009322 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009323}
9324
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009325/*
9326 * Initialze mbedtls_ssl_config
9327 */
9328void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9329{
9330 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9331}
9332
Simon Butcherc97b6972015-12-27 23:48:17 +00009333#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009334static int ssl_preset_default_hashes[] = {
9335#if defined(MBEDTLS_SHA512_C)
9336 MBEDTLS_MD_SHA512,
9337 MBEDTLS_MD_SHA384,
9338#endif
9339#if defined(MBEDTLS_SHA256_C)
9340 MBEDTLS_MD_SHA256,
9341 MBEDTLS_MD_SHA224,
9342#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009343#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009344 MBEDTLS_MD_SHA1,
9345#endif
9346 MBEDTLS_MD_NONE
9347};
Simon Butcherc97b6972015-12-27 23:48:17 +00009348#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009349
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009350static int ssl_preset_suiteb_ciphersuites[] = {
9351 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9352 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9353 0
9354};
9355
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009356#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009357static int ssl_preset_suiteb_hashes[] = {
9358 MBEDTLS_MD_SHA256,
9359 MBEDTLS_MD_SHA384,
9360 MBEDTLS_MD_NONE
9361};
9362#endif
9363
9364#if defined(MBEDTLS_ECP_C)
9365static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9366 MBEDTLS_ECP_DP_SECP256R1,
9367 MBEDTLS_ECP_DP_SECP384R1,
9368 MBEDTLS_ECP_DP_NONE
9369};
9370#endif
9371
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009372/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009373 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009374 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009375int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009376 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009377{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009378#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009379 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009380#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009381
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009382 /* Use the functions here so that they are covered in tests,
9383 * but otherwise access member directly for efficiency */
9384 mbedtls_ssl_conf_endpoint( conf, endpoint );
9385 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009386
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009387 /*
9388 * Things that are common to all presets
9389 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009390#if defined(MBEDTLS_SSL_CLI_C)
9391 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9392 {
9393 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9394#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9395 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9396#endif
9397 }
9398#endif
9399
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009400#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009401 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009402#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009403
9404#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9405 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9406#endif
9407
9408#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9409 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9410#endif
9411
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009412#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9413 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9414#endif
9415
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009416#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009417 conf->f_cookie_write = ssl_cookie_write_dummy;
9418 conf->f_cookie_check = ssl_cookie_check_dummy;
9419#endif
9420
9421#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9422 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9423#endif
9424
Janos Follath088ce432017-04-10 12:42:31 +01009425#if defined(MBEDTLS_SSL_SRV_C)
9426 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9427#endif
9428
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009429#if defined(MBEDTLS_SSL_PROTO_DTLS)
9430 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9431 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9432#endif
9433
9434#if defined(MBEDTLS_SSL_RENEGOTIATION)
9435 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009436 memset( conf->renego_period, 0x00, 2 );
9437 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009438#endif
9439
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009440#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9441 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9442 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009443 const unsigned char dhm_p[] =
9444 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9445 const unsigned char dhm_g[] =
9446 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9447
Hanno Beckera90658f2017-10-04 15:29:08 +01009448 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9449 dhm_p, sizeof( dhm_p ),
9450 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009451 {
9452 return( ret );
9453 }
9454 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009455#endif
9456
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009457 /*
9458 * Preset-specific defaults
9459 */
9460 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009461 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009462 /*
9463 * NSA Suite B
9464 */
9465 case MBEDTLS_SSL_PRESET_SUITEB:
9466 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9467 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9468 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9469 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9470
9471 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9472 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9473 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9474 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9475 ssl_preset_suiteb_ciphersuites;
9476
9477#if defined(MBEDTLS_X509_CRT_PARSE_C)
9478 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009479#endif
9480
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009481#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009482 conf->sig_hashes = ssl_preset_suiteb_hashes;
9483#endif
9484
9485#if defined(MBEDTLS_ECP_C)
9486 conf->curve_list = ssl_preset_suiteb_curves;
9487#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009488 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009489
9490 /*
9491 * Default
9492 */
9493 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009494 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9495 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9496 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9497 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9498 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9499 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9500 MBEDTLS_SSL_MIN_MINOR_VERSION :
9501 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009502 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9503 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9504
9505#if defined(MBEDTLS_SSL_PROTO_DTLS)
9506 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9507 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9508#endif
9509
9510 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9511 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9512 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9513 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9514 mbedtls_ssl_list_ciphersuites();
9515
9516#if defined(MBEDTLS_X509_CRT_PARSE_C)
9517 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9518#endif
9519
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009520#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009521 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009522#endif
9523
9524#if defined(MBEDTLS_ECP_C)
9525 conf->curve_list = mbedtls_ecp_grp_id_list();
9526#endif
9527
9528#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9529 conf->dhm_min_bitlen = 1024;
9530#endif
9531 }
9532
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009533 return( 0 );
9534}
9535
9536/*
9537 * Free mbedtls_ssl_config
9538 */
9539void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9540{
9541#if defined(MBEDTLS_DHM_C)
9542 mbedtls_mpi_free( &conf->dhm_P );
9543 mbedtls_mpi_free( &conf->dhm_G );
9544#endif
9545
9546#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9547 if( conf->psk != NULL )
9548 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009549 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009550 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009551 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009552 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009553 }
9554
9555 if( conf->psk_identity != NULL )
9556 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009557 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009558 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009559 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009560 conf->psk_identity_len = 0;
9561 }
9562#endif
9563
9564#if defined(MBEDTLS_X509_CRT_PARSE_C)
9565 ssl_key_cert_free( conf->key_cert );
9566#endif
9567
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009568 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009569}
9570
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009571#if defined(MBEDTLS_PK_C) && \
9572 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009573/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578#if defined(MBEDTLS_RSA_C)
9579 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9580 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009581#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#if defined(MBEDTLS_ECDSA_C)
9583 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9584 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009587}
9588
Hanno Becker7e5437a2017-04-28 17:15:26 +01009589unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9590{
9591 switch( type ) {
9592 case MBEDTLS_PK_RSA:
9593 return( MBEDTLS_SSL_SIG_RSA );
9594 case MBEDTLS_PK_ECDSA:
9595 case MBEDTLS_PK_ECKEY:
9596 return( MBEDTLS_SSL_SIG_ECDSA );
9597 default:
9598 return( MBEDTLS_SSL_SIG_ANON );
9599 }
9600}
9601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009603{
9604 switch( sig )
9605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606#if defined(MBEDTLS_RSA_C)
9607 case MBEDTLS_SSL_SIG_RSA:
9608 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009609#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610#if defined(MBEDTLS_ECDSA_C)
9611 case MBEDTLS_SSL_SIG_ECDSA:
9612 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009613#endif
9614 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009616 }
9617}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009618#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009619
Hanno Becker7e5437a2017-04-28 17:15:26 +01009620#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9621 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9622
9623/* Find an entry in a signature-hash set matching a given hash algorithm. */
9624mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9625 mbedtls_pk_type_t sig_alg )
9626{
9627 switch( sig_alg )
9628 {
9629 case MBEDTLS_PK_RSA:
9630 return( set->rsa );
9631 case MBEDTLS_PK_ECDSA:
9632 return( set->ecdsa );
9633 default:
9634 return( MBEDTLS_MD_NONE );
9635 }
9636}
9637
9638/* Add a signature-hash-pair to a signature-hash set */
9639void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9640 mbedtls_pk_type_t sig_alg,
9641 mbedtls_md_type_t md_alg )
9642{
9643 switch( sig_alg )
9644 {
9645 case MBEDTLS_PK_RSA:
9646 if( set->rsa == MBEDTLS_MD_NONE )
9647 set->rsa = md_alg;
9648 break;
9649
9650 case MBEDTLS_PK_ECDSA:
9651 if( set->ecdsa == MBEDTLS_MD_NONE )
9652 set->ecdsa = md_alg;
9653 break;
9654
9655 default:
9656 break;
9657 }
9658}
9659
9660/* Allow exactly one hash algorithm for each signature. */
9661void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9662 mbedtls_md_type_t md_alg )
9663{
9664 set->rsa = md_alg;
9665 set->ecdsa = md_alg;
9666}
9667
9668#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9669 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9670
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009671/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009672 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009675{
9676 switch( hash )
9677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678#if defined(MBEDTLS_MD5_C)
9679 case MBEDTLS_SSL_HASH_MD5:
9680 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009681#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682#if defined(MBEDTLS_SHA1_C)
9683 case MBEDTLS_SSL_HASH_SHA1:
9684 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686#if defined(MBEDTLS_SHA256_C)
9687 case MBEDTLS_SSL_HASH_SHA224:
9688 return( MBEDTLS_MD_SHA224 );
9689 case MBEDTLS_SSL_HASH_SHA256:
9690 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692#if defined(MBEDTLS_SHA512_C)
9693 case MBEDTLS_SSL_HASH_SHA384:
9694 return( MBEDTLS_MD_SHA384 );
9695 case MBEDTLS_SSL_HASH_SHA512:
9696 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009697#endif
9698 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009700 }
9701}
9702
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009703/*
9704 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9705 */
9706unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9707{
9708 switch( md )
9709 {
9710#if defined(MBEDTLS_MD5_C)
9711 case MBEDTLS_MD_MD5:
9712 return( MBEDTLS_SSL_HASH_MD5 );
9713#endif
9714#if defined(MBEDTLS_SHA1_C)
9715 case MBEDTLS_MD_SHA1:
9716 return( MBEDTLS_SSL_HASH_SHA1 );
9717#endif
9718#if defined(MBEDTLS_SHA256_C)
9719 case MBEDTLS_MD_SHA224:
9720 return( MBEDTLS_SSL_HASH_SHA224 );
9721 case MBEDTLS_MD_SHA256:
9722 return( MBEDTLS_SSL_HASH_SHA256 );
9723#endif
9724#if defined(MBEDTLS_SHA512_C)
9725 case MBEDTLS_MD_SHA384:
9726 return( MBEDTLS_SSL_HASH_SHA384 );
9727 case MBEDTLS_MD_SHA512:
9728 return( MBEDTLS_SSL_HASH_SHA512 );
9729#endif
9730 default:
9731 return( MBEDTLS_SSL_HASH_NONE );
9732 }
9733}
9734
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009735#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009736/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009737 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009738 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009739 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009740int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009743
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009744 if( ssl->conf->curve_list == NULL )
9745 return( -1 );
9746
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009747 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009748 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009749 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009750
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009751 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009752}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009753#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009754
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009755#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009756/*
9757 * Check if a hash proposed by the peer is in our list.
9758 * Return 0 if we're willing to use it, -1 otherwise.
9759 */
9760int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9761 mbedtls_md_type_t md )
9762{
9763 const int *cur;
9764
9765 if( ssl->conf->sig_hashes == NULL )
9766 return( -1 );
9767
9768 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9769 if( *cur == (int) md )
9770 return( 0 );
9771
9772 return( -1 );
9773}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009774#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776#if defined(MBEDTLS_X509_CRT_PARSE_C)
9777int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9778 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009779 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009780 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009781{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009782 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009784 int usage = 0;
9785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009787 const char *ext_oid;
9788 size_t ext_len;
9789#endif
9790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9792 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009793 ((void) cert);
9794 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009795 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009796#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9799 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009800 {
9801 /* Server part of the key exchange */
9802 switch( ciphersuite->key_exchange )
9803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804 case MBEDTLS_KEY_EXCHANGE_RSA:
9805 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009806 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009807 break;
9808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9810 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9811 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9812 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009813 break;
9814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9816 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009817 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009818 break;
9819
9820 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 case MBEDTLS_KEY_EXCHANGE_NONE:
9822 case MBEDTLS_KEY_EXCHANGE_PSK:
9823 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9824 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02009825 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009826 usage = 0;
9827 }
9828 }
9829 else
9830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
9832 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009833 }
9834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009836 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009837 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009838 ret = -1;
9839 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009840#else
9841 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
9845 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9848 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009849 }
9850 else
9851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9853 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009854 }
9855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009857 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009858 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009859 ret = -1;
9860 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009862
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009863 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02009866
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009867/*
9868 * Convert version numbers to/from wire format
9869 * and, for DTLS, to/from TLS equivalent.
9870 *
9871 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08009872 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009873 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
9874 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
9875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009877 unsigned char ver[2] )
9878{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879#if defined(MBEDTLS_SSL_PROTO_DTLS)
9880 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009883 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9884
9885 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
9886 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
9887 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009888 else
9889#else
9890 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009891#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009892 {
9893 ver[0] = (unsigned char) major;
9894 ver[1] = (unsigned char) minor;
9895 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009896}
9897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009899 const unsigned char ver[2] )
9900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901#if defined(MBEDTLS_SSL_PROTO_DTLS)
9902 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009903 {
9904 *major = 255 - ver[0] + 2;
9905 *minor = 255 - ver[1] + 1;
9906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009908 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
9909 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009910 else
9911#else
9912 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009913#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01009914 {
9915 *major = ver[0];
9916 *minor = ver[1];
9917 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01009918}
9919
Simon Butcher99000142016-10-13 17:21:01 +01009920int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
9921{
9922#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9923 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
9924 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9925
9926 switch( md )
9927 {
9928#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
9929#if defined(MBEDTLS_MD5_C)
9930 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01009931 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01009932#endif
9933#if defined(MBEDTLS_SHA1_C)
9934 case MBEDTLS_SSL_HASH_SHA1:
9935 ssl->handshake->calc_verify = ssl_calc_verify_tls;
9936 break;
9937#endif
9938#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
9939#if defined(MBEDTLS_SHA512_C)
9940 case MBEDTLS_SSL_HASH_SHA384:
9941 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
9942 break;
9943#endif
9944#if defined(MBEDTLS_SHA256_C)
9945 case MBEDTLS_SSL_HASH_SHA256:
9946 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
9947 break;
9948#endif
9949 default:
9950 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9951 }
9952
9953 return 0;
9954#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
9955 (void) ssl;
9956 (void) md;
9957
9958 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
9959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9960}
9961
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009962#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9963 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9964int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
9965 unsigned char *output,
9966 unsigned char *data, size_t data_len )
9967{
9968 int ret = 0;
9969 mbedtls_md5_context mbedtls_md5;
9970 mbedtls_sha1_context mbedtls_sha1;
9971
9972 mbedtls_md5_init( &mbedtls_md5 );
9973 mbedtls_sha1_init( &mbedtls_sha1 );
9974
9975 /*
9976 * digitally-signed struct {
9977 * opaque md5_hash[16];
9978 * opaque sha_hash[20];
9979 * };
9980 *
9981 * md5_hash
9982 * MD5(ClientHello.random + ServerHello.random
9983 * + ServerParams);
9984 * sha_hash
9985 * SHA(ClientHello.random + ServerHello.random
9986 * + ServerParams);
9987 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009988 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009989 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009991 goto exit;
9992 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009993 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009994 ssl->handshake->randbytes, 64 ) ) != 0 )
9995 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009997 goto exit;
9998 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01009999 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010000 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010002 goto exit;
10003 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010004 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010005 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010007 goto exit;
10008 }
10009
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010010 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010011 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010013 goto exit;
10014 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010015 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010016 ssl->handshake->randbytes, 64 ) ) != 0 )
10017 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010019 goto exit;
10020 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010021 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010022 data_len ) ) != 0 )
10023 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010025 goto exit;
10026 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010027 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010028 output + 16 ) ) != 0 )
10029 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010031 goto exit;
10032 }
10033
10034exit:
10035 mbedtls_md5_free( &mbedtls_md5 );
10036 mbedtls_sha1_free( &mbedtls_sha1 );
10037
10038 if( ret != 0 )
10039 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10040 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10041
10042 return( ret );
10043
10044}
10045#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10046 MBEDTLS_SSL_PROTO_TLS1_1 */
10047
10048#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10049 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10050int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010051 unsigned char *hash, size_t *hashlen,
10052 unsigned char *data, size_t data_len,
10053 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010054{
10055 int ret = 0;
10056 mbedtls_md_context_t ctx;
10057 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010058 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010059
10060 mbedtls_md_init( &ctx );
10061
10062 /*
10063 * digitally-signed struct {
10064 * opaque client_random[32];
10065 * opaque server_random[32];
10066 * ServerDHParams params;
10067 * };
10068 */
10069 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10070 {
10071 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10072 goto exit;
10073 }
10074 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10075 {
10076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10077 goto exit;
10078 }
10079 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10080 {
10081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10082 goto exit;
10083 }
10084 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10085 {
10086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10087 goto exit;
10088 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010089 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010090 {
10091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10092 goto exit;
10093 }
10094
10095exit:
10096 mbedtls_md_free( &ctx );
10097
10098 if( ret != 0 )
10099 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10100 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10101
10102 return( ret );
10103}
10104#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 */