blob: d4df533cf15851783d0d119bd741f6cd8bb4f0aa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Beckerd5847772018-08-28 10:09:23 +0100121/* Forward declarations for functions related to message buffering. */
122static void ssl_buffering_free( mbedtls_ssl_context *ssl );
123static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
124 uint8_t slot );
125static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
126static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
127static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
128static int ssl_buffer_message( mbedtls_ssl_context *ssl );
129static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100130static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100131
Hanno Beckera67dee22018-08-22 10:05:20 +0100132static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100133static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134{
Hanno Becker11682cc2018-08-22 14:41:02 +0100135 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
137 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100138 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100139
140 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
141}
142
Hanno Becker67bc7c32018-08-06 11:33:50 +0100143static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
144{
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 size_t const bytes_written = ssl->out_left;
146 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147
148 /* Double-check that the write-index hasn't gone
149 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100150 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100151 {
152 /* Should never happen... */
153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
154 }
155
156 return( (int) ( mtu - bytes_written ) );
157}
158
159static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
160{
161 int ret;
162 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400163 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100164
165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
166 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
167
168 if( max_len > mfl )
169 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100170
171 /* By the standard (RFC 6066 Sect. 4), the MFL extension
172 * only limits the maximum record payload size, so in theory
173 * we would be allowed to pack multiple records of payload size
174 * MFL into a single datagram. However, this would mean that there's
175 * no way to explicitly communicate MTU restrictions to the peer.
176 *
177 * The following reduction of max_len makes sure that we never
178 * write datagrams larger than MFL + Record Expansion Overhead.
179 */
180 if( max_len <= ssl->out_left )
181 return( 0 );
182
183 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100184#endif
185
186 ret = ssl_get_remaining_space_in_datagram( ssl );
187 if( ret < 0 )
188 return( ret );
189 remaining = (size_t) ret;
190
191 ret = mbedtls_ssl_get_record_expansion( ssl );
192 if( ret < 0 )
193 return( ret );
194 expansion = (size_t) ret;
195
196 if( remaining <= expansion )
197 return( 0 );
198
199 remaining -= expansion;
200 if( remaining >= max_len )
201 remaining = max_len;
202
203 return( (int) remaining );
204}
205
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206/*
207 * Double the retransmit timeout value, within the allowed range,
208 * returning -1 if the maximum value has already been reached.
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211{
212 uint32_t new_timeout;
213
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200214 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200215 return( -1 );
216
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200217 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
218 * in the following way: after the initial transmission and a first
219 * retransmission, back off to a temporary estimated MTU of 508 bytes.
220 * This value is guaranteed to be deliverable (if not guaranteed to be
221 * delivered) of any compliant IPv4 (and IPv6) network, and should work
222 * on most non-IP stacks too. */
223 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400224 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200225 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
227 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200228
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 new_timeout = 2 * ssl->handshake->retransmit_timeout;
230
231 /* Avoid arithmetic overflow and range overflow */
232 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200233 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200235 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200236 }
237
238 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240 ssl->handshake->retransmit_timeout ) );
241
242 return( 0 );
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200246{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200247 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249 ssl->handshake->retransmit_timeout ) );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200254/*
255 * Convert max_fragment_length codes to length.
256 * RFC 6066 says:
257 * enum{
258 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
259 * } MaxFragmentLength;
260 * and we add 0 -> extension unused
261 */
Angus Grattond8213d02016-05-25 20:56:48 +1000262static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200263{
Angus Grattond8213d02016-05-25 20:56:48 +1000264 switch( mfl )
265 {
266 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
267 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
268 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
269 return 512;
270 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
271 return 1024;
272 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
273 return 2048;
274 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
275 return 4096;
276 default:
277 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
278 }
279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281
Hanno Becker52055ae2019-02-06 14:30:46 +0000282int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
283 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_session_free( dst );
286 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289 if( src->peer_cert != NULL )
290 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200291 int ret;
292
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200293 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200294 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200300 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 dst->peer_cert = NULL;
304 return( ret );
305 }
306 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000307
308#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
309 if( src->peer_cert_digest != NULL )
310 {
311 dst->peer_cert_digest_len = src->peer_cert_digest_len;
312 dst->peer_cert_digest =
313 mbedtls_calloc( 1, dst->peer_cert_digest_len );
314 if( dst->peer_cert_digest == NULL )
315 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
316
317 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
318 src->peer_cert_digest_len );
319 dst->peer_cert_digest_type = src->peer_cert_digest_type;
320 }
321#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200324
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200325#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200326 if( src->ticket != NULL )
327 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200328 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200329 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200331
332 memcpy( dst->ticket, src->ticket, src->ticket_len );
333 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200334#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200335
336 return( 0 );
337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
340int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200341 const unsigned char *key_enc, const unsigned char *key_dec,
342 size_t keylen,
343 const unsigned char *iv_enc, const unsigned char *iv_dec,
344 size_t ivlen,
345 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200346 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
348int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
349int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
352#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000353
Paul Bakker5121ce52009-01-03 21:22:43 +0000354/*
355 * Key material generation
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200358static int ssl3_prf( const unsigned char *secret, size_t slen,
359 const char *label,
360 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000361 unsigned char *dstbuf, size_t dlen )
362{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100363 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000364 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200365 mbedtls_md5_context md5;
366 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000367 unsigned char padding[16];
368 unsigned char sha1sum[20];
369 ((void)label);
370
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200371 mbedtls_md5_init( &md5 );
372 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200373
Paul Bakker5f70b252012-09-13 14:23:06 +0000374 /*
375 * SSLv3:
376 * block =
377 * MD5( secret + SHA1( 'A' + secret + random ) ) +
378 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
379 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
380 * ...
381 */
382 for( i = 0; i < dlen / 16; i++ )
383 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200384 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000385
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100386 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100388 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100389 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100390 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100391 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100392 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100393 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100394 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100395 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000396
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100398 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100399 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100400 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100401 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100402 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100403 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100404 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000405 }
406
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100407exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200408 mbedtls_md5_free( &md5 );
409 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000410
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500411 mbedtls_platform_zeroize( padding, sizeof( padding ) );
412 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000413
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100414 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200419static int tls1_prf( const unsigned char *secret, size_t slen,
420 const char *label,
421 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000422 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 size_t nb, hs;
425 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200426 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 unsigned char tmp[128];
428 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 const mbedtls_md_info_t *md_info;
430 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100431 int ret;
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 hs = ( slen + 1 ) / 2;
439 S1 = secret;
440 S2 = secret + slen - hs;
441
442 nb = strlen( label );
443 memcpy( tmp + 20, label, nb );
444 memcpy( tmp + 20 + nb, random, rlen );
445 nb += rlen;
446
447 /*
448 * First compute P_md5(secret,label+random)[0..dlen]
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100454 return( ret );
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
457 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
458 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460 for( i = 0; i < dlen; i += 16 )
461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_md_hmac_reset ( &md_ctx );
463 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
464 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_md_hmac_reset ( &md_ctx );
467 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
468 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
471
472 for( j = 0; j < k; j++ )
473 dstbuf[i + j] = h_i[j];
474 }
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 /*
479 * XOR out with P_sha1(secret,label+random)[0..dlen]
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100485 return( ret );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
488 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
489 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491 for( i = 0; i < dlen; i += 20 )
492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_md_hmac_reset ( &md_ctx );
494 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
495 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_md_hmac_reset ( &md_ctx );
498 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
499 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
502
503 for( j = 0; j < k; j++ )
504 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
505 }
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100508
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500509 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
510 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 return( 0 );
513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500517#if defined(MBEDTLS_USE_PSA_CRYPTO)
518static int tls_prf_generic( mbedtls_md_type_t md_type,
519 const unsigned char *secret, size_t slen,
520 const char *label,
521 const unsigned char *random, size_t rlen,
522 unsigned char *dstbuf, size_t dlen )
523{
524 psa_status_t status;
525 psa_algorithm_t alg;
526 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500527 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500528 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
529
Andrzej Kurek2f760752019-01-28 08:08:15 -0500530 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500531 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500532
Andrzej Kurekc929a822019-01-14 03:51:11 -0500533 if( md_type == MBEDTLS_MD_SHA384 )
534 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
535 else
536 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
537
Andrzej Kurek2f760752019-01-28 08:08:15 -0500538 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500539 psa_key_policy_set_usage( &policy,
540 PSA_KEY_USAGE_DERIVE,
541 alg );
542 status = psa_set_key_policy( master_slot, &policy );
543 if( status != PSA_SUCCESS )
544 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
545
546 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
547 if( status != PSA_SUCCESS )
548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
549
550 status = psa_key_derivation( &generator,
551 master_slot, alg,
552 random, rlen,
553 (unsigned char const *) label,
554 (size_t) strlen( label ),
555 dlen );
556 if( status != PSA_SUCCESS )
557 {
558 psa_generator_abort( &generator );
559 psa_destroy_key( master_slot );
560 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
561 }
562
563 status = psa_generator_read( &generator, dstbuf, dlen );
564 if( status != PSA_SUCCESS )
565 {
566 psa_generator_abort( &generator );
567 psa_destroy_key( master_slot );
568 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
569 }
570
571 status = psa_generator_abort( &generator );
572 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500573 {
574 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500575 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500576 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500577
578 status = psa_destroy_key( master_slot );
579 if( status != PSA_SUCCESS )
580 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
581
Andrzej Kurek33171262019-01-15 03:25:18 -0500582 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500583}
584
585#else /* MBEDTLS_USE_PSA_CRYPTO */
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100588 const unsigned char *secret, size_t slen,
589 const char *label,
590 const unsigned char *random, size_t rlen,
591 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000592{
593 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100594 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000595 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
597 const mbedtls_md_info_t *md_info;
598 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100599 int ret;
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607
608 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000610
611 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100612 memcpy( tmp + md_len, label, nb );
613 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614 nb += rlen;
615
616 /*
617 * Compute P_<hash>(secret, label + random)[0..dlen]
618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100620 return( ret );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
623 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
624 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100625
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
634 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100636 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500644 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
645 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000646
647 return( 0 );
648}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500649#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100651static int tls_prf_sha256( const unsigned char *secret, size_t slen,
652 const char *label,
653 const unsigned char *random, size_t rlen,
654 unsigned char *dstbuf, size_t dlen )
655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100657 label, random, rlen, dstbuf, dlen ) );
658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200662static int tls_prf_sha384( const unsigned char *secret, size_t slen,
663 const char *label,
664 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665 unsigned char *dstbuf, size_t dlen )
666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_SHA512_C */
671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
676 defined(MBEDTLS_SSL_PROTO_TLS1_1)
677static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200678#endif
Paul Bakker380da532012-04-18 16:10:25 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SSL_PROTO_SSL3)
681static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
682static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200683#endif
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
686static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
687static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#endif
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
691#if defined(MBEDTLS_SHA256_C)
692static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
693static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
694static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_SHA512_C)
698static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
699static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
700static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703
Hanno Becker7d0a5692018-10-23 15:26:22 +0100704#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
705 defined(MBEDTLS_USE_PSA_CRYPTO)
706static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
707{
708 if( ssl->conf->f_psk != NULL )
709 {
710 /* If we've used a callback to select the PSK,
711 * the static configuration is irrelevant. */
712 if( ssl->handshake->psk_opaque != 0 )
713 return( 1 );
714
715 return( 0 );
716 }
717
718 if( ssl->conf->psk_opaque != 0 )
719 return( 1 );
720
721 return( 0 );
722}
723#endif /* MBEDTLS_USE_PSA_CRYPTO &&
724 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000727{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200728 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000729#if defined(MBEDTLS_USE_PSA_CRYPTO)
730 int psa_fallthrough;
731#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char keyblk[256];
734 unsigned char *key1;
735 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100736 unsigned char *mac_enc;
737 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000738 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000740 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 const mbedtls_cipher_info_t *cipher_info;
742 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100743
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000744 /* cf. RFC 5246, Section 8.1:
745 * "The master secret is always exactly 48 bytes in length." */
746 size_t const master_secret_len = 48;
747
Hanno Becker35b23c72018-10-23 12:10:41 +0100748#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
749 unsigned char session_hash[48];
750#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_ssl_session *session = ssl->session_negotiate;
753 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
754 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100759 if( cipher_info == NULL )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100762 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100764 }
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100767 if( md_info == NULL )
768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100770 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100772 }
773
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000775 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_SSL_PROTO_SSL3)
778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000779 {
Paul Bakker48916f92012-09-16 19:57:18 +0000780 handshake->tls_prf = ssl3_prf;
781 handshake->calc_verify = ssl_calc_verify_ssl;
782 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784 else
785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
787 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000788 {
Paul Bakker48916f92012-09-16 19:57:18 +0000789 handshake->tls_prf = tls1_prf;
790 handshake->calc_verify = ssl_calc_verify_tls;
791 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200793 else
794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
796#if defined(MBEDTLS_SHA512_C)
797 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
798 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000799 {
Paul Bakker48916f92012-09-16 19:57:18 +0000800 handshake->tls_prf = tls_prf_sha384;
801 handshake->calc_verify = ssl_calc_verify_tls_sha384;
802 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000803 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_SHA256_C)
807 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000808 {
Paul Bakker48916f92012-09-16 19:57:18 +0000809 handshake->tls_prf = tls_prf_sha256;
810 handshake->calc_verify = ssl_calc_verify_tls_sha256;
811 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000812 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200813 else
814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200819 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000820
821 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 * SSLv3:
823 * master =
824 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
825 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
826 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200827 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 * master = PRF( premaster, "master secret", randbytes )[0..47]
830 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100831 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
834 }
835 else
836 {
837 /* The label for the KDF used for key expansion.
838 * This is either "master secret" or "extended master secret"
839 * depending on whether the Extended Master Secret extension
840 * is used. */
841 char const *lbl = "master secret";
842
843 /* The salt for the KDF used for key expansion.
844 * - If the Extended Master Secret extension is not used,
845 * this is ClientHello.Random + ServerHello.Random
846 * (see Sect. 8.1 in RFC 5246).
847 * - If the Extended Master Secret extension is used,
848 * this is the transcript of the handshake so far.
849 * (see Sect. 4 in RFC 7627). */
850 unsigned char const *salt = handshake->randbytes;
851 size_t salt_len = 64;
852
853#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
854 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
855 ssl->transform_negotiate->ciphersuite_info;
856 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
857#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
860 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200863
Hanno Becker35b23c72018-10-23 12:10:41 +0100864 lbl = "extended master secret";
865 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200866 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100871 if( md_type == MBEDTLS_MD_SHA384 )
872 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200873 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100874#endif /* MBEDTLS_SHA512_C */
875 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200876 }
877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100879 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200880
Hanno Becker35b23c72018-10-23 12:10:41 +0100881 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200882 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100883#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
884
Hanno Becker7d0a5692018-10-23 15:26:22 +0100885#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
886 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
887 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
888 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
889 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100890 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100891 /* Perform PSK-to-MS expansion in a single step. */
892 psa_status_t status;
893 psa_algorithm_t alg;
894 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500895 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100896
897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
898
899 psk = ssl->conf->psk_opaque;
900 if( ssl->handshake->psk_opaque != 0 )
901 psk = ssl->handshake->psk_opaque;
902
903 if( md_type == MBEDTLS_MD_SHA384 )
904 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
905 else
906 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
907
908 status = psa_key_derivation( &generator, psk, alg,
909 salt, salt_len,
910 (unsigned char const *) lbl,
911 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000912 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100913 if( status != PSA_SUCCESS )
914 {
915 psa_generator_abort( &generator );
916 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
917 }
918
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000919 status = psa_generator_read( &generator, session->master,
920 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100921 if( status != PSA_SUCCESS )
922 {
923 psa_generator_abort( &generator );
924 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
925 }
926
927 status = psa_generator_abort( &generator );
928 if( status != PSA_SUCCESS )
929 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100930 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100931 else
932#endif
933 {
934 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
935 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000936 session->master,
937 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100938 if( ret != 0 )
939 {
940 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
941 return( ret );
942 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200943
Hanno Becker7d0a5692018-10-23 15:26:22 +0100944 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
945 handshake->premaster,
946 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100947
Hanno Becker7d0a5692018-10-23 15:26:22 +0100948 mbedtls_platform_zeroize( handshake->premaster,
949 sizeof(handshake->premaster) );
950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 /*
954 * Swap the client and server random values.
955 */
Paul Bakker48916f92012-09-16 19:57:18 +0000956 memcpy( tmp, handshake->randbytes, 64 );
957 memcpy( handshake->randbytes, tmp + 32, 32 );
958 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500959 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 /*
962 * SSLv3:
963 * key block =
964 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
965 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
966 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
967 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
968 * ...
969 *
970 * TLSv1:
971 * key block = PRF( master, "key expansion", randbytes )
972 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100973 ret = handshake->tls_prf( session->master, 48, "key expansion",
974 handshake->randbytes, 64, keyblk, 256 );
975 if( ret != 0 )
976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100978 return( ret );
979 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
982 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
983 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
984 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
985 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500987 mbedtls_platform_zeroize( handshake->randbytes,
988 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
991 * Determine the appropriate key, IV and MAC length.
992 */
Paul Bakker68884e32013-01-07 18:20:04 +0100993
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200994 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200997 cipher_info->mode == MBEDTLS_MODE_CCM ||
998 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001000 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001001
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001002 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001004
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001005 /* All modes haves 96-bit IVs;
1006 * GCM and CCM has 4 implicit and 8 explicit bytes
1007 * ChachaPoly has all 12 bytes implicit
1008 */
Paul Bakker68884e32013-01-07 18:20:04 +01001009 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001010 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1011 transform->fixed_ivlen = 12;
1012 else
1013 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001014
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001015 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
1016 taglen = transform->ciphersuite_info->flags &
1017 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1018
1019
1020 /* Minimum length of encrypted record */
1021 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1022 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 }
1024 else
1025 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001026 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1028 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001031 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001035 mac_key_len = mbedtls_md_get_size( md_info );
1036 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001039 /*
1040 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1041 * (rfc 6066 page 13 or rfc 2104 section 4),
1042 * so we only need to adjust the length here.
1043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001047
1048#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1049 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001050 * HMAC implementation which also truncates the key
1051 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001052 mac_key_len = transform->maclen;
1053#endif
1054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001056
1057 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001058 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001060 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001062 transform->minlen = transform->maclen;
1063 else
Paul Bakker68884e32013-01-07 18:20:04 +01001064 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001065 /*
1066 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001067 * 1. if EtM is in use: one block plus MAC
1068 * otherwise: * first multiple of blocklen greater than maclen
1069 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1072 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001073 {
1074 transform->minlen = transform->maclen
1075 + cipher_info->block_size;
1076 }
1077 else
1078#endif
1079 {
1080 transform->minlen = transform->maclen
1081 + cipher_info->block_size
1082 - transform->maclen % cipher_info->block_size;
1083 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1086 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1087 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001088 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001089 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1092 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1093 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001094 {
1095 transform->minlen += transform->ivlen;
1096 }
1097 else
1098#endif
1099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 }
Paul Bakker68884e32013-01-07 18:20:04 +01001103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001107 transform->keylen, transform->minlen, transform->ivlen,
1108 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
1110 /*
1111 * Finally setup the cipher contexts, IVs and MAC secrets.
1112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001114 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001116 key1 = keyblk + mac_key_len * 2;
1117 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakker68884e32013-01-07 18:20:04 +01001119 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001120 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001122 /*
1123 * This is not used in TLS v1.1.
1124 */
Paul Bakker48916f92012-09-16 19:57:18 +00001125 iv_copy_len = ( transform->fixed_ivlen ) ?
1126 transform->fixed_ivlen : transform->ivlen;
1127 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1128 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
1131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_SSL_CLI_C */
1133#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001134 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001136 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1137 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Hanno Becker81c7b182017-11-09 18:39:33 +00001139 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001140 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001142 /*
1143 * This is not used in TLS v1.1.
1144 */
Paul Bakker48916f92012-09-16 19:57:18 +00001145 iv_copy_len = ( transform->fixed_ivlen ) ?
1146 transform->fixed_ivlen : transform->ivlen;
1147 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1148 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001149 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_SSL_PROTO_SSL3)
1159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001160 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001161 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001165 }
1166
Hanno Becker81c7b182017-11-09 18:39:33 +00001167 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1168 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001169 }
1170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1172#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1173 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1174 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001175 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001176 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1177 For AEAD-based ciphersuites, there is nothing to do here. */
1178 if( mac_key_len != 0 )
1179 {
1180 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1181 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1182 }
Paul Bakker68884e32013-01-07 18:20:04 +01001183 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001184 else
1185#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001189 }
Paul Bakker68884e32013-01-07 18:20:04 +01001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1192 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001193 {
1194 int ret = 0;
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001199 transform->iv_enc, transform->iv_dec,
1200 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001201 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001202 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1205 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001206 }
1207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001209
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001210#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1211 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001212 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001213 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1214 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001215 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001216 iv_copy_len );
1217 }
1218#endif
1219
Hanno Beckerf704bef2018-11-16 15:21:18 +00001220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001221
1222 /* Only use PSA-based ciphers for TLS-1.2.
1223 * That's relevant at least for TLS-1.0, where
1224 * we assume that mbedtls_cipher_crypt() updates
1225 * the structure field for the IV, which the PSA-based
1226 * implementation currently doesn't. */
1227#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1228 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001229 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001230 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1231 cipher_info, taglen );
1232 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1233 {
1234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1235 return( ret );
1236 }
1237
1238 if( ret == 0 )
1239 {
1240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1241 psa_fallthrough = 0;
1242 }
1243 else
1244 {
1245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1246 psa_fallthrough = 1;
1247 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001248 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001249 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001250 psa_fallthrough = 1;
1251#else
1252 psa_fallthrough = 1;
1253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001254
Hanno Beckercb1cc802018-11-17 22:27:38 +00001255 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001256#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001257 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001258 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001261 return( ret );
1262 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001263
Hanno Beckerf704bef2018-11-16 15:21:18 +00001264#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001265 /* Only use PSA-based ciphers for TLS-1.2.
1266 * That's relevant at least for TLS-1.0, where
1267 * we assume that mbedtls_cipher_crypt() updates
1268 * the structure field for the IV, which the PSA-based
1269 * implementation currently doesn't. */
1270#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1271 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001272 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001273 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1274 cipher_info, taglen );
1275 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1276 {
1277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1278 return( ret );
1279 }
1280
1281 if( ret == 0 )
1282 {
1283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1284 psa_fallthrough = 0;
1285 }
1286 else
1287 {
1288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1289 psa_fallthrough = 1;
1290 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001291 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001292 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001293 psa_fallthrough = 1;
1294#else
1295 psa_fallthrough = 1;
1296#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001297
Hanno Beckercb1cc802018-11-17 22:27:38 +00001298 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001299#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001300 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001301 cipher_info ) ) != 0 )
1302 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001304 return( ret );
1305 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001308 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312 return( ret );
1313 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001316 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001320 return( ret );
1321 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_CIPHER_MODE_CBC)
1324 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1327 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001330 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001331 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1334 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001337 return( ret );
1338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001342 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001345 // Initialize compression
1346 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001348 {
Paul Bakker16770332013-10-11 09:59:44 +02001349 if( ssl->compress_buf == NULL )
1350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001352 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001353 if( ssl->compress_buf == NULL )
1354 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001356 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001357 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001358 }
1359 }
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001362
Paul Bakker48916f92012-09-16 19:57:18 +00001363 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1364 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001365
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001366 if( deflateInit( &transform->ctx_deflate,
1367 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001368 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1371 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001372 }
1373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
1378 return( 0 );
1379}
1380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_SSL_PROTO_SSL3)
1382void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001384 mbedtls_md5_context md5;
1385 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 unsigned char pad_1[48];
1387 unsigned char pad_2[48];
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001391 mbedtls_md5_init( &md5 );
1392 mbedtls_sha1_init( &sha1 );
1393
1394 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1395 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Paul Bakker380da532012-04-18 16:10:25 +00001397 memset( pad_1, 0x36, 48 );
1398 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1401 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1402 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001404 mbedtls_md5_starts_ret( &md5 );
1405 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1406 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1407 mbedtls_md5_update_ret( &md5, hash, 16 );
1408 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001410 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1411 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1412 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001413
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001414 mbedtls_sha1_starts_ret( &sha1 );
1415 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1416 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1417 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1418 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001422
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001423 mbedtls_md5_free( &md5 );
1424 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001425
Paul Bakker380da532012-04-18 16:10:25 +00001426 return;
1427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1431void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001432{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001433 mbedtls_md5_context md5;
1434 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001438 mbedtls_md5_init( &md5 );
1439 mbedtls_sha1_init( &sha1 );
1440
1441 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1442 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Andrzej Kurekeb342242019-01-29 09:14:33 -05001444 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001445 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001450 mbedtls_md5_free( &md5 );
1451 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001452
Paul Bakker380da532012-04-18 16:10:25 +00001453 return;
1454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1458#if defined(MBEDTLS_SHA256_C)
1459void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001460{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001461#if defined(MBEDTLS_USE_PSA_CRYPTO)
1462 size_t hash_size;
1463 psa_status_t status;
1464 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1465
1466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1467 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1468 if( status != PSA_SUCCESS )
1469 {
1470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1471 return;
1472 }
1473
1474 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1475 if( status != PSA_SUCCESS )
1476 {
1477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1478 return;
1479 }
1480 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1482#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001483 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001485 mbedtls_sha256_init( &sha256 );
1486
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001488
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001489 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001490 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001496#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001497 return;
1498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SHA512_C)
1502void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001503{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001504#if defined(MBEDTLS_USE_PSA_CRYPTO)
1505 size_t hash_size;
1506 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001507 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001508
1509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001510 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001511 if( status != PSA_SUCCESS )
1512 {
1513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1514 return;
1515 }
1516
Andrzej Kurek972fba52019-01-30 03:29:12 -05001517 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001518 if( status != PSA_SUCCESS )
1519 {
1520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1521 return;
1522 }
1523 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1525#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001526 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001527
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001528 mbedtls_sha512_init( &sha512 );
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001531
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001532 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001533 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001538 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001539#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 return;
1541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_SHA512_C */
1543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1546int 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 +02001547{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001548 unsigned char *p = ssl->handshake->premaster;
1549 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001550 const unsigned char *psk = ssl->conf->psk;
1551 size_t psk_len = ssl->conf->psk_len;
1552
1553 /* If the psk callback was called, use its result */
1554 if( ssl->handshake->psk != NULL )
1555 {
1556 psk = ssl->handshake->psk;
1557 psk_len = ssl->handshake->psk_len;
1558 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559
1560 /*
1561 * PMS = struct {
1562 * opaque other_secret<0..2^16-1>;
1563 * opaque psk<0..2^16-1>;
1564 * };
1565 * with "other_secret" depending on the particular key exchange
1566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1568 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001569 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001570 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001572
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001573 *(p++) = (unsigned char)( psk_len >> 8 );
1574 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001575
1576 if( end < p || (size_t)( end - p ) < psk_len )
1577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1578
1579 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001580 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 }
1582 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1584#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1585 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001586 {
1587 /*
1588 * other_secret already set by the ClientKeyExchange message,
1589 * and is 48 bytes long
1590 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001591 if( end - p < 2 )
1592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1593
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001594 *p++ = 0;
1595 *p++ = 48;
1596 p += 48;
1597 }
1598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1600#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1601 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001603 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001604 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001606 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001608 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001609 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 return( ret );
1613 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001614 *(p++) = (unsigned char)( len >> 8 );
1615 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 p += len;
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 }
1620 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1622#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1623 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001624 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001625 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 size_t zlen;
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001629 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001630 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633 return( ret );
1634 }
1635
1636 *(p++) = (unsigned char)( zlen >> 8 );
1637 *(p++) = (unsigned char)( zlen );
1638 p += zlen;
1639
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001640 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1641 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001642 }
1643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648 }
1649
1650 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001651 if( end - p < 2 )
1652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001653
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001654 *(p++) = (unsigned char)( psk_len >> 8 );
1655 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001656
1657 if( end < p || (size_t)( end - p ) < psk_len )
1658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1659
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001660 memcpy( p, psk, psk_len );
1661 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001662
1663 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1664
1665 return( 0 );
1666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001670/*
1671 * SSLv3.0 MAC functions
1672 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001673#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674static void ssl_mac( mbedtls_md_context_t *md_ctx,
1675 const unsigned char *secret,
1676 const unsigned char *buf, size_t len,
1677 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001678 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001679{
1680 unsigned char header[11];
1681 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001682 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1684 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001685
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001686 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001688 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001689 else
Paul Bakker68884e32013-01-07 18:20:04 +01001690 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
1692 memcpy( header, ctr, 8 );
1693 header[ 8] = (unsigned char) type;
1694 header[ 9] = (unsigned char)( len >> 8 );
1695 header[10] = (unsigned char)( len );
1696
Paul Bakker68884e32013-01-07 18:20:04 +01001697 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_md_starts( md_ctx );
1699 mbedtls_md_update( md_ctx, secret, md_size );
1700 mbedtls_md_update( md_ctx, padding, padlen );
1701 mbedtls_md_update( md_ctx, header, 11 );
1702 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001703 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Paul Bakker68884e32013-01-07 18:20:04 +01001705 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_md_starts( md_ctx );
1707 mbedtls_md_update( md_ctx, secret, md_size );
1708 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001709 mbedtls_md_update( md_ctx, out, md_size );
1710 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1715 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001716 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001717#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001718#endif
1719
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001720/* The function below is only used in the Lucky 13 counter-measure in
1721 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1722#if defined(SSL_SOME_MODES_USE_MAC) && \
1723 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1724 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1725 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1726/* This function makes sure every byte in the memory region is accessed
1727 * (in ascending addresses order) */
1728static void ssl_read_memory( unsigned char *p, size_t len )
1729{
1730 unsigned char acc = 0;
1731 volatile unsigned char force;
1732
1733 for( ; len != 0; p++, len-- )
1734 acc ^= *p;
1735
1736 force = acc;
1737 (void) force;
1738}
1739#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1740
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001741/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001747 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001755 }
1756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001760 ssl->out_msg, ssl->out_msglen );
1761
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001763 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001765#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 if( mode == MBEDTLS_MODE_STREAM ||
1767 ( mode == MBEDTLS_MODE_CBC
1768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1769 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001770#endif
1771 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_SSL_PROTO_SSL3)
1774 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001775 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001776 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001777
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 ssl_mac( &ssl->transform_out->md_ctx_enc,
1779 ssl->transform_out->mac_enc,
1780 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001781 ssl->out_ctr, ssl->out_msgtype,
1782 mac );
1783
1784 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001785 }
1786 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1789 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1790 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001791 {
Hanno Becker992b6872017-11-09 18:57:39 +00001792 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1795 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1796 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1797 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001799 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001801
1802 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 }
1804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001805#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001809 }
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001812 ssl->out_msg + ssl->out_msglen,
1813 ssl->transform_out->maclen );
1814
1815 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001816 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001817 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001818#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 /*
1821 * Encrypt
1822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1824 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001826 int ret;
1827 size_t olen = 0;
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 "including %d bytes of padding",
1831 ssl->out_msglen, 0 ) );
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001834 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001835 ssl->transform_out->ivlen,
1836 ssl->out_msg, ssl->out_msglen,
1837 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001840 return( ret );
1841 }
1842
1843 if( ssl->out_msglen != olen )
1844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 }
Paul Bakker68884e32013-01-07 18:20:04 +01001849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851#if defined(MBEDTLS_GCM_C) || \
1852 defined(MBEDTLS_CCM_C) || \
1853 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 mode == MBEDTLS_MODE_CCM ||
1856 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001857 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001858 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001859 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001860 unsigned char *enc_msg;
1861 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862 unsigned char iv[12];
1863 mbedtls_ssl_transform *transform = ssl->transform_out;
1864 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001866 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001867
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001868 /*
1869 * Prepare additional authenticated data
1870 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001871 memcpy( add_data, ssl->out_ctr, 8 );
1872 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001874 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001875 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1876 add_data[12] = ssl->out_msglen & 0xFF;
1877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001878 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001879
Paul Bakker68884e32013-01-07 18:20:04 +01001880 /*
1881 * Generate IV
1882 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001883 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1884 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001885 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001886 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1887 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1888 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1889
1890 }
1891 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1892 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001893 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001894 unsigned char i;
1895
1896 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1897
1898 for( i = 0; i < 8; i++ )
1899 iv[i+4] ^= ssl->out_ctr[i];
1900 }
1901 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001902 {
1903 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001906 }
1907
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001908 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1909 iv, transform->ivlen );
1910 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1911 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001912
Paul Bakker68884e32013-01-07 18:20:04 +01001913 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001914 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001915 */
1916 enc_msg = ssl->out_msg;
1917 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001921 "including 0 bytes of padding",
1922 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001923
Paul Bakker68884e32013-01-07 18:20:04 +01001924 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001925 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001926 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001927 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1928 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001929 add_data, 13,
1930 enc_msg, enc_msglen,
1931 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001932 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001935 return( ret );
1936 }
1937
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001938 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001942 }
1943
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001944 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001945 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1951#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001952 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001955 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001956 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001957 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001958
Paul Bakker48916f92012-09-16 19:57:18 +00001959 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1960 ssl->transform_out->ivlen;
1961 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 padlen = 0;
1963
1964 for( i = 0; i <= padlen; i++ )
1965 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1966
1967 ssl->out_msglen += padlen + 1;
1968
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001969 enc_msglen = ssl->out_msglen;
1970 enc_msg = ssl->out_msg;
1971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001973 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001974 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1975 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001978 {
1979 /*
1980 * Generate IV
1981 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001982 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001983 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001984 if( ret != 0 )
1985 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001986
Paul Bakker92be97b2013-01-02 17:30:03 +01001987 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001988 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001989
1990 /*
1991 * Fix pointer positions and message length with added IV
1992 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001993 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001995 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002001 ssl->out_msglen, ssl->transform_out->ivlen,
2002 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02002005 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002006 ssl->transform_out->ivlen,
2007 enc_msg, enc_msglen,
2008 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002011 return( ret );
2012 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002013
Paul Bakkercca5b812013-08-31 17:40:26 +02002014 if( enc_msglen != olen )
2015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002018 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2021 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002022 {
2023 /*
2024 * Save IV in SSL3 and TLS1
2025 */
2026 memcpy( ssl->transform_out->iv_enc,
2027 ssl->transform_out->cipher_ctx_enc.iv,
2028 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002030#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002033 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002034 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002035 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2036
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002037 /*
2038 * MAC(MAC_write_key, seq_num +
2039 * TLSCipherText.type +
2040 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002041 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002042 * IV + // except for TLS 1.0
2043 * ENC(content + padding + padding_length));
2044 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002045 unsigned char pseudo_hdr[13];
2046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002048
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002049 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2050 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002051 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2052 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2057 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002058 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002059 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002061
Hanno Becker3d8c9072018-01-05 16:24:22 +00002062 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2063 ssl->transform_out->maclen );
2064
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002065 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002066 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002067 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002072 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002078 /* Make extra sure authentication was performed, exactly once */
2079 if( auth_done != 1 )
2080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002083 }
2084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 return( 0 );
2088}
2089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002093 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002094#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002095 size_t padlen = 0, correct = 1;
2096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002100 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002104 }
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002107
Paul Bakker48916f92012-09-16 19:57:18 +00002108 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002111 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2116 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002117 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002118 int ret;
2119 size_t olen = 0;
2120
Paul Bakker68884e32013-01-07 18:20:04 +01002121 padlen = 0;
2122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002124 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002125 ssl->transform_in->ivlen,
2126 ssl->in_msg, ssl->in_msglen,
2127 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002130 return( ret );
2131 }
2132
2133 if( ssl->in_msglen != olen )
2134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
Paul Bakker68884e32013-01-07 18:20:04 +01002139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002141#if defined(MBEDTLS_GCM_C) || \
2142 defined(MBEDTLS_CCM_C) || \
2143 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002145 mode == MBEDTLS_MODE_CCM ||
2146 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002147 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002148 int ret;
2149 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002150 unsigned char *dec_msg;
2151 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002152 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002153 unsigned char iv[12];
2154 mbedtls_ssl_transform *transform = ssl->transform_in;
2155 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002157 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002158
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002159 /*
2160 * Compute and update sizes
2161 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002162 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002165 "+ taglen (%d)", ssl->in_msglen,
2166 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002168 }
2169 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2170
Paul Bakker68884e32013-01-07 18:20:04 +01002171 dec_msg = ssl->in_msg;
2172 dec_msg_result = ssl->in_msg;
2173 ssl->in_msglen = dec_msglen;
2174
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002175 /*
2176 * Prepare additional authenticated data
2177 */
Paul Bakker68884e32013-01-07 18:20:04 +01002178 memcpy( add_data, ssl->in_ctr, 8 );
2179 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002181 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002182 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2183 add_data[12] = ssl->in_msglen & 0xFF;
2184
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002185 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002186
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002187 /*
2188 * Prepare IV
2189 */
2190 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2191 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002192 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002193 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2194 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002195
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002196 }
2197 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2198 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002199 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002200 unsigned char i;
2201
2202 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2203
2204 for( i = 0; i < 8; i++ )
2205 iv[i+4] ^= ssl->in_ctr[i];
2206 }
2207 else
2208 {
2209 /* Reminder if we ever add an AEAD mode with a different size */
2210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2212 }
2213
2214 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002216
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002217 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002218 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002221 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002222 add_data, 13,
2223 dec_msg, dec_msglen,
2224 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002225 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2230 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002231
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002232 return( ret );
2233 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002234 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002235
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002236 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002240 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2244#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002245 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 {
Paul Bakker45829992013-01-03 14:52:21 +01002248 /*
2249 * Decrypt and check the padding
2250 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002251 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252 unsigned char *dec_msg;
2253 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002254 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002255 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002256 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002257
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 /*
Paul Bakker45829992013-01-03 14:52:21 +01002259 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2262 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002263 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002264#endif
Paul Bakker45829992013-01-03 14:52:21 +01002265
2266 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2267 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002270 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2271 ssl->transform_in->ivlen,
2272 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002274 }
2275
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002276 dec_msglen = ssl->in_msglen;
2277 dec_msg = ssl->in_msg;
2278 dec_msg_result = ssl->in_msg;
2279
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002280 /*
2281 * Authenticate before decrypt if enabled
2282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2284 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002285 {
Hanno Becker992b6872017-11-09 18:57:39 +00002286 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002287 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002290
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002291 dec_msglen -= ssl->transform_in->maclen;
2292 ssl->in_msglen -= ssl->transform_in->maclen;
2293
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002294 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2295 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2296 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2297 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2302 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002303 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002304 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002308 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002309 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002310 ssl->transform_in->maclen );
2311
Hanno Becker992b6872017-11-09 18:57:39 +00002312 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2313 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002318 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002319 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002322
2323 /*
2324 * Check length sanity
2325 */
2326 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002329 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002331 }
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002334 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002335 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002338 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002340 dec_msglen -= ssl->transform_in->ivlen;
2341 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002342
Paul Bakker48916f92012-09-16 19:57:18 +00002343 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002344 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002349 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002350 ssl->transform_in->ivlen,
2351 dec_msg, dec_msglen,
2352 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002355 return( ret );
2356 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002357
Paul Bakkercca5b812013-08-31 17:40:26 +02002358 if( dec_msglen != olen )
2359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2361 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002362 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2365 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002366 {
2367 /*
2368 * Save IV in SSL3 and TLS1
2369 */
2370 memcpy( ssl->transform_in->iv_dec,
2371 ssl->transform_in->cipher_ctx_dec.iv,
2372 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
2376 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002377
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002378 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002379 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_SSL_DEBUG_ALL)
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002383 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002384#endif
Paul Bakker45829992013-01-03 14:52:21 +01002385 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002386 correct = 0;
2387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#if defined(MBEDTLS_SSL_PROTO_SSL3)
2390 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
Paul Bakker48916f92012-09-16 19:57:18 +00002392 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#if defined(MBEDTLS_SSL_DEBUG_ALL)
2395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002397 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002398#endif
Paul Bakker45829992013-01-03 14:52:21 +01002399 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401 }
2402 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2404#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2405 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2406 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 {
2408 /*
Paul Bakker45829992013-01-03 14:52:21 +01002409 * TLSv1+: always check the padding up to the first failure
2410 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002412 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002413 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002414 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002415
Paul Bakker956c9e02013-12-19 14:42:28 +01002416 /*
2417 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002418 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002419 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002420 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002421 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002422 *
2423 * In both cases we reset padding_idx to a safe value (0) to
2424 * prevent out-of-buffer reads.
2425 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002426 correct &= ( padlen <= ssl->in_msglen );
2427 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002428 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002429
2430 padding_idx *= correct;
2431
Angus Grattonb512bc12018-06-19 15:57:50 +10002432 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002433 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002434 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002435 pad_count += real_count *
2436 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2437 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002438
2439 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002442 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002444#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002445 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002447 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2449 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002453 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002454
2455 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002457 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002459 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002465#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 * Authenticate if not done yet.
2472 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002474#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002478
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002479 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002481 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2482 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_PROTO_SSL3)
2485 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002486 {
2487 ssl_mac( &ssl->transform_in->md_ctx_dec,
2488 ssl->transform_in->mac_dec,
2489 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002490 ssl->in_ctr, ssl->in_msgtype,
2491 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002492 }
2493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2495#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2496 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2497 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002498 {
2499 /*
2500 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002501 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002502 *
2503 * Known timing attacks:
2504 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2505 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002506 * To compensate for different timings for the MAC calculation
2507 * depending on how much padding was removed (which is determined
2508 * by padlen), process extra_run more blocks through the hash
2509 * function.
2510 *
2511 * The formula in the paper is
2512 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2513 * where L1 is the size of the header plus the decrypted message
2514 * plus CBC padding and L2 is the size of the header plus the
2515 * decrypted message. This is for an underlying hash function
2516 * with 64-byte blocks.
2517 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2518 * correctly. We round down instead of up, so -56 is the correct
2519 * value for our calculations instead of -55.
2520 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002521 * Repeat the formula rather than defining a block_size variable.
2522 * This avoids requiring division by a variable at runtime
2523 * (which would be marginally less efficient and would require
2524 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002525 */
2526 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002527
2528 /*
2529 * The next two sizes are the minimum and maximum values of
2530 * in_msglen over all padlen values.
2531 *
2532 * They're independent of padlen, since we previously did
2533 * in_msglen -= padlen.
2534 *
2535 * Note that max_len + maclen is never more than the buffer
2536 * length, as we previously did in_msglen -= maclen too.
2537 */
2538 const size_t max_len = ssl->in_msglen + padlen;
2539 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2540
Gilles Peskine20b44082018-05-29 14:06:49 +02002541 switch( ssl->transform_in->ciphersuite_info->mac )
2542 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002543#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2544 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002545 case MBEDTLS_MD_MD5:
2546 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002547 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002548 /* 8 bytes of message size, 64-byte compression blocks */
2549 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2550 ( 13 + ssl->in_msglen + 8 ) / 64;
2551 break;
2552#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002553#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002554 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002555 /* 16 bytes of message size, 128-byte compression blocks */
2556 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2557 ( 13 + ssl->in_msglen + 16 ) / 128;
2558 break;
2559#endif
2560 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2563 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002564
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002565 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2568 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2569 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2570 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002572 /* Make sure we access everything even when padlen > 0. This
2573 * makes the synchronisation requirements for just-in-time
2574 * Prime+Probe attacks much tighter and hopefully impractical. */
2575 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002576 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002577
2578 /* Call mbedtls_md_process at least once due to cache attacks
2579 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002580 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002584
2585 /* Make sure we access all the memory that could contain the MAC,
2586 * before we check it in the next code block. This makes the
2587 * synchronisation requirements for just-in-time Prime+Probe
2588 * attacks much tighter and hopefully impractical. */
2589 ssl_read_memory( ssl->in_msg + min_len,
2590 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002591 }
2592 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2594 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002600#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002601 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2602 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2603 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Hanno Becker992b6872017-11-09 18:57:39 +00002606 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2607 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_DEBUG_ALL)
2610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002611#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002612 correct = 0;
2613 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002614 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002615 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002616
2617 /*
2618 * Finally check the correct flag
2619 */
2620 if( correct == 0 )
2621 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002622#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002623
2624 /* Make extra sure authentication was performed, exactly once */
2625 if( auth_done != 1 )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
2631 if( ssl->in_msglen == 0 )
2632 {
Angus Gratton34817922018-06-19 15:58:22 +10002633#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2634 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2635 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2636 {
2637 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2639 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2640 }
2641#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2642
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 ssl->nb_zero++;
2644
2645 /*
2646 * Three or more empty messages may be a DoS attack
2647 * (excessive CPU consumption).
2648 */
2649 if( ssl->nb_zero > 3 )
2650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655 }
2656 else
2657 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002661 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002662 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002663 }
2664 else
2665#endif
2666 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002667 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002668 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2669 if( ++ssl->in_ctr[i - 1] != 0 )
2670 break;
2671
2672 /* The loop goes to its end iff the counter is wrapping */
2673 if( i == ssl_ep_len( ssl ) )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2676 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002677 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002678 }
2679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
2682 return( 0 );
2683}
2684
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002685#undef MAC_NONE
2686#undef MAC_PLAINTEXT
2687#undef MAC_CIPHERTEXT
2688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002690/*
2691 * Compression/decompression functions
2692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002694{
2695 int ret;
2696 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002697 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002698 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002699 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002702
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002703 if( len_pre == 0 )
2704 return( 0 );
2705
Paul Bakker2770fbd2012-07-03 13:30:23 +00002706 memcpy( msg_pre, ssl->out_msg, len_pre );
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709 ssl->out_msglen ) );
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002712 ssl->out_msg, ssl->out_msglen );
2713
Paul Bakker48916f92012-09-16 19:57:18 +00002714 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2715 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2716 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002717 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002718
Paul Bakker48916f92012-09-16 19:57:18 +00002719 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720 if( ret != Z_OK )
2721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2723 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724 }
2725
Angus Grattond8213d02016-05-25 20:56:48 +10002726 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002727 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002730 ssl->out_msglen ) );
2731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002733 ssl->out_msg, ssl->out_msglen );
2734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002736
2737 return( 0 );
2738}
2739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002741{
2742 int ret;
2743 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002744 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002745 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002746 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002749
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002750 if( len_pre == 0 )
2751 return( 0 );
2752
Paul Bakker2770fbd2012-07-03 13:30:23 +00002753 memcpy( msg_pre, ssl->in_msg, len_pre );
2754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002756 ssl->in_msglen ) );
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002759 ssl->in_msg, ssl->in_msglen );
2760
Paul Bakker48916f92012-09-16 19:57:18 +00002761 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2762 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2763 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002764 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002765 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766
Paul Bakker48916f92012-09-16 19:57:18 +00002767 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002768 if( ret != Z_OK )
2769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2771 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002772 }
2773
Angus Grattond8213d02016-05-25 20:56:48 +10002774 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002775 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002778 ssl->in_msglen ) );
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781 ssl->in_msg, ssl->in_msglen );
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002784
2785 return( 0 );
2786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2790static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_PROTO_DTLS)
2793static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002794{
2795 /* If renegotiation is not enforced, retransmit until we would reach max
2796 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002797 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002798 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002799 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002800 unsigned char doublings = 1;
2801
2802 while( ratio != 0 )
2803 {
2804 ++doublings;
2805 ratio >>= 1;
2806 }
2807
2808 if( ++ssl->renego_records_seen > doublings )
2809 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002811 return( 0 );
2812 }
2813 }
2814
2815 return( ssl_write_hello_request( ssl ) );
2816}
2817#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002821 * Fill the input message buffer by appending data to it.
2822 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002823 *
2824 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2825 * available (from this read and/or a previous one). Otherwise, an error code
2826 * is returned (possibly EOF or WANT_READ).
2827 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002828 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2829 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2830 * since we always read a whole datagram at once.
2831 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002832 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002833 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002836{
Paul Bakker23986e52011-04-24 08:57:21 +00002837 int ret;
2838 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002842 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002845 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002847 }
2848
Angus Grattond8213d02016-05-25 20:56:48 +10002849 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002853 }
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002856 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002858 uint32_t timeout;
2859
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002860 /* Just to be sure */
2861 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2862 {
2863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2864 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2866 }
2867
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002868 /*
2869 * The point is, we need to always read a full datagram at once, so we
2870 * sometimes read more then requested, and handle the additional data.
2871 * It could be the rest of the current record (while fetching the
2872 * header) and/or some other records in the same datagram.
2873 */
2874
2875 /*
2876 * Move to the next record in the already read datagram if applicable
2877 */
2878 if( ssl->next_record_offset != 0 )
2879 {
2880 if( ssl->in_left < ssl->next_record_offset )
2881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002884 }
2885
2886 ssl->in_left -= ssl->next_record_offset;
2887
2888 if( ssl->in_left != 0 )
2889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002891 ssl->next_record_offset ) );
2892 memmove( ssl->in_hdr,
2893 ssl->in_hdr + ssl->next_record_offset,
2894 ssl->in_left );
2895 }
2896
2897 ssl->next_record_offset = 0;
2898 }
2899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002902
2903 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002904 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002905 */
2906 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002909 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002910 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002911
2912 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002913 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002914 * are not at the beginning of a new record, the caller did something
2915 * wrong.
2916 */
2917 if( ssl->in_left != 0 )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002921 }
2922
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002923 /*
2924 * Don't even try to read if time's out already.
2925 * This avoids by-passing the timer when repeatedly receiving messages
2926 * that will end up being dropped.
2927 */
2928 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002929 {
2930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002931 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002932 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002933 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002934 {
Angus Grattond8213d02016-05-25 20:56:48 +10002935 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002938 timeout = ssl->handshake->retransmit_timeout;
2939 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002940 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002943
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002944 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002945 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2946 timeout );
2947 else
2948 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002951
2952 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002954 }
2955
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002956 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002959 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002962 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002963 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002966 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002967 }
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002972 return( ret );
2973 }
2974
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002975 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002978 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002980 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002984 return( ret );
2985 }
2986
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002987 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002988 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002990 }
2991
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 if( ret < 0 )
2993 return( ret );
2994
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002995 ssl->in_left = ret;
2996 }
2997 else
2998#endif
2999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003001 ssl->in_left, nb_want ) );
3002
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003003 while( ssl->in_left < nb_want )
3004 {
3005 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003006
3007 if( ssl_check_timer( ssl ) != 0 )
3008 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3009 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003010 {
3011 if( ssl->f_recv_timeout != NULL )
3012 {
3013 ret = ssl->f_recv_timeout( ssl->p_bio,
3014 ssl->in_hdr + ssl->in_left, len,
3015 ssl->conf->read_timeout );
3016 }
3017 else
3018 {
3019 ret = ssl->f_recv( ssl->p_bio,
3020 ssl->in_hdr + ssl->in_left, len );
3021 }
3022 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003025 ssl->in_left, nb_want ) );
3026 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003027
3028 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003030
3031 if( ret < 0 )
3032 return( ret );
3033
mohammad160352aecb92018-03-28 23:41:40 -07003034 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003035 {
Darryl Green11999bb2018-03-13 15:22:58 +00003036 MBEDTLS_SSL_DEBUG_MSG( 1,
3037 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003038 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3040 }
3041
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003042 ssl->in_left += ret;
3043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 }
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
3048 return( 0 );
3049}
3050
3051/*
3052 * Flush any data not yet written
3053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003055{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003056 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003057 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003061 if( ssl->f_send == NULL )
3062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003064 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003066 }
3067
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003068 /* Avoid incrementing counter if data is flushed */
3069 if( ssl->out_left == 0 )
3070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003072 return( 0 );
3073 }
3074
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 while( ssl->out_left > 0 )
3076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3078 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Hanno Becker2b1e3542018-08-06 11:19:13 +01003080 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003081 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
3085 if( ret <= 0 )
3086 return( ret );
3087
mohammad160352aecb92018-03-28 23:41:40 -07003088 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003089 {
Darryl Green11999bb2018-03-13 15:22:58 +00003090 MBEDTLS_SSL_DEBUG_MSG( 1,
3091 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003092 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3094 }
3095
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 ssl->out_left -= ret;
3097 }
3098
Hanno Becker2b1e3542018-08-06 11:19:13 +01003099#if defined(MBEDTLS_SSL_PROTO_DTLS)
3100 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003101 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003102 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003103 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003104 else
3105#endif
3106 {
3107 ssl->out_hdr = ssl->out_buf + 8;
3108 }
3109 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
3113 return( 0 );
3114}
3115
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003116/*
3117 * Functions to handle the DTLS retransmission state machine
3118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003120/*
3121 * Append current handshake message to current outgoing flight
3122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3127 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3128 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003129
3130 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003131 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003132 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003135 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003136 }
3137
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003138 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003142 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003143 }
3144
3145 /* Copy current handshake message with headers */
3146 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3147 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003148 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003149 msg->next = NULL;
3150
3151 /* Append to the current flight */
3152 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003153 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003154 else
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003157 while( cur->next != NULL )
3158 cur = cur->next;
3159 cur->next = msg;
3160 }
3161
Hanno Becker3b235902018-08-06 09:54:53 +01003162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163 return( 0 );
3164}
3165
3166/*
3167 * Free the current flight of handshake messages
3168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 mbedtls_ssl_flight_item *cur = flight;
3172 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173
3174 while( cur != NULL )
3175 {
3176 next = cur->next;
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 mbedtls_free( cur->p );
3179 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180
3181 cur = next;
3182 }
3183}
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3186static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003187#endif
3188
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003190 * Swap transform_out and out_ctr with the alternative ones
3191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003195 unsigned char tmp_out_ctr[8];
3196
3197 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003200 return;
3201 }
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003204
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003205 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003206 tmp_transform = ssl->transform_out;
3207 ssl->transform_out = ssl->handshake->alt_transform_out;
3208 ssl->handshake->alt_transform_out = tmp_transform;
3209
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003210 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003211 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3212 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003213 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003214
3215 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003216 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3219 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3224 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003225 }
3226 }
3227#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003228}
3229
3230/*
3231 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003232 */
3233int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3234{
3235 int ret = 0;
3236
3237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3238
3239 ret = mbedtls_ssl_flight_transmit( ssl );
3240
3241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3242
3243 return( ret );
3244}
3245
3246/*
3247 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003248 *
3249 * Need to remember the current message in case flush_output returns
3250 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003251 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003253int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003254{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003255 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003259 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003261
3262 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003263 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003264 ssl_swap_epochs( ssl );
3265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003267 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003268
3269 while( ssl->handshake->cur_msg != NULL )
3270 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003271 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003272 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003273
Hanno Beckere1dcb032018-08-17 16:47:58 +01003274 int const is_finished =
3275 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3276 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3277
Hanno Becker04da1892018-08-14 13:22:10 +01003278 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3279 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3280
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003281 /* Swap epochs before sending Finished: we can't do it after
3282 * sending ChangeCipherSpec, in case write returns WANT_READ.
3283 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003284 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003285 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003287 ssl_swap_epochs( ssl );
3288 }
3289
Hanno Becker67bc7c32018-08-06 11:33:50 +01003290 ret = ssl_get_remaining_payload_in_datagram( ssl );
3291 if( ret < 0 )
3292 return( ret );
3293 max_frag_len = (size_t) ret;
3294
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003295 /* CCS is copied as is, while HS messages may need fragmentation */
3296 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3297 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003298 if( max_frag_len == 0 )
3299 {
3300 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3301 return( ret );
3302
3303 continue;
3304 }
3305
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003306 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003307 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003308 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003310 /* Update position inside current message */
3311 ssl->handshake->cur_msg_p += cur->len;
3312 }
3313 else
3314 {
3315 const unsigned char * const p = ssl->handshake->cur_msg_p;
3316 const size_t hs_len = cur->len - 12;
3317 const size_t frag_off = p - ( cur->p + 12 );
3318 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003319 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320
Hanno Beckere1dcb032018-08-17 16:47:58 +01003321 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003322 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003323 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003324 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3327 return( ret );
3328
3329 continue;
3330 }
3331 max_hs_frag_len = max_frag_len - 12;
3332
3333 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3334 max_hs_frag_len : rem_len;
3335
3336 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003337 {
3338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003339 (unsigned) cur_hs_frag_len,
3340 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003341 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003342
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003343 /* Messages are stored with handshake headers as if not fragmented,
3344 * copy beginning of headers then fill fragmentation fields.
3345 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3346 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003347
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003348 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3349 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3350 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3351
Hanno Becker67bc7c32018-08-06 11:33:50 +01003352 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3353 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3354 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003355
3356 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3357
Hanno Becker3f7b9732018-08-28 09:53:25 +01003358 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003359 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3360 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003361 ssl->out_msgtype = cur->type;
3362
3363 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003364 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003365 }
3366
3367 /* If done with the current message move to the next one if any */
3368 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3369 {
3370 if( cur->next != NULL )
3371 {
3372 ssl->handshake->cur_msg = cur->next;
3373 ssl->handshake->cur_msg_p = cur->next->p + 12;
3374 }
3375 else
3376 {
3377 ssl->handshake->cur_msg = NULL;
3378 ssl->handshake->cur_msg_p = NULL;
3379 }
3380 }
3381
3382 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003383 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003386 return( ret );
3387 }
3388 }
3389
Hanno Becker67bc7c32018-08-06 11:33:50 +01003390 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3391 return( ret );
3392
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003393 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3395 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003396 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3400 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003401
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003403
3404 return( 0 );
3405}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003406
3407/*
3408 * To be called when the last message of an incoming flight is received.
3409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003411{
3412 /* We won't need to resend that one any more */
3413 ssl_flight_free( ssl->handshake->flight );
3414 ssl->handshake->flight = NULL;
3415 ssl->handshake->cur_msg = NULL;
3416
3417 /* The next incoming flight will start with this msg_seq */
3418 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3419
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003420 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003421 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003422
Hanno Becker0271f962018-08-16 13:23:47 +01003423 /* Clear future message buffering structure. */
3424 ssl_buffering_free( ssl );
3425
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003426 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003427 ssl_set_timer( ssl, 0 );
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3430 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003433 }
3434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003436}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003437
3438/*
3439 * To be called when the last message of an outgoing flight is send.
3440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003442{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003443 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003444 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3447 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003450 }
3451 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455
Paul Bakker5121ce52009-01-03 21:22:43 +00003456/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003457 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003459
3460/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003461 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003462 *
3463 * - fill in handshake headers
3464 * - update handshake checksum
3465 * - DTLS: save message for resending
3466 * - then pass to the record layer
3467 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003468 * DTLS: except for HelloRequest, messages are only queued, and will only be
3469 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003470 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003471 * Inputs:
3472 * - ssl->out_msglen: 4 + actual handshake message len
3473 * (4 is the size of handshake headers for TLS)
3474 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3475 * - ssl->out_msg + 4: the handshake message body
3476 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003477 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003478 * - ssl->out_msglen: the length of the record contents
3479 * (including handshake headers but excluding record headers)
3480 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003481 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003482int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003483{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003484 int ret;
3485 const size_t hs_len = ssl->out_msglen - 4;
3486 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3489
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003490 /*
3491 * Sanity checks
3492 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003493 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003494 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3495 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003496 /* In SSLv3, the client might send a NoCertificate alert. */
3497#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3498 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3499 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3500 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3501#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3502 {
3503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3505 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003508 /* Whenever we send anything different from a
3509 * HelloRequest we should be in a handshake - double check. */
3510 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3511 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003512 ssl->handshake == NULL )
3513 {
3514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003522 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003527
Hanno Beckerb50a2532018-08-06 11:52:54 +01003528 /* Double-check that we did not exceed the bounds
3529 * of the outgoing record buffer.
3530 * This should never fail as the various message
3531 * writing functions must obey the bounds of the
3532 * outgoing record buffer, but better be safe.
3533 *
3534 * Note: We deliberately do not check for the MTU or MFL here.
3535 */
3536 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3537 {
3538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3539 "size %u, maximum %u",
3540 (unsigned) ssl->out_msglen,
3541 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3543 }
3544
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003545 /*
3546 * Fill handshake headers
3547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003550 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3551 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3552 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003554 /*
3555 * DTLS has additional fields in the Handshake layer,
3556 * between the length field and the actual payload:
3557 * uint16 message_seq;
3558 * uint24 fragment_offset;
3559 * uint24 fragment_length;
3560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003563 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003564 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003565 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003566 {
3567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3568 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003569 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003570 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3572 }
3573
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003574 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003575 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003576
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003577 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003578 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003579 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003580 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3581 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3582 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003583 }
3584 else
3585 {
3586 ssl->out_msg[4] = 0;
3587 ssl->out_msg[5] = 0;
3588 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003589
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003590 /* Handshake hashes are computed without fragmentation,
3591 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003592 memset( ssl->out_msg + 6, 0x00, 3 );
3593 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003596
Hanno Becker0207e532018-08-28 10:28:28 +01003597 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003598 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3599 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 }
3601
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003602 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003605 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3606 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 {
3608 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611 return( ret );
3612 }
3613 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003614 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003615#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003616 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003617 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003618 {
3619 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3620 return( ret );
3621 }
3622 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003623
3624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3625
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003626 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003627}
3628
3629/*
3630 * Record layer functions
3631 */
3632
3633/*
3634 * Write current record.
3635 *
3636 * Uses:
3637 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3638 * - ssl->out_msglen: length of the record content (excl headers)
3639 * - ssl->out_msg: record content
3640 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003641int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003642{
3643 int ret, done = 0;
3644 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003645 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003646
3647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003650 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003652 {
3653 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656 return( ret );
3657 }
3658
3659 len = ssl->out_msglen;
3660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3664 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 ret = mbedtls_ssl_hw_record_write( ssl );
3669 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003673 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003674
3675 if( ret == 0 )
3676 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003679 if( !done )
3680 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003681 unsigned i;
3682 size_t protected_record_size;
3683
Paul Bakker05ef8352012-05-08 09:17:57 +00003684 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003686 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003687
Hanno Becker19859472018-08-06 09:40:20 +01003688 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003689 ssl->out_len[0] = (unsigned char)( len >> 8 );
3690 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003691
Paul Bakker48916f92012-09-16 19:57:18 +00003692 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003693 {
3694 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003697 return( ret );
3698 }
3699
3700 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003701 ssl->out_len[0] = (unsigned char)( len >> 8 );
3702 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003703 }
3704
Hanno Becker2b1e3542018-08-06 11:19:13 +01003705 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3706
3707#if defined(MBEDTLS_SSL_PROTO_DTLS)
3708 /* In case of DTLS, double-check that we don't exceed
3709 * the remaining space in the datagram. */
3710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3711 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003712 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003713 if( ret < 0 )
3714 return( ret );
3715
3716 if( protected_record_size > (size_t) ret )
3717 {
3718 /* Should never happen */
3719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3720 }
3721 }
3722#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003725 "version = [%d:%d], msglen = %d",
3726 ssl->out_hdr[0], ssl->out_hdr[1],
3727 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003730 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003731
3732 ssl->out_left += protected_record_size;
3733 ssl->out_hdr += protected_record_size;
3734 ssl_update_out_pointers( ssl, ssl->transform_out );
3735
Hanno Becker04484622018-08-06 09:49:38 +01003736 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3737 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3738 break;
3739
3740 /* The loop goes to its end iff the counter is wrapping */
3741 if( i == ssl_ep_len( ssl ) )
3742 {
3743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3744 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
Hanno Becker67bc7c32018-08-06 11:33:50 +01003748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3750 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003752 size_t remaining;
3753 ret = ssl_get_remaining_payload_in_datagram( ssl );
3754 if( ret < 0 )
3755 {
3756 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3757 ret );
3758 return( ret );
3759 }
3760
3761 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003763 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003764 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003765 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003766 else
3767 {
Hanno Becker513815a2018-08-20 11:56:09 +01003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769 }
3770 }
3771#endif /* MBEDTLS_SSL_PROTO_DTLS */
3772
3773 if( ( flush == SSL_FORCE_FLUSH ) &&
3774 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 return( ret );
3778 }
3779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781
3782 return( 0 );
3783}
3784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003786
3787static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3788{
3789 if( ssl->in_msglen < ssl->in_hslen ||
3790 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3791 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3792 {
3793 return( 1 );
3794 }
3795 return( 0 );
3796}
Hanno Becker44650b72018-08-16 12:51:11 +01003797
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003798static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003799{
3800 return( ( ssl->in_msg[9] << 16 ) |
3801 ( ssl->in_msg[10] << 8 ) |
3802 ssl->in_msg[11] );
3803}
3804
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003805static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003806{
3807 return( ( ssl->in_msg[6] << 16 ) |
3808 ( ssl->in_msg[7] << 8 ) |
3809 ssl->in_msg[8] );
3810}
3811
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003812static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003813{
3814 uint32_t msg_len, frag_off, frag_len;
3815
3816 msg_len = ssl_get_hs_total_len( ssl );
3817 frag_off = ssl_get_hs_frag_off( ssl );
3818 frag_len = ssl_get_hs_frag_len( ssl );
3819
3820 if( frag_off > msg_len )
3821 return( -1 );
3822
3823 if( frag_len > msg_len - frag_off )
3824 return( -1 );
3825
3826 if( frag_len + 12 > ssl->in_msglen )
3827 return( -1 );
3828
3829 return( 0 );
3830}
3831
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003832/*
3833 * Mark bits in bitmask (used for DTLS HS reassembly)
3834 */
3835static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3836{
3837 unsigned int start_bits, end_bits;
3838
3839 start_bits = 8 - ( offset % 8 );
3840 if( start_bits != 8 )
3841 {
3842 size_t first_byte_idx = offset / 8;
3843
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003844 /* Special case */
3845 if( len <= start_bits )
3846 {
3847 for( ; len != 0; len-- )
3848 mask[first_byte_idx] |= 1 << ( start_bits - len );
3849
3850 /* Avoid potential issues with offset or len becoming invalid */
3851 return;
3852 }
3853
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003854 offset += start_bits; /* Now offset % 8 == 0 */
3855 len -= start_bits;
3856
3857 for( ; start_bits != 0; start_bits-- )
3858 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3859 }
3860
3861 end_bits = len % 8;
3862 if( end_bits != 0 )
3863 {
3864 size_t last_byte_idx = ( offset + len ) / 8;
3865
3866 len -= end_bits; /* Now len % 8 == 0 */
3867
3868 for( ; end_bits != 0; end_bits-- )
3869 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3870 }
3871
3872 memset( mask + offset / 8, 0xFF, len / 8 );
3873}
3874
3875/*
3876 * Check that bitmask is full
3877 */
3878static int ssl_bitmask_check( unsigned char *mask, size_t len )
3879{
3880 size_t i;
3881
3882 for( i = 0; i < len / 8; i++ )
3883 if( mask[i] != 0xFF )
3884 return( -1 );
3885
3886 for( i = 0; i < len % 8; i++ )
3887 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3888 return( -1 );
3889
3890 return( 0 );
3891}
3892
Hanno Becker56e205e2018-08-16 09:06:12 +01003893/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003894static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003895 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003896{
Hanno Becker56e205e2018-08-16 09:06:12 +01003897 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003898
Hanno Becker56e205e2018-08-16 09:06:12 +01003899 alloc_len = 12; /* Handshake header */
3900 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003901
Hanno Beckerd07df862018-08-16 09:14:58 +01003902 if( add_bitmap )
3903 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003904
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003905 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003906}
Hanno Becker56e205e2018-08-16 09:06:12 +01003907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003909
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003910static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003911{
3912 return( ( ssl->in_msg[1] << 16 ) |
3913 ( ssl->in_msg[2] << 8 ) |
3914 ssl->in_msg[3] );
3915}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003916
Simon Butcher99000142016-10-13 17:21:01 +01003917int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003918{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003922 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003924 }
3925
Hanno Becker12555c62018-08-16 12:47:53 +01003926 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003929 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003930 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003933 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003934 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003935 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003936 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003937
Hanno Becker44650b72018-08-16 12:51:11 +01003938 if( ssl_check_hs_header( ssl ) != 0 )
3939 {
3940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3941 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3942 }
3943
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003944 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003945 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3946 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3947 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3948 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003949 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003950 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3951 {
3952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3953 recv_msg_seq,
3954 ssl->handshake->in_msg_seq ) );
3955 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3956 }
3957
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003958 /* Retransmit only on last message from previous flight, to avoid
3959 * too many retransmissions.
3960 * Besides, No sane server ever retransmits HelloVerifyRequest */
3961 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003965 "message_seq = %d, start_of_flight = %d",
3966 recv_msg_seq,
3967 ssl->handshake->in_flight_start_seq ) );
3968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003972 return( ret );
3973 }
3974 }
3975 else
3976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003978 "message_seq = %d, expected = %d",
3979 recv_msg_seq,
3980 ssl->handshake->in_msg_seq ) );
3981 }
3982
Hanno Becker90333da2017-10-10 11:27:13 +01003983 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003984 }
3985 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003986
Hanno Becker6d97ef52018-08-16 13:09:04 +01003987 /* Message reassembly is handled alongside buffering of future
3988 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003989 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003990 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003991 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003994 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003995 }
3996 }
3997 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003999 /* With TLS we don't handle fragmentation (for now) */
4000 if( ssl->in_msglen < ssl->in_hslen )
4001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4003 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004004 }
4005
Simon Butcher99000142016-10-13 17:21:01 +01004006 return( 0 );
4007}
4008
4009void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4010{
Hanno Becker0271f962018-08-16 13:23:47 +01004011 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004012
Hanno Becker0271f962018-08-16 13:23:47 +01004013 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004014 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004015 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004016 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004017
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004018 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004020 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004021 ssl->handshake != NULL )
4022 {
Hanno Becker0271f962018-08-16 13:23:47 +01004023 unsigned offset;
4024 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004025
Hanno Becker0271f962018-08-16 13:23:47 +01004026 /* Increment handshake sequence number */
4027 hs->in_msg_seq++;
4028
4029 /*
4030 * Clear up handshake buffering and reassembly structure.
4031 */
4032
4033 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004034 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004035
4036 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004037 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4038 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004039 offset++, hs_buf++ )
4040 {
4041 *hs_buf = *(hs_buf + 1);
4042 }
4043
4044 /* Create a fresh last entry */
4045 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004046 }
4047#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004048}
4049
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004050/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004051 * DTLS anti-replay: RFC 6347 4.1.2.6
4052 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004053 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4054 * Bit n is set iff record number in_window_top - n has been seen.
4055 *
4056 * Usually, in_window_top is the last record number seen and the lsb of
4057 * in_window is set. The only exception is the initial state (record number 0
4058 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4061static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004062{
4063 ssl->in_window_top = 0;
4064 ssl->in_window = 0;
4065}
4066
4067static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4068{
4069 return( ( (uint64_t) buf[0] << 40 ) |
4070 ( (uint64_t) buf[1] << 32 ) |
4071 ( (uint64_t) buf[2] << 24 ) |
4072 ( (uint64_t) buf[3] << 16 ) |
4073 ( (uint64_t) buf[4] << 8 ) |
4074 ( (uint64_t) buf[5] ) );
4075}
4076
4077/*
4078 * Return 0 if sequence number is acceptable, -1 otherwise
4079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004081{
4082 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4083 uint64_t bit;
4084
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004085 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004086 return( 0 );
4087
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004088 if( rec_seqnum > ssl->in_window_top )
4089 return( 0 );
4090
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004091 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004092
4093 if( bit >= 64 )
4094 return( -1 );
4095
4096 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4097 return( -1 );
4098
4099 return( 0 );
4100}
4101
4102/*
4103 * Update replay window on new validated record
4104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004106{
4107 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4108
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004109 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004110 return;
4111
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004112 if( rec_seqnum > ssl->in_window_top )
4113 {
4114 /* Update window_top and the contents of the window */
4115 uint64_t shift = rec_seqnum - ssl->in_window_top;
4116
4117 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004118 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004119 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004120 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004121 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004122 ssl->in_window |= 1;
4123 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004124
4125 ssl->in_window_top = rec_seqnum;
4126 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004127 else
4128 {
4129 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004130 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004131
4132 if( bit < 64 ) /* Always true, but be extra sure */
4133 ssl->in_window |= (uint64_t) 1 << bit;
4134 }
4135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004137
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004138#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004139/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004140static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4141
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004142/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004143 * Without any SSL context, check if a datagram looks like a ClientHello with
4144 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004145 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004146 *
4147 * - if cookie is valid, return 0
4148 * - if ClientHello looks superficially valid but cookie is not,
4149 * fill obuf and set olen, then
4150 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4151 * - otherwise return a specific error code
4152 */
4153static int ssl_check_dtls_clihlo_cookie(
4154 mbedtls_ssl_cookie_write_t *f_cookie_write,
4155 mbedtls_ssl_cookie_check_t *f_cookie_check,
4156 void *p_cookie,
4157 const unsigned char *cli_id, size_t cli_id_len,
4158 const unsigned char *in, size_t in_len,
4159 unsigned char *obuf, size_t buf_len, size_t *olen )
4160{
4161 size_t sid_len, cookie_len;
4162 unsigned char *p;
4163
4164 if( f_cookie_write == NULL || f_cookie_check == NULL )
4165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4166
4167 /*
4168 * Structure of ClientHello with record and handshake headers,
4169 * and expected values. We don't need to check a lot, more checks will be
4170 * done when actually parsing the ClientHello - skipping those checks
4171 * avoids code duplication and does not make cookie forging any easier.
4172 *
4173 * 0-0 ContentType type; copied, must be handshake
4174 * 1-2 ProtocolVersion version; copied
4175 * 3-4 uint16 epoch; copied, must be 0
4176 * 5-10 uint48 sequence_number; copied
4177 * 11-12 uint16 length; (ignored)
4178 *
4179 * 13-13 HandshakeType msg_type; (ignored)
4180 * 14-16 uint24 length; (ignored)
4181 * 17-18 uint16 message_seq; copied
4182 * 19-21 uint24 fragment_offset; copied, must be 0
4183 * 22-24 uint24 fragment_length; (ignored)
4184 *
4185 * 25-26 ProtocolVersion client_version; (ignored)
4186 * 27-58 Random random; (ignored)
4187 * 59-xx SessionID session_id; 1 byte len + sid_len content
4188 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4189 * ...
4190 *
4191 * Minimum length is 61 bytes.
4192 */
4193 if( in_len < 61 ||
4194 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4195 in[3] != 0 || in[4] != 0 ||
4196 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4197 {
4198 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4199 }
4200
4201 sid_len = in[59];
4202 if( sid_len > in_len - 61 )
4203 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4204
4205 cookie_len = in[60 + sid_len];
4206 if( cookie_len > in_len - 60 )
4207 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4208
4209 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4210 cli_id, cli_id_len ) == 0 )
4211 {
4212 /* Valid cookie */
4213 return( 0 );
4214 }
4215
4216 /*
4217 * If we get here, we've got an invalid cookie, let's prepare HVR.
4218 *
4219 * 0-0 ContentType type; copied
4220 * 1-2 ProtocolVersion version; copied
4221 * 3-4 uint16 epoch; copied
4222 * 5-10 uint48 sequence_number; copied
4223 * 11-12 uint16 length; olen - 13
4224 *
4225 * 13-13 HandshakeType msg_type; hello_verify_request
4226 * 14-16 uint24 length; olen - 25
4227 * 17-18 uint16 message_seq; copied
4228 * 19-21 uint24 fragment_offset; copied
4229 * 22-24 uint24 fragment_length; olen - 25
4230 *
4231 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4232 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4233 *
4234 * Minimum length is 28.
4235 */
4236 if( buf_len < 28 )
4237 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4238
4239 /* Copy most fields and adapt others */
4240 memcpy( obuf, in, 25 );
4241 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4242 obuf[25] = 0xfe;
4243 obuf[26] = 0xff;
4244
4245 /* Generate and write actual cookie */
4246 p = obuf + 28;
4247 if( f_cookie_write( p_cookie,
4248 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4249 {
4250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4251 }
4252
4253 *olen = p - obuf;
4254
4255 /* Go back and fill length fields */
4256 obuf[27] = (unsigned char)( *olen - 28 );
4257
4258 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4259 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4260 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4261
4262 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4263 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4264
4265 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4266}
4267
4268/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004269 * Handle possible client reconnect with the same UDP quadruplet
4270 * (RFC 6347 Section 4.2.8).
4271 *
4272 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4273 * that looks like a ClientHello.
4274 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004275 * - if the input looks like a ClientHello without cookies,
4276 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004277 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004278 * - if the input looks like a ClientHello with a valid cookie,
4279 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004280 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004281 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004282 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004283 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004284 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4285 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004286 */
4287static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4288{
4289 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004290 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004291
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004292 ret = ssl_check_dtls_clihlo_cookie(
4293 ssl->conf->f_cookie_write,
4294 ssl->conf->f_cookie_check,
4295 ssl->conf->p_cookie,
4296 ssl->cli_id, ssl->cli_id_len,
4297 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004298 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004299
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004300 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4301
4302 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004303 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004304 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004305 * If the error is permanent we'll catch it later,
4306 * if it's not, then hopefully it'll work next time. */
4307 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4308
4309 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004310 }
4311
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004312 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004313 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004314 /* Got a valid cookie, partially reset context */
4315 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4316 {
4317 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4318 return( ret );
4319 }
4320
4321 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004322 }
4323
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004324 return( ret );
4325}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004326#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004327
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004329 * ContentType type;
4330 * ProtocolVersion version;
4331 * uint16 epoch; // DTLS only
4332 * uint48 sequence_number; // DTLS only
4333 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004334 *
4335 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004336 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004337 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4338 *
4339 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004340 * 1. proceed with the record if this function returns 0
4341 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4342 * 3. return CLIENT_RECONNECT if this function return that value
4343 * 4. drop the whole datagram if this function returns anything else.
4344 * Point 2 is needed when the peer is resending, and we have already received
4345 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004349 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 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 +02004352
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004354 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004355 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004359 ssl->in_msgtype,
4360 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004362 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4364 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4365 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4366 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004369
4370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004371 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4372 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004373 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4374#endif /* MBEDTLS_SSL_PROTO_DTLS */
4375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4376 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004379 }
4380
4381 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004382 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4385 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 }
4387
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004388 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4391 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 }
4393
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004394 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004395 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004396 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4399 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004400 }
4401
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004402 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004403 * DTLS-related tests.
4404 * Check epoch before checking length constraint because
4405 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4406 * message gets duplicated before the corresponding Finished message,
4407 * the second ChangeCipherSpec should be discarded because it belongs
4408 * to an old epoch, but not because its length is shorter than
4409 * the minimum record length for packets using the new record transform.
4410 * Note that these two kinds of failures are handled differently,
4411 * as an unexpected record is silently skipped but an invalid
4412 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004413 */
4414#if defined(MBEDTLS_SSL_PROTO_DTLS)
4415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4416 {
4417 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4418
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004419 /* Check epoch (and sequence number) with DTLS */
4420 if( rec_epoch != ssl->in_epoch )
4421 {
4422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4423 "expected %d, received %d",
4424 ssl->in_epoch, rec_epoch ) );
4425
4426#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4427 /*
4428 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4429 * access the first byte of record content (handshake type), as we
4430 * have an active transform (possibly iv_len != 0), so use the
4431 * fact that the record header len is 13 instead.
4432 */
4433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4434 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4435 rec_epoch == 0 &&
4436 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4437 ssl->in_left > 13 &&
4438 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4441 "from the same port" ) );
4442 return( ssl_handle_possible_reconnect( ssl ) );
4443 }
4444 else
4445#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004446 {
4447 /* Consider buffering the record. */
4448 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4449 {
4450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4451 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4452 }
4453
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004454 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004455 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004456 }
4457
4458#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4459 /* Replay detection only works for the current epoch */
4460 if( rec_epoch == ssl->in_epoch &&
4461 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4462 {
4463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4464 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4465 }
4466#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004467
Hanno Becker52c6dc62017-05-26 16:07:36 +01004468 /* Drop unexpected ApplicationData records,
4469 * except at the beginning of renegotiations */
4470 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4471 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4472#if defined(MBEDTLS_SSL_RENEGOTIATION)
4473 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4474 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4475#endif
4476 )
4477 {
4478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4479 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4480 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004481 }
4482#endif /* MBEDTLS_SSL_PROTO_DTLS */
4483
Hanno Becker52c6dc62017-05-26 16:07:36 +01004484
4485 /* Check length against bounds of the current transform and version */
4486 if( ssl->transform_in == NULL )
4487 {
4488 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004489 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004490 {
4491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4492 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4493 }
4494 }
4495 else
4496 {
4497 if( ssl->in_msglen < ssl->transform_in->minlen )
4498 {
4499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4500 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4501 }
4502
4503#if defined(MBEDTLS_SSL_PROTO_SSL3)
4504 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004505 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004506 {
4507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4508 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4509 }
4510#endif
4511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4512 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4513 /*
4514 * TLS encrypted messages can have up to 256 bytes of padding
4515 */
4516 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4517 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004518 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004519 {
4520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4521 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4522 }
4523#endif
4524 }
4525
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004526 return( 0 );
4527}
Paul Bakker5121ce52009-01-03 21:22:43 +00004528
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004529/*
4530 * If applicable, decrypt (and decompress) record content
4531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533{
4534 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4537 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4540 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 ret = mbedtls_ssl_hw_record_read( ssl );
4545 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004549 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004550
4551 if( ret == 0 )
4552 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004553 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004555 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 {
4557 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004560 return( ret );
4561 }
4562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 ssl->in_msg, ssl->in_msglen );
4565
Angus Grattond8213d02016-05-25 20:56:48 +10004566 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4569 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 }
4571 }
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004574 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004576 {
4577 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004580 return( ret );
4581 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004586 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004589 }
4590#endif
4591
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592 return( 0 );
4593}
4594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004596
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004597/*
4598 * Read a record.
4599 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004600 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4601 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4602 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004603 */
Hanno Becker1097b342018-08-15 14:09:41 +01004604
4605/* Helper functions for mbedtls_ssl_read_record(). */
4606static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004607static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4608static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004609
Hanno Becker327c93b2018-08-15 13:56:18 +01004610int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004611 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004612{
4613 int ret;
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004616
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004617 if( ssl->keep_current_message == 0 )
4618 {
4619 do {
Simon Butcher99000142016-10-13 17:21:01 +01004620
Hanno Becker26994592018-08-15 14:14:59 +01004621 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004622 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004623 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004624
Hanno Beckere74d5562018-08-15 14:26:08 +01004625 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004626 {
Hanno Becker40f50842018-08-15 14:48:01 +01004627#if defined(MBEDTLS_SSL_PROTO_DTLS)
4628 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004629
Hanno Becker40f50842018-08-15 14:48:01 +01004630 /* We only check for buffered messages if the
4631 * current datagram is fully consumed. */
4632 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004633 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004634 {
Hanno Becker40f50842018-08-15 14:48:01 +01004635 if( ssl_load_buffered_message( ssl ) == 0 )
4636 have_buffered = 1;
4637 }
4638
4639 if( have_buffered == 0 )
4640#endif /* MBEDTLS_SSL_PROTO_DTLS */
4641 {
4642 ret = ssl_get_next_record( ssl );
4643 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4644 continue;
4645
4646 if( ret != 0 )
4647 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004648 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004649 return( ret );
4650 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004651 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004652 }
4653
4654 ret = mbedtls_ssl_handle_message_type( ssl );
4655
Hanno Becker40f50842018-08-15 14:48:01 +01004656#if defined(MBEDTLS_SSL_PROTO_DTLS)
4657 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4658 {
4659 /* Buffer future message */
4660 ret = ssl_buffer_message( ssl );
4661 if( ret != 0 )
4662 return( ret );
4663
4664 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4665 }
4666#endif /* MBEDTLS_SSL_PROTO_DTLS */
4667
Hanno Becker90333da2017-10-10 11:27:13 +01004668 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4669 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004670
4671 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004672 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004673 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004674 return( ret );
4675 }
4676
Hanno Becker327c93b2018-08-15 13:56:18 +01004677 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004678 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004679 {
4680 mbedtls_ssl_update_handshake_status( ssl );
4681 }
Simon Butcher99000142016-10-13 17:21:01 +01004682 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004683 else
Simon Butcher99000142016-10-13 17:21:01 +01004684 {
Hanno Becker02f59072018-08-15 14:00:24 +01004685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004686 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004687 }
4688
4689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4690
4691 return( 0 );
4692}
4693
Hanno Becker40f50842018-08-15 14:48:01 +01004694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004695static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004696{
Hanno Becker40f50842018-08-15 14:48:01 +01004697 if( ssl->in_left > ssl->next_record_offset )
4698 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004699
Hanno Becker40f50842018-08-15 14:48:01 +01004700 return( 0 );
4701}
4702
4703static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4704{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004705 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004706 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004707 int ret = 0;
4708
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004709 if( hs == NULL )
4710 return( -1 );
4711
Hanno Beckere00ae372018-08-20 09:39:42 +01004712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4713
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004714 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4715 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4716 {
4717 /* Check if we have seen a ChangeCipherSpec before.
4718 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004719 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004720 {
4721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4722 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004723 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004724 }
4725
Hanno Becker39b8bc92018-08-28 17:17:13 +01004726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004727 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4728 ssl->in_msglen = 1;
4729 ssl->in_msg[0] = 1;
4730
4731 /* As long as they are equal, the exact value doesn't matter. */
4732 ssl->in_left = 0;
4733 ssl->next_record_offset = 0;
4734
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004735 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004736 goto exit;
4737 }
Hanno Becker37f95322018-08-16 13:55:32 +01004738
Hanno Beckerb8f50142018-08-28 10:01:34 +01004739#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004740 /* Debug only */
4741 {
4742 unsigned offset;
4743 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4744 {
4745 hs_buf = &hs->buffering.hs[offset];
4746 if( hs_buf->is_valid == 1 )
4747 {
4748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4749 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004750 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004751 }
4752 }
4753 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004754#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004755
4756 /* Check if we have buffered and/or fully reassembled the
4757 * next handshake message. */
4758 hs_buf = &hs->buffering.hs[0];
4759 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4760 {
4761 /* Synthesize a record containing the buffered HS message. */
4762 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4763 ( hs_buf->data[2] << 8 ) |
4764 hs_buf->data[3];
4765
4766 /* Double-check that we haven't accidentally buffered
4767 * a message that doesn't fit into the input buffer. */
4768 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4769 {
4770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4772 }
4773
4774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4775 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4776 hs_buf->data, msg_len + 12 );
4777
4778 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4779 ssl->in_hslen = msg_len + 12;
4780 ssl->in_msglen = msg_len + 12;
4781 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4782
4783 ret = 0;
4784 goto exit;
4785 }
4786 else
4787 {
4788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4789 hs->in_msg_seq ) );
4790 }
4791
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004792 ret = -1;
4793
4794exit:
4795
4796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4797 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004798}
4799
Hanno Beckera02b0b42018-08-21 17:20:27 +01004800static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4801 size_t desired )
4802{
4803 int offset;
4804 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4806 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004807
Hanno Becker01315ea2018-08-21 17:22:17 +01004808 /* Get rid of future records epoch first, if such exist. */
4809 ssl_free_buffered_record( ssl );
4810
4811 /* Check if we have enough space available now. */
4812 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4813 hs->buffering.total_bytes_buffered ) )
4814 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004816 return( 0 );
4817 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004818
Hanno Becker4f432ad2018-08-28 10:02:32 +01004819 /* We don't have enough space to buffer the next expected handshake
4820 * message. Remove buffers used for future messages to gain space,
4821 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004822 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4823 offset >= 0; offset-- )
4824 {
4825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4826 offset ) );
4827
Hanno Beckerb309b922018-08-23 13:18:05 +01004828 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004829
4830 /* Check if we have enough space available now. */
4831 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4832 hs->buffering.total_bytes_buffered ) )
4833 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004835 return( 0 );
4836 }
4837 }
4838
4839 return( -1 );
4840}
4841
Hanno Becker40f50842018-08-15 14:48:01 +01004842static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4843{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004844 int ret = 0;
4845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4846
4847 if( hs == NULL )
4848 return( 0 );
4849
4850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4851
4852 switch( ssl->in_msgtype )
4853 {
4854 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004856
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004857 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004858 break;
4859
4860 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004861 {
4862 unsigned recv_msg_seq_offset;
4863 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4864 mbedtls_ssl_hs_buffer *hs_buf;
4865 size_t msg_len = ssl->in_hslen - 12;
4866
4867 /* We should never receive an old handshake
4868 * message - double-check nonetheless. */
4869 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4870 {
4871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4873 }
4874
4875 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4876 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4877 {
4878 /* Silently ignore -- message too far in the future */
4879 MBEDTLS_SSL_DEBUG_MSG( 2,
4880 ( "Ignore future HS message with sequence number %u, "
4881 "buffering window %u - %u",
4882 recv_msg_seq, ssl->handshake->in_msg_seq,
4883 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4884
4885 goto exit;
4886 }
4887
4888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4889 recv_msg_seq, recv_msg_seq_offset ) );
4890
4891 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4892
4893 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004894 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004895 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004896 size_t reassembly_buf_sz;
4897
Hanno Becker37f95322018-08-16 13:55:32 +01004898 hs_buf->is_fragmented =
4899 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4900
4901 /* We copy the message back into the input buffer
4902 * after reassembly, so check that it's not too large.
4903 * This is an implementation-specific limitation
4904 * and not one from the standard, hence it is not
4905 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004906 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004907 {
4908 /* Ignore message */
4909 goto exit;
4910 }
4911
Hanno Beckere0b150f2018-08-21 15:51:03 +01004912 /* Check if we have enough space to buffer the message. */
4913 if( hs->buffering.total_bytes_buffered >
4914 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4915 {
4916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4918 }
4919
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004920 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4921 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004922
4923 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4924 hs->buffering.total_bytes_buffered ) )
4925 {
4926 if( recv_msg_seq_offset > 0 )
4927 {
4928 /* If we can't buffer a future message because
4929 * of space limitations -- ignore. */
4930 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",
4931 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4932 (unsigned) hs->buffering.total_bytes_buffered ) );
4933 goto exit;
4934 }
Hanno Beckere1801392018-08-21 16:51:05 +01004935 else
4936 {
4937 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",
4938 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4939 (unsigned) hs->buffering.total_bytes_buffered ) );
4940 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004941
Hanno Beckera02b0b42018-08-21 17:20:27 +01004942 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004943 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004944 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",
4945 (unsigned) msg_len,
4946 (unsigned) reassembly_buf_sz,
4947 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004948 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004949 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4950 goto exit;
4951 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004952 }
4953
4954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4955 msg_len ) );
4956
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004957 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4958 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004959 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004960 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004961 goto exit;
4962 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004963 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004964
4965 /* Prepare final header: copy msg_type, length and message_seq,
4966 * then add standardised fragment_offset and fragment_length */
4967 memcpy( hs_buf->data, ssl->in_msg, 6 );
4968 memset( hs_buf->data + 6, 0, 3 );
4969 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4970
4971 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004972
4973 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004974 }
4975 else
4976 {
4977 /* Make sure msg_type and length are consistent */
4978 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4979 {
4980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4981 /* Ignore */
4982 goto exit;
4983 }
4984 }
4985
Hanno Becker4422bbb2018-08-20 09:40:19 +01004986 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004987 {
4988 size_t frag_len, frag_off;
4989 unsigned char * const msg = hs_buf->data + 12;
4990
4991 /*
4992 * Check and copy current fragment
4993 */
4994
4995 /* Validation of header fields already done in
4996 * mbedtls_ssl_prepare_handshake_record(). */
4997 frag_off = ssl_get_hs_frag_off( ssl );
4998 frag_len = ssl_get_hs_frag_len( ssl );
4999
5000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5001 frag_off, frag_len ) );
5002 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5003
5004 if( hs_buf->is_fragmented )
5005 {
5006 unsigned char * const bitmask = msg + msg_len;
5007 ssl_bitmask_set( bitmask, frag_off, frag_len );
5008 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5009 msg_len ) == 0 );
5010 }
5011 else
5012 {
5013 hs_buf->is_complete = 1;
5014 }
5015
5016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5017 hs_buf->is_complete ? "" : "not yet " ) );
5018 }
5019
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005020 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005021 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005022
5023 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005024 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005025 break;
5026 }
5027
5028exit:
5029
5030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5031 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005032}
5033#endif /* MBEDTLS_SSL_PROTO_DTLS */
5034
Hanno Becker1097b342018-08-15 14:09:41 +01005035static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005036{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005037 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005038 * Consume last content-layer message and potentially
5039 * update in_msglen which keeps track of the contents'
5040 * consumption state.
5041 *
5042 * (1) Handshake messages:
5043 * Remove last handshake message, move content
5044 * and adapt in_msglen.
5045 *
5046 * (2) Alert messages:
5047 * Consume whole record content, in_msglen = 0.
5048 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005049 * (3) Change cipher spec:
5050 * Consume whole record content, in_msglen = 0.
5051 *
5052 * (4) Application data:
5053 * Don't do anything - the record layer provides
5054 * the application data as a stream transport
5055 * and consumes through mbedtls_ssl_read only.
5056 *
5057 */
5058
5059 /* Case (1): Handshake messages */
5060 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005061 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005062 /* Hard assertion to be sure that no application data
5063 * is in flight, as corrupting ssl->in_msglen during
5064 * ssl->in_offt != NULL is fatal. */
5065 if( ssl->in_offt != NULL )
5066 {
5067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5069 }
5070
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005071 /*
5072 * Get next Handshake message in the current record
5073 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005074
Hanno Becker4a810fb2017-05-24 16:27:30 +01005075 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005076 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005077 * current handshake content: If DTLS handshake
5078 * fragmentation is used, that's the fragment
5079 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005080 * size here is faulty and should be changed at
5081 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005082 * (2) While it doesn't seem to cause problems, one
5083 * has to be very careful not to assume that in_hslen
5084 * is always <= in_msglen in a sensible communication.
5085 * Again, it's wrong for DTLS handshake fragmentation.
5086 * The following check is therefore mandatory, and
5087 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005088 * Additionally, ssl->in_hslen might be arbitrarily out of
5089 * bounds after handling a DTLS message with an unexpected
5090 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005091 */
5092 if( ssl->in_hslen < ssl->in_msglen )
5093 {
5094 ssl->in_msglen -= ssl->in_hslen;
5095 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5096 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005097
Hanno Becker4a810fb2017-05-24 16:27:30 +01005098 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5099 ssl->in_msg, ssl->in_msglen );
5100 }
5101 else
5102 {
5103 ssl->in_msglen = 0;
5104 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005105
Hanno Becker4a810fb2017-05-24 16:27:30 +01005106 ssl->in_hslen = 0;
5107 }
5108 /* Case (4): Application data */
5109 else if( ssl->in_offt != NULL )
5110 {
5111 return( 0 );
5112 }
5113 /* Everything else (CCS & Alerts) */
5114 else
5115 {
5116 ssl->in_msglen = 0;
5117 }
5118
Hanno Becker1097b342018-08-15 14:09:41 +01005119 return( 0 );
5120}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005121
Hanno Beckere74d5562018-08-15 14:26:08 +01005122static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5123{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005124 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005125 return( 1 );
5126
5127 return( 0 );
5128}
5129
Hanno Becker5f066e72018-08-16 14:56:31 +01005130#if defined(MBEDTLS_SSL_PROTO_DTLS)
5131
5132static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5133{
5134 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5135 if( hs == NULL )
5136 return;
5137
Hanno Becker01315ea2018-08-21 17:22:17 +01005138 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005139 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005140 hs->buffering.total_bytes_buffered -=
5141 hs->buffering.future_record.len;
5142
5143 mbedtls_free( hs->buffering.future_record.data );
5144 hs->buffering.future_record.data = NULL;
5145 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005146}
5147
5148static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5149{
5150 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5151 unsigned char * rec;
5152 size_t rec_len;
5153 unsigned rec_epoch;
5154
5155 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5156 return( 0 );
5157
5158 if( hs == NULL )
5159 return( 0 );
5160
Hanno Becker5f066e72018-08-16 14:56:31 +01005161 rec = hs->buffering.future_record.data;
5162 rec_len = hs->buffering.future_record.len;
5163 rec_epoch = hs->buffering.future_record.epoch;
5164
5165 if( rec == NULL )
5166 return( 0 );
5167
Hanno Becker4cb782d2018-08-20 11:19:05 +01005168 /* Only consider loading future records if the
5169 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005170 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005171 return( 0 );
5172
Hanno Becker5f066e72018-08-16 14:56:31 +01005173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5174
5175 if( rec_epoch != ssl->in_epoch )
5176 {
5177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5178 goto exit;
5179 }
5180
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5182
5183 /* Double-check that the record is not too large */
5184 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5185 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5186 {
5187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5189 }
5190
5191 memcpy( ssl->in_hdr, rec, rec_len );
5192 ssl->in_left = rec_len;
5193 ssl->next_record_offset = 0;
5194
5195 ssl_free_buffered_record( ssl );
5196
5197exit:
5198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5199 return( 0 );
5200}
5201
5202static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5203{
5204 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5205 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005206 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005207
5208 /* Don't buffer future records outside handshakes. */
5209 if( hs == NULL )
5210 return( 0 );
5211
5212 /* Only buffer handshake records (we are only interested
5213 * in Finished messages). */
5214 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5215 return( 0 );
5216
5217 /* Don't buffer more than one future epoch record. */
5218 if( hs->buffering.future_record.data != NULL )
5219 return( 0 );
5220
Hanno Becker01315ea2018-08-21 17:22:17 +01005221 /* Don't buffer record if there's not enough buffering space remaining. */
5222 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5223 hs->buffering.total_bytes_buffered ) )
5224 {
5225 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",
5226 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5227 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005228 return( 0 );
5229 }
5230
Hanno Becker5f066e72018-08-16 14:56:31 +01005231 /* Buffer record */
5232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5233 ssl->in_epoch + 1 ) );
5234 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5235 rec_hdr_len + ssl->in_msglen );
5236
5237 /* ssl_parse_record_header() only considers records
5238 * of the next epoch as candidates for buffering. */
5239 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005240 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005241
5242 hs->buffering.future_record.data =
5243 mbedtls_calloc( 1, hs->buffering.future_record.len );
5244 if( hs->buffering.future_record.data == NULL )
5245 {
5246 /* If we run out of RAM trying to buffer a
5247 * record from the next epoch, just ignore. */
5248 return( 0 );
5249 }
5250
Hanno Becker01315ea2018-08-21 17:22:17 +01005251 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005252
Hanno Becker01315ea2018-08-21 17:22:17 +01005253 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005254 return( 0 );
5255}
5256
5257#endif /* MBEDTLS_SSL_PROTO_DTLS */
5258
Hanno Beckere74d5562018-08-15 14:26:08 +01005259static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005260{
5261 int ret;
5262
Hanno Becker5f066e72018-08-16 14:56:31 +01005263#if defined(MBEDTLS_SSL_PROTO_DTLS)
5264 /* We might have buffered a future record; if so,
5265 * and if the epoch matches now, load it.
5266 * On success, this call will set ssl->in_left to
5267 * the length of the buffered record, so that
5268 * the calls to ssl_fetch_input() below will
5269 * essentially be no-ops. */
5270 ret = ssl_load_buffered_record( ssl );
5271 if( ret != 0 )
5272 return( ret );
5273#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005278 return( ret );
5279 }
5280
5281 if( ( ret = ssl_parse_record_header( 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é-Gonnardbe619c12015-09-08 11:21:21 +02005284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5285 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005286 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005287 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5288 {
5289 ret = ssl_buffer_future_record( ssl );
5290 if( ret != 0 )
5291 return( ret );
5292
5293 /* Fall through to handling of unexpected records */
5294 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5295 }
5296
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005297 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5298 {
5299 /* Skip unexpected record (but not whole datagram) */
5300 ssl->next_record_offset = ssl->in_msglen
5301 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005302
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5304 "(header)" ) );
5305 }
5306 else
5307 {
5308 /* Skip invalid record and the rest of the datagram */
5309 ssl->next_record_offset = 0;
5310 ssl->in_left = 0;
5311
5312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5313 "(header)" ) );
5314 }
5315
5316 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005317 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005318 }
5319#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005320 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005321 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005322
5323 /*
5324 * Read and optionally decrypt the message contents
5325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5327 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005330 return( ret );
5331 }
5332
5333 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005335 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005338 if( ssl->next_record_offset < ssl->in_left )
5339 {
5340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5341 }
5342 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005343 else
5344#endif
5345 ssl->in_left = 0;
5346
5347 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005351 {
5352 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5354 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005355 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005356 /* Except when waiting for Finished as a bad mac here
5357 * probably means something went wrong in the handshake
5358 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5359 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5360 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5361 {
5362#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5363 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5364 {
5365 mbedtls_ssl_send_alert_message( ssl,
5366 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5367 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5368 }
5369#endif
5370 return( ret );
5371 }
5372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005374 if( ssl->conf->badmac_limit != 0 &&
5375 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5378 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005379 }
5380#endif
5381
Hanno Becker4a810fb2017-05-24 16:27:30 +01005382 /* As above, invalid records cause
5383 * dismissal of the whole datagram. */
5384
5385 ssl->next_record_offset = 0;
5386 ssl->in_left = 0;
5387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005389 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005390 }
5391
5392 return( ret );
5393 }
5394 else
5395#endif
5396 {
5397 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5399 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 mbedtls_ssl_send_alert_message( ssl,
5402 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5403 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005404 }
5405#endif
5406 return( ret );
5407 }
5408 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005409
Simon Butcher99000142016-10-13 17:21:01 +01005410 return( 0 );
5411}
5412
5413int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5414{
5415 int ret;
5416
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005417 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005418 * Handle particular types of records
5419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 {
Simon Butcher99000142016-10-13 17:21:01 +01005422 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5423 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005424 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
5427
Hanno Beckere678eaa2018-08-21 14:57:46 +01005428 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005429 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005430 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005431 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5433 ssl->in_msglen ) );
5434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005435 }
5436
Hanno Beckere678eaa2018-08-21 14:57:46 +01005437 if( ssl->in_msg[0] != 1 )
5438 {
5439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5440 ssl->in_msg[0] ) );
5441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5442 }
5443
5444#if defined(MBEDTLS_SSL_PROTO_DTLS)
5445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5446 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5447 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5448 {
5449 if( ssl->handshake == NULL )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5452 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5453 }
5454
5455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5456 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5457 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005458#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005459 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005463 if( ssl->in_msglen != 2 )
5464 {
5465 /* Note: Standard allows for more than one 2 byte alert
5466 to be packed in a single message, but Mbed TLS doesn't
5467 currently support this. */
5468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5469 ssl->in_msglen ) );
5470 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5471 }
5472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 ssl->in_msg[0], ssl->in_msg[1] ) );
5475
5476 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005477 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005482 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 }
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5487 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5490 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005492
5493#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5494 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5495 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5496 {
Hanno Becker90333da2017-10-10 11:27:13 +01005497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005498 /* Will be handled when trying to parse ServerHello */
5499 return( 0 );
5500 }
5501#endif
5502
5503#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5504 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5505 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5506 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5507 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5510 /* Will be handled in mbedtls_ssl_parse_certificate() */
5511 return( 0 );
5512 }
5513#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5514
5515 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005516 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 }
5518
Hanno Beckerc76c6192017-06-06 10:03:17 +01005519#if defined(MBEDTLS_SSL_PROTO_DTLS)
5520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5521 ssl->handshake != NULL &&
5522 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5523 {
5524 ssl_handshake_wrapup_free_hs_transform( ssl );
5525 }
5526#endif
5527
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 return( 0 );
5529}
5530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005532{
5533 int ret;
5534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5537 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005538 {
5539 return( ret );
5540 }
5541
5542 return( 0 );
5543}
5544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005546 unsigned char level,
5547 unsigned char message )
5548{
5549 int ret;
5550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005551 if( ssl == NULL || ssl->conf == NULL )
5552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005558 ssl->out_msglen = 2;
5559 ssl->out_msg[0] = level;
5560 ssl->out_msg[1] = message;
5561
Hanno Becker67bc7c32018-08-06 11:33:50 +01005562 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005565 return( ret );
5566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005568
5569 return( 0 );
5570}
5571
Paul Bakker5121ce52009-01-03 21:22:43 +00005572/*
5573 * Handshake functions
5574 */
Hanno Becker21489932019-02-05 13:20:55 +00005575#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005576/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005582
Hanno Becker7177a882019-02-05 13:36:46 +00005583 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005586 ssl->state++;
5587 return( 0 );
5588 }
5589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005592}
5593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005599
Hanno Becker7177a882019-02-05 13:36:46 +00005600 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005603 ssl->state++;
5604 return( 0 );
5605 }
5606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005609}
Gilles Peskinef9828522017-05-03 12:28:43 +02005610
Hanno Becker21489932019-02-05 13:20:55 +00005611#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005612/* Some certificate support -> implement write and parse */
5613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005617 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 const mbedtls_x509_crt *crt;
5619 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005622
Hanno Becker7177a882019-02-05 13:36:46 +00005623 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005626 ssl->state++;
5627 return( 0 );
5628 }
5629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 {
5633 if( ssl->client_auth == 0 )
5634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 ssl->state++;
5637 return( 0 );
5638 }
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 /*
5642 * If using SSLv3 and got no cert, send an Alert message
5643 * (otherwise an empty Certificate message will be sent).
5644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5646 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 {
5648 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5650 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5651 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 goto write_msg;
5655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#endif /* MBEDTLS_SSL_CLI_C */
5659#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5665 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 }
5667 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
5672 /*
5673 * 0 . 0 handshake type
5674 * 1 . 3 handshake length
5675 * 4 . 6 length of all certs
5676 * 7 . 9 length of cert. 1
5677 * 10 . n-1 peer certificate
5678 * n . n+2 length of cert. 2
5679 * n+3 . ... upper level cert, etc.
5680 */
5681 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683
Paul Bakker29087132010-03-21 21:03:34 +00005684 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 {
5686 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005687 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005690 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 }
5693
5694 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5695 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5696 ssl->out_msg[i + 2] = (unsigned char)( n );
5697
5698 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5699 i += n; crt = crt->next;
5700 }
5701
5702 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5703 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5704 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5705
5706 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5708 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005709
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005710#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005711write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005712#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005713
5714 ssl->state++;
5715
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005716 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 return( ret );
5720 }
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723
Paul Bakkered27a042013-04-18 22:46:23 +02005724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005725}
5726
Hanno Becker84879e32019-01-31 07:44:03 +00005727#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005728
5729#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005730static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5731 unsigned char *crt_buf,
5732 size_t crt_buf_len )
5733{
5734 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5735
5736 if( peer_crt == NULL )
5737 return( -1 );
5738
5739 if( peer_crt->raw.len != crt_buf_len )
5740 return( -1 );
5741
Hanno Becker46f34d02019-02-08 14:00:04 +00005742 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005743}
Hanno Becker177475a2019-02-05 17:02:46 +00005744#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5745static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5746 unsigned char *crt_buf,
5747 size_t crt_buf_len )
5748{
5749 int ret;
5750 unsigned char const * const peer_cert_digest =
5751 ssl->session->peer_cert_digest;
5752 mbedtls_md_type_t const peer_cert_digest_type =
5753 ssl->session->peer_cert_digest_type;
5754 mbedtls_md_info_t const * const digest_info =
5755 mbedtls_md_info_from_type( peer_cert_digest_type );
5756 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5757 size_t digest_len;
5758
5759 if( peer_cert_digest == NULL || digest_info == NULL )
5760 return( -1 );
5761
5762 digest_len = mbedtls_md_get_size( digest_info );
5763 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5764 return( -1 );
5765
5766 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5767 if( ret != 0 )
5768 return( -1 );
5769
5770 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5771}
5772#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005773#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005774
Hanno Becker1294a0b2019-02-05 12:38:15 +00005775static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5776{
5777 if( session->peer_cert != NULL )
5778 {
5779 mbedtls_x509_crt_free( session->peer_cert );
5780 mbedtls_free( session->peer_cert );
5781 session->peer_cert = NULL;
5782 }
Hanno Becker9198ad12019-02-05 17:00:50 +00005783
5784#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5785 if( session->peer_cert_digest != NULL )
5786 {
5787 /* Zeroization is not necessary. */
5788 mbedtls_free( session->peer_cert_digest );
5789 session->peer_cert_digest = NULL;
5790 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5791 session->peer_cert_digest_len = 0;
5792 }
5793#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker1294a0b2019-02-05 12:38:15 +00005794}
5795
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005796/*
5797 * Once the certificate message is read, parse it into a cert chain and
5798 * perform basic checks, but leave actual verification to the caller
5799 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005800static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5801 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005802{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005803 int ret;
5804#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5805 int crt_cnt=0;
5806#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005807 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005808 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5814 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 }
5817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5819 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005822 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005828
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005832 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005833
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005834 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5839 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841 }
5842
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005843 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5844 i += 3;
5845
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005846 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 while( i < ssl->in_hslen )
5848 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005849 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005850 if ( i + 3 > ssl->in_hslen ) {
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005852 mbedtls_ssl_send_alert_message( ssl,
5853 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5854 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005855 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5856 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005857 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5858 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 if( ssl->in_msg[i] != 0 )
5860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005862 mbedtls_ssl_send_alert_message( ssl,
5863 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5864 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 }
5867
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005868 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5870 | (unsigned int) ssl->in_msg[i + 2];
5871 i += 3;
5872
5873 if( n < 128 || i + n > ssl->in_hslen )
5874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005876 mbedtls_ssl_send_alert_message( ssl,
5877 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5878 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880 }
5881
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005882 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005883#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5884 if( crt_cnt++ == 0 &&
5885 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5886 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005887 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005888 /* During client-side renegotiation, check that the server's
5889 * end-CRTs hasn't changed compared to the initial handshake,
5890 * mitigating the triple handshake attack. On success, reuse
5891 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5893 if( ssl_check_peer_crt_unchanged( ssl,
5894 &ssl->in_msg[i],
5895 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005896 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5898 mbedtls_ssl_send_alert_message( ssl,
5899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5900 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5901 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005902 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005903
5904 /* Now we can safely free the original chain. */
5905 ssl_clear_peer_cert( ssl->session );
5906 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005907#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5908
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005909 /* Parse the next certificate in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005910 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005911 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005912 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005913 case 0: /*ok*/
5914 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5915 /* Ignore certificate with an unknown algorithm: maybe a
5916 prior certificate was already trusted. */
5917 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005918
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005919 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5920 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5921 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005922
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005923 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5924 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5925 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005926
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005927 default:
5928 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5929 crt_parse_der_failed:
5930 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5931 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5932 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 }
5934
5935 i += n;
5936 }
5937
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005938 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005939 return( 0 );
5940}
5941
Hanno Becker4a55f632019-02-05 12:49:06 +00005942#if defined(MBEDTLS_SSL_SRV_C)
5943static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5944{
5945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5946 return( -1 );
5947
5948#if defined(MBEDTLS_SSL_PROTO_SSL3)
5949 /*
5950 * Check if the client sent an empty certificate
5951 */
5952 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5953 {
5954 if( ssl->in_msglen == 2 &&
5955 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5956 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5957 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5958 {
5959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5960 return( 0 );
5961 }
5962
5963 return( -1 );
5964 }
5965#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5966
5967#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5968 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5969 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5970 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5971 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5972 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5973 {
5974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5975 return( 0 );
5976 }
5977
5978 return( -1 );
5979#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5980 MBEDTLS_SSL_PROTO_TLS1_2 */
5981}
5982#endif /* MBEDTLS_SSL_SRV_C */
5983
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005984/* Check if a certificate message is expected.
5985 * Return either
5986 * - SSL_CERTIFICATE_EXPECTED, or
5987 * - SSL_CERTIFICATE_SKIP
5988 * indicating whether a Certificate message is expected or not.
5989 */
5990#define SSL_CERTIFICATE_EXPECTED 0
5991#define SSL_CERTIFICATE_SKIP 1
5992static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5993 int authmode )
5994{
5995 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5996 ssl->transform_negotiate->ciphersuite_info;
5997
5998 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5999 return( SSL_CERTIFICATE_SKIP );
6000
6001#if defined(MBEDTLS_SSL_SRV_C)
6002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6003 {
6004 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6005 return( SSL_CERTIFICATE_SKIP );
6006
6007 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6008 {
6009 /* NOTE: Is it intentional that we set verify_result
6010 * to SKIP_VERIFY on server-side only? */
6011 ssl->session_negotiate->verify_result =
6012 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6013 return( SSL_CERTIFICATE_SKIP );
6014 }
6015 }
6016#endif /* MBEDTLS_SSL_SRV_C */
6017
6018 return( SSL_CERTIFICATE_EXPECTED );
6019}
6020
Hanno Becker68636192019-02-05 14:36:34 +00006021static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6022 int authmode,
6023 mbedtls_x509_crt *chain,
6024 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006025{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006026 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006027 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6028 ssl->transform_negotiate->ciphersuite_info;
Hanno Becker68636192019-02-05 14:36:34 +00006029 mbedtls_x509_crt *ca_chain;
6030 mbedtls_x509_crl *ca_crl;
6031
6032 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6033 return( 0 );
6034
6035#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6036 if( ssl->handshake->sni_ca_chain != NULL )
6037 {
6038 ca_chain = ssl->handshake->sni_ca_chain;
6039 ca_crl = ssl->handshake->sni_ca_crl;
6040 }
6041 else
6042#endif
6043 {
6044 ca_chain = ssl->conf->ca_chain;
6045 ca_crl = ssl->conf->ca_crl;
6046 }
6047
6048 /*
6049 * Main check: verify certificate
6050 */
6051 ret = mbedtls_x509_crt_verify_restartable(
6052 chain,
6053 ca_chain, ca_crl,
6054 ssl->conf->cert_profile,
6055 ssl->hostname,
6056 &ssl->session_negotiate->verify_result,
6057 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6058
6059 if( ret != 0 )
6060 {
6061 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6062 }
6063
6064#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6065 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6066 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6067#endif
6068
6069 /*
6070 * Secondary checks: always done, but change 'ret' only if it was 0
6071 */
6072
6073#if defined(MBEDTLS_ECP_C)
6074 {
6075 const mbedtls_pk_context *pk = &chain->pk;
6076
6077 /* If certificate uses an EC key, make sure the curve is OK */
6078 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6079 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6080 {
6081 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6082
6083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6084 if( ret == 0 )
6085 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6086 }
6087 }
6088#endif /* MBEDTLS_ECP_C */
6089
6090 if( mbedtls_ssl_check_cert_usage( chain,
6091 ciphersuite_info,
6092 ! ssl->conf->endpoint,
6093 &ssl->session_negotiate->verify_result ) != 0 )
6094 {
6095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6096 if( ret == 0 )
6097 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6098 }
6099
6100 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6101 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6102 * with details encoded in the verification flags. All other kinds
6103 * of error codes, including those from the user provided f_vrfy
6104 * functions, are treated as fatal and lead to a failure of
6105 * ssl_parse_certificate even if verification was optional. */
6106 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6107 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6108 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6109 {
6110 ret = 0;
6111 }
6112
6113 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6114 {
6115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6116 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6117 }
6118
6119 if( ret != 0 )
6120 {
6121 uint8_t alert;
6122
6123 /* The certificate may have been rejected for several reasons.
6124 Pick one and send the corresponding alert. Which alert to send
6125 may be a subject of debate in some cases. */
6126 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6127 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6128 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6129 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6130 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6131 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6132 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6133 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6134 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6135 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6136 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6137 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6138 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6139 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6140 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6141 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6142 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6143 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6144 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6145 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6146 else
6147 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6148 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6149 alert );
6150 }
6151
6152#if defined(MBEDTLS_DEBUG_C)
6153 if( ssl->session_negotiate->verify_result != 0 )
6154 {
6155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6156 ssl->session_negotiate->verify_result ) );
6157 }
6158 else
6159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6161 }
6162#endif /* MBEDTLS_DEBUG_C */
6163
6164 return( ret );
6165}
6166
6167int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6168{
6169 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006170 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006171#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6172 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6173 ? ssl->handshake->sni_authmode
6174 : ssl->conf->authmode;
6175#else
6176 const int authmode = ssl->conf->authmode;
6177#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006178 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006179
6180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6181
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006182 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6183 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006184 {
6185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006186 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006187 }
6188
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006189#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6190 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006191 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006192 {
6193 goto crt_verify;
6194 }
6195#endif
6196
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006197 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006198 {
6199 /* mbedtls_ssl_read_record may have sent an alert already. We
6200 let it decide whether to alert. */
6201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6202 return( ret );
6203 }
6204
Hanno Becker4a55f632019-02-05 12:49:06 +00006205#if defined(MBEDTLS_SSL_SRV_C)
6206 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6207 {
6208 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006209
6210 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006211 ret = 0;
6212 else
6213 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006214
Hanno Becker6bdfab22019-02-05 13:11:17 +00006215 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006216 }
6217#endif /* MBEDTLS_SSL_SRV_C */
6218
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006219 /* Clear existing peer CRT structure in case we tried to
6220 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006221 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006222 ssl->session_negotiate->peer_cert =
6223 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6224 if( ssl->session_negotiate->peer_cert == NULL )
6225 {
6226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6227 sizeof( mbedtls_x509_crt ) ) );
6228 mbedtls_ssl_send_alert_message( ssl,
6229 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6230 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6231 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6232 }
6233 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Hanno Becker7a955a02019-02-05 13:08:01 +00006234
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006235 ret = ssl_parse_certificate_chain( ssl, ssl->session_negotiate->peer_cert );
6236 if( ret != 0 )
Hanno Beckerfcd9e712019-02-05 14:35:46 +00006237 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006238
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006239#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6240 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006241 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006242
6243crt_verify:
6244 if( ssl->handshake->ecrs_enabled)
6245 rs_ctx = &ssl->handshake->ecrs_ctx;
6246#endif
6247
Hanno Becker68636192019-02-05 14:36:34 +00006248 ret = ssl_parse_certificate_verify( ssl, authmode,
6249 ssl->session_negotiate->peer_cert,
6250 rs_ctx );
6251 if( ret != 0 )
6252 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006253
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006254#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6255 /* Remember digest of the peer's end-CRT. */
6256 ssl->session_negotiate->peer_cert_digest =
6257 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6258 if( ssl->session_negotiate->peer_cert_digest == NULL )
6259 {
6260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6261 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6262 mbedtls_ssl_send_alert_message( ssl,
6263 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6264 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6265 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6266 }
6267 ret = mbedtls_md( mbedtls_md_info_from_type(
6268 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6269 ssl->session_negotiate->peer_cert->raw.p,
6270 ssl->session_negotiate->peer_cert->raw.len,
6271 ssl->session_negotiate->peer_cert_digest );
6272 if( ret != 0 )
6273 return( ret );
6274
6275 ssl->session_negotiate->peer_cert_digest_type =
6276 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6277 ssl->session_negotiate->peer_cert_digest_len =
6278 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6279#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006282
Hanno Becker6bdfab22019-02-05 13:11:17 +00006283exit:
6284
6285 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 return( ret );
6287}
Hanno Becker21489932019-02-05 13:20:55 +00006288#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006291{
6292 int ret;
6293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006297 ssl->out_msglen = 1;
6298 ssl->out_msg[0] = 1;
6299
Paul Bakker5121ce52009-01-03 21:22:43 +00006300 ssl->state++;
6301
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006302 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006303 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006305 return( ret );
6306 }
6307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006309
6310 return( 0 );
6311}
6312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006314{
6315 int ret;
6316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006318
Hanno Becker327c93b2018-08-15 13:56:18 +01006319 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006322 return( ret );
6323 }
6324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006328 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6329 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 }
6332
Hanno Beckere678eaa2018-08-21 14:57:46 +01006333 /* CCS records are only accepted if they have length 1 and content '1',
6334 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006335
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006336 /*
6337 * Switch to our negotiated transform and session parameters for inbound
6338 * data.
6339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006341 ssl->transform_in = ssl->transform_negotiate;
6342 ssl->session_in = ssl->session_negotiate;
6343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006345 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006348 ssl_dtls_replay_reset( ssl );
6349#endif
6350
6351 /* Increment epoch */
6352 if( ++ssl->in_epoch == 0 )
6353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006355 /* This is highly unlikely to happen for legitimate reasons, so
6356 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006358 }
6359 }
6360 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006362 memset( ssl->in_ctr, 0, 8 );
6363
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006364 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6367 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006372 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6373 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006375 }
6376 }
6377#endif
6378
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 ssl->state++;
6380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006382
6383 return( 0 );
6384}
6385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6387 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006388{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006389 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6392 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6393 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006394 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006395 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6398#if defined(MBEDTLS_SHA512_C)
6399 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006400 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6401 else
6402#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403#if defined(MBEDTLS_SHA256_C)
6404 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006405 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006406 else
6407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006411 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006412 }
Paul Bakker380da532012-04-18 16:10:25 +00006413}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6418 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006419 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6420 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6423#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006424#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006425 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006426 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6427#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006428 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006429#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006432#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006433 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006434 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006435#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006436 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006437#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006440}
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006443 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6446 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006447 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6448 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006449#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6451#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006452#if defined(MBEDTLS_USE_PSA_CRYPTO)
6453 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6454#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006455 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006456#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006457#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006459#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006460 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006461#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006462 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006463#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006466}
6467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6469 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6470static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006471 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006472{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006473 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6474 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006475}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006476#endif
Paul Bakker380da532012-04-18 16:10:25 +00006477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6479#if defined(MBEDTLS_SHA256_C)
6480static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006481 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006482{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006483#if defined(MBEDTLS_USE_PSA_CRYPTO)
6484 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6485#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006486 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006487#endif
Paul Bakker380da532012-04-18 16:10:25 +00006488}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006489#endif
Paul Bakker380da532012-04-18 16:10:25 +00006490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491#if defined(MBEDTLS_SHA512_C)
6492static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006493 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006494{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006495#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006496 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006497#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006498 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006499#endif
Paul Bakker380da532012-04-18 16:10:25 +00006500}
Paul Bakker769075d2012-11-24 11:26:46 +01006501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006505static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006507{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006508 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006509 mbedtls_md5_context md5;
6510 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006511
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 unsigned char padbuf[48];
6513 unsigned char md5sum[16];
6514 unsigned char sha1sum[20];
6515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006517 if( !session )
6518 session = ssl->session;
6519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006521
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006522 mbedtls_md5_init( &md5 );
6523 mbedtls_sha1_init( &sha1 );
6524
6525 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6526 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006527
6528 /*
6529 * SSLv3:
6530 * hash =
6531 * MD5( master + pad2 +
6532 * MD5( handshake + sender + master + pad1 ) )
6533 * + SHA1( master + pad2 +
6534 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 */
6536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006538 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6539 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006543 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6544 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006545#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006548 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006549
Paul Bakker1ef83d62012-04-11 12:09:53 +00006550 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006552 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6553 mbedtls_md5_update_ret( &md5, session->master, 48 );
6554 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6555 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006556
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006557 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6558 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6559 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6560 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006561
Paul Bakker1ef83d62012-04-11 12:09:53 +00006562 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006563
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006564 mbedtls_md5_starts_ret( &md5 );
6565 mbedtls_md5_update_ret( &md5, session->master, 48 );
6566 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6567 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6568 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006569
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006570 mbedtls_sha1_starts_ret( &sha1 );
6571 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6572 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6573 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6574 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006577
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006578 mbedtls_md5_free( &md5 );
6579 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006581 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6582 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6583 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006590static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006592{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006593 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006594 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006595 mbedtls_md5_context md5;
6596 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006597 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006600 if( !session )
6601 session = ssl->session;
6602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006604
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006605 mbedtls_md5_init( &md5 );
6606 mbedtls_sha1_init( &sha1 );
6607
6608 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6609 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006610
Paul Bakker1ef83d62012-04-11 12:09:53 +00006611 /*
6612 * TLSv1:
6613 * hash = PRF( master, finished_label,
6614 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6615 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006618 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6619 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006620#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006623 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6624 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006625#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006628 ? "client finished"
6629 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006631 mbedtls_md5_finish_ret( &md5, padbuf );
6632 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006633
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006634 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006635 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006638
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006639 mbedtls_md5_free( &md5 );
6640 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006641
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006642 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6649#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006650static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006652{
6653 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006654 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006655 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006656#if defined(MBEDTLS_USE_PSA_CRYPTO)
6657 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006658 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006659 psa_status_t status;
6660#else
6661 mbedtls_sha256_context sha256;
6662#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006665 if( !session )
6666 session = ssl->session;
6667
Andrzej Kurekeb342242019-01-29 09:14:33 -05006668 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6669 ? "client finished"
6670 : "server finished";
6671
6672#if defined(MBEDTLS_USE_PSA_CRYPTO)
6673 sha256_psa = psa_hash_operation_init();
6674
6675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6676
6677 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6678 if( status != PSA_SUCCESS )
6679 {
6680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6681 return;
6682 }
6683
6684 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6685 if( status != PSA_SUCCESS )
6686 {
6687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6688 return;
6689 }
6690 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6691#else
6692
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006693 mbedtls_sha256_init( &sha256 );
6694
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006696
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006697 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006698
6699 /*
6700 * TLSv1.2:
6701 * hash = PRF( master, finished_label,
6702 * Hash( handshake ) )[0.11]
6703 */
6704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if !defined(MBEDTLS_SHA256_ALT)
6706 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006707 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006708#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006709
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006710 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006711 mbedtls_sha256_free( &sha256 );
6712#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006713
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006714 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006715 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006718
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006719 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006726static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006728{
6729 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006730 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006731 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006732#if defined(MBEDTLS_USE_PSA_CRYPTO)
6733 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006734 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006735 psa_status_t status;
6736#else
6737 mbedtls_sha512_context sha512;
6738#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006741 if( !session )
6742 session = ssl->session;
6743
Andrzej Kurekeb342242019-01-29 09:14:33 -05006744 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6745 ? "client finished"
6746 : "server finished";
6747
6748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006749 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006750
6751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6752
Andrzej Kurek972fba52019-01-30 03:29:12 -05006753 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006754 if( status != PSA_SUCCESS )
6755 {
6756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6757 return;
6758 }
6759
Andrzej Kurek972fba52019-01-30 03:29:12 -05006760 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006761 if( status != PSA_SUCCESS )
6762 {
6763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6764 return;
6765 }
6766 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6767#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006768 mbedtls_sha512_init( &sha512 );
6769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006771
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006772 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006773
6774 /*
6775 * TLSv1.2:
6776 * hash = PRF( master, finished_label,
6777 * Hash( handshake ) )[0.11]
6778 */
6779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006781 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6782 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006783#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006784
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006785 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006786 mbedtls_sha512_free( &sha512 );
6787#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006788
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006789 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006790 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006793
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006794 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798#endif /* MBEDTLS_SHA512_C */
6799#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006802{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006804
6805 /*
6806 * Free our handshake params
6807 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006808 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006810 ssl->handshake = NULL;
6811
6812 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006813 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006814 */
6815 if( ssl->transform )
6816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 mbedtls_ssl_transform_free( ssl->transform );
6818 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006819 }
6820 ssl->transform = ssl->transform_negotiate;
6821 ssl->transform_negotiate = NULL;
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006824}
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006827{
6828 int resume = ssl->handshake->resume;
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832#if defined(MBEDTLS_SSL_RENEGOTIATION)
6833 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006836 ssl->renego_records_seen = 0;
6837 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006838#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006839
6840 /*
6841 * Free the previous session and switch in the current one
6842 */
Paul Bakker0a597072012-09-25 21:55:46 +00006843 if( ssl->session )
6844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006846 /* RFC 7366 3.1: keep the EtM state */
6847 ssl->session_negotiate->encrypt_then_mac =
6848 ssl->session->encrypt_then_mac;
6849#endif
6850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 mbedtls_ssl_session_free( ssl->session );
6852 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006853 }
6854 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006855 ssl->session_negotiate = NULL;
6856
Paul Bakker0a597072012-09-25 21:55:46 +00006857 /*
6858 * Add cache entry
6859 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006860 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006861 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006862 resume == 0 )
6863 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006864 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006866 }
Paul Bakker0a597072012-09-25 21:55:46 +00006867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006870 ssl->handshake->flight != NULL )
6871 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006872 /* Cancel handshake timer */
6873 ssl_set_timer( ssl, 0 );
6874
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006875 /* Keep last flight around in case we need to resend it:
6876 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006878 }
6879 else
6880#endif
6881 ssl_handshake_wrapup_free_hs_transform( ssl );
6882
Paul Bakker48916f92012-09-16 19:57:18 +00006883 ssl->state++;
6884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006886}
6887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006889{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006890 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006893
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006894 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006895
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006896 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006897
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006898 /*
6899 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6900 * may define some other value. Currently (early 2016), no defined
6901 * ciphersuite does this (and this is unlikely to change as activity has
6902 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006907 ssl->verify_data_len = hash_len;
6908 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006909#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006910
Paul Bakker5121ce52009-01-03 21:22:43 +00006911 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6913 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006914
6915 /*
6916 * In case of session resuming, invert the client and server
6917 * ChangeCipherSpec messages order.
6918 */
Paul Bakker0a597072012-09-25 21:55:46 +00006919 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006922 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006926 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006928#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 }
6930 else
6931 ssl->state++;
6932
Paul Bakker48916f92012-09-16 19:57:18 +00006933 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006934 * Switch to our negotiated transform and session parameters for outbound
6935 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006940 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006941 {
6942 unsigned char i;
6943
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006944 /* Remember current epoch settings for resending */
6945 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006946 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006947
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006948 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006949 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006950
6951 /* Increment epoch */
6952 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006953 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006954 break;
6955
6956 /* The loop goes to its end iff the counter is wrapping */
6957 if( i == 0 )
6958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6960 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006961 }
6962 }
6963 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006965 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006967 ssl->transform_out = ssl->transform_negotiate;
6968 ssl->session_out = ssl->session_negotiate;
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6971 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6976 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006977 }
6978 }
6979#endif
6980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006984#endif
6985
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006986 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006987 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006989 return( ret );
6990 }
6991
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006992#if defined(MBEDTLS_SSL_PROTO_DTLS)
6993 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6994 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6995 {
6996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6997 return( ret );
6998 }
6999#endif
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007002
7003 return( 0 );
7004}
7005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007007#define SSL_MAX_HASH_LEN 36
7008#else
7009#define SSL_MAX_HASH_LEN 12
7010#endif
7011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007013{
Paul Bakker23986e52011-04-24 08:57:21 +00007014 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007015 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007016 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007019
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007020 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021
Hanno Becker327c93b2018-08-15 13:56:18 +01007022 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007025 return( ret );
7026 }
7027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007031 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7032 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007034 }
7035
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007036 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037#if defined(MBEDTLS_SSL_PROTO_SSL3)
7038 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007039 hash_len = 36;
7040 else
7041#endif
7042 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7045 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007048 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7049 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051 }
7052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007054 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007057 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7058 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007060 }
7061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007063 ssl->verify_data_len = hash_len;
7064 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007065#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007066
Paul Bakker0a597072012-09-25 21:55:46 +00007067 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007070 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007076#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007077 }
7078 else
7079 ssl->state++;
7080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007084#endif
7085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
7088 return( 0 );
7089}
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7096 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7097 mbedtls_md5_init( &handshake->fin_md5 );
7098 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007099 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7100 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7103#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007104#if defined(MBEDTLS_USE_PSA_CRYPTO)
7105 handshake->fin_sha256_psa = psa_hash_operation_init();
7106 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7107#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007109 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007110#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007113#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007114 handshake->fin_sha384_psa = psa_hash_operation_init();
7115 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007116#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007118 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007119#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007122
7123 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007124
7125#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7126 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7127 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7128#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_DHM_C)
7131 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133#if defined(MBEDTLS_ECDH_C)
7134 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007135#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007136#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007137 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007138#if defined(MBEDTLS_SSL_CLI_C)
7139 handshake->ecjpake_cache = NULL;
7140 handshake->ecjpake_cache_len = 0;
7141#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007142#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007143
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007144#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007145 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007146#endif
7147
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007148#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7149 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7150#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007151}
7152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7158 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 mbedtls_md_init( &transform->md_ctx_enc );
7161 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007162}
7163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007167}
7168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007170{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007171 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007172 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007174 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007176 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007177 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007178
7179 /*
7180 * Either the pointers are now NULL or cleared properly and can be freed.
7181 * Now allocate missing structures.
7182 */
7183 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007184 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007185 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007186 }
Paul Bakker48916f92012-09-16 19:57:18 +00007187
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007188 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007189 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007190 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007191 }
Paul Bakker48916f92012-09-16 19:57:18 +00007192
Paul Bakker82788fb2014-10-20 13:59:19 +02007193 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007194 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007195 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007196 }
Paul Bakker48916f92012-09-16 19:57:18 +00007197
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007198 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007199 if( ssl->handshake == NULL ||
7200 ssl->transform_negotiate == NULL ||
7201 ssl->session_negotiate == NULL )
7202 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 mbedtls_free( ssl->handshake );
7206 mbedtls_free( ssl->transform_negotiate );
7207 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007208
7209 ssl->handshake = NULL;
7210 ssl->transform_negotiate = NULL;
7211 ssl->session_negotiate = NULL;
7212
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007213 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007214 }
7215
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007216 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007218 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007219 ssl_handshake_params_init( ssl->handshake );
7220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7223 {
7224 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007225
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007226 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7227 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7228 else
7229 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007230
7231 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007232 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007233#endif
7234
Paul Bakker48916f92012-09-16 19:57:18 +00007235 return( 0 );
7236}
7237
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007238#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007239/* Dummy cookie callbacks for defaults */
7240static int ssl_cookie_write_dummy( void *ctx,
7241 unsigned char **p, unsigned char *end,
7242 const unsigned char *cli_id, size_t cli_id_len )
7243{
7244 ((void) ctx);
7245 ((void) p);
7246 ((void) end);
7247 ((void) cli_id);
7248 ((void) cli_id_len);
7249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007251}
7252
7253static int ssl_cookie_check_dummy( void *ctx,
7254 const unsigned char *cookie, size_t cookie_len,
7255 const unsigned char *cli_id, size_t cli_id_len )
7256{
7257 ((void) ctx);
7258 ((void) cookie);
7259 ((void) cookie_len);
7260 ((void) cli_id);
7261 ((void) cli_id_len);
7262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007264}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007265#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007266
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007267/* Once ssl->out_hdr as the address of the beginning of the
7268 * next outgoing record is set, deduce the other pointers.
7269 *
7270 * Note: For TLS, we save the implicit record sequence number
7271 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7272 * and the caller has to make sure there's space for this.
7273 */
7274
7275static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7276 mbedtls_ssl_transform *transform )
7277{
7278#if defined(MBEDTLS_SSL_PROTO_DTLS)
7279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7280 {
7281 ssl->out_ctr = ssl->out_hdr + 3;
7282 ssl->out_len = ssl->out_hdr + 11;
7283 ssl->out_iv = ssl->out_hdr + 13;
7284 }
7285 else
7286#endif
7287 {
7288 ssl->out_ctr = ssl->out_hdr - 8;
7289 ssl->out_len = ssl->out_hdr + 3;
7290 ssl->out_iv = ssl->out_hdr + 5;
7291 }
7292
7293 /* Adjust out_msg to make space for explicit IV, if used. */
7294 if( transform != NULL &&
7295 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7296 {
7297 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7298 }
7299 else
7300 ssl->out_msg = ssl->out_iv;
7301}
7302
7303/* Once ssl->in_hdr as the address of the beginning of the
7304 * next incoming record is set, deduce the other pointers.
7305 *
7306 * Note: For TLS, we save the implicit record sequence number
7307 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7308 * and the caller has to make sure there's space for this.
7309 */
7310
7311static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7312 mbedtls_ssl_transform *transform )
7313{
7314#if defined(MBEDTLS_SSL_PROTO_DTLS)
7315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7316 {
7317 ssl->in_ctr = ssl->in_hdr + 3;
7318 ssl->in_len = ssl->in_hdr + 11;
7319 ssl->in_iv = ssl->in_hdr + 13;
7320 }
7321 else
7322#endif
7323 {
7324 ssl->in_ctr = ssl->in_hdr - 8;
7325 ssl->in_len = ssl->in_hdr + 3;
7326 ssl->in_iv = ssl->in_hdr + 5;
7327 }
7328
7329 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7330 if( transform != NULL &&
7331 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7332 {
7333 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7334 }
7335 else
7336 ssl->in_msg = ssl->in_iv;
7337}
7338
Paul Bakker5121ce52009-01-03 21:22:43 +00007339/*
7340 * Initialize an SSL context
7341 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007342void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7343{
7344 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7345}
7346
7347/*
7348 * Setup an SSL context
7349 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007350
7351static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7352{
7353 /* Set the incoming and outgoing record pointers. */
7354#if defined(MBEDTLS_SSL_PROTO_DTLS)
7355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7356 {
7357 ssl->out_hdr = ssl->out_buf;
7358 ssl->in_hdr = ssl->in_buf;
7359 }
7360 else
7361#endif /* MBEDTLS_SSL_PROTO_DTLS */
7362 {
7363 ssl->out_hdr = ssl->out_buf + 8;
7364 ssl->in_hdr = ssl->in_buf + 8;
7365 }
7366
7367 /* Derive other internal pointers. */
7368 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7369 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7370}
7371
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007372int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007373 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007374{
Paul Bakker48916f92012-09-16 19:57:18 +00007375 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007376
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007377 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007378
7379 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007380 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007381 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007382
7383 /* Set to NULL in case of an error condition */
7384 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007385
Angus Grattond8213d02016-05-25 20:56:48 +10007386 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7387 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007388 {
Angus Grattond8213d02016-05-25 20:56:48 +10007389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007390 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007391 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007392 }
7393
7394 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7395 if( ssl->out_buf == NULL )
7396 {
7397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007398 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007399 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007400 }
7401
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007402 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007403
Paul Bakker48916f92012-09-16 19:57:18 +00007404 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007405 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007406
7407 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007408
7409error:
7410 mbedtls_free( ssl->in_buf );
7411 mbedtls_free( ssl->out_buf );
7412
7413 ssl->conf = NULL;
7414
7415 ssl->in_buf = NULL;
7416 ssl->out_buf = NULL;
7417
7418 ssl->in_hdr = NULL;
7419 ssl->in_ctr = NULL;
7420 ssl->in_len = NULL;
7421 ssl->in_iv = NULL;
7422 ssl->in_msg = NULL;
7423
7424 ssl->out_hdr = NULL;
7425 ssl->out_ctr = NULL;
7426 ssl->out_len = NULL;
7427 ssl->out_iv = NULL;
7428 ssl->out_msg = NULL;
7429
k-stachowiak9f7798e2018-07-31 16:52:32 +02007430 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007431}
7432
7433/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007434 * Reset an initialized and used SSL context for re-use while retaining
7435 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007436 *
7437 * If partial is non-zero, keep data in the input buffer and client ID.
7438 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007439 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007440static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007441{
Paul Bakker48916f92012-09-16 19:57:18 +00007442 int ret;
7443
Hanno Becker7e772132018-08-10 12:38:21 +01007444#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7445 !defined(MBEDTLS_SSL_SRV_C)
7446 ((void) partial);
7447#endif
7448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007450
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007451 /* Cancel any possibly running timer */
7452 ssl_set_timer( ssl, 0 );
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_RENEGOTIATION)
7455 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007456 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007457
7458 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7460 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007461#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007463
Paul Bakker7eb013f2011-10-06 12:37:39 +00007464 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007465 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007466
7467 ssl->in_msgtype = 0;
7468 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007470 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007471 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007472#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007474 ssl_dtls_replay_reset( ssl );
7475#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007476
7477 ssl->in_hslen = 0;
7478 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007479
7480 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007481
7482 ssl->out_msgtype = 0;
7483 ssl->out_msglen = 0;
7484 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7486 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007487 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007488#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007489
Hanno Becker19859472018-08-06 09:40:20 +01007490 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7491
Paul Bakker48916f92012-09-16 19:57:18 +00007492 ssl->transform_in = NULL;
7493 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007494
Hanno Becker78640902018-08-13 16:35:15 +01007495 ssl->session_in = NULL;
7496 ssl->session_out = NULL;
7497
Angus Grattond8213d02016-05-25 20:56:48 +10007498 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007499
7500#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007501 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007502#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7503 {
7504 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007505 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007506 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7509 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7512 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7515 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007516 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007517 }
7518#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007519
Paul Bakker48916f92012-09-16 19:57:18 +00007520 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522 mbedtls_ssl_transform_free( ssl->transform );
7523 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007524 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007525 }
Paul Bakker48916f92012-09-16 19:57:18 +00007526
Paul Bakkerc0463502013-02-14 11:19:38 +01007527 if( ssl->session )
7528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 mbedtls_ssl_session_free( ssl->session );
7530 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007531 ssl->session = NULL;
7532 }
7533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007535 ssl->alpn_chosen = NULL;
7536#endif
7537
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007538#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007539#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007540 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007541#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007542 {
7543 mbedtls_free( ssl->cli_id );
7544 ssl->cli_id = NULL;
7545 ssl->cli_id_len = 0;
7546 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007547#endif
7548
Paul Bakker48916f92012-09-16 19:57:18 +00007549 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7550 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007551
7552 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007553}
7554
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007555/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007556 * Reset an initialized and used SSL context for re-use while retaining
7557 * all application-set variables, function pointers and data.
7558 */
7559int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7560{
7561 return( ssl_session_reset_int( ssl, 0 ) );
7562}
7563
7564/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007565 * SSL set accessors
7566 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007567void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007568{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007569 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007570}
7571
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007572void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007574 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007575}
7576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007578void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007579{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007580 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007581}
7582#endif
7583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007585void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007587 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007588}
7589#endif
7590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007592
Hanno Becker1841b0a2018-08-24 11:13:57 +01007593void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7594 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007595{
7596 ssl->disable_datagram_packing = !allow_packing;
7597}
7598
7599void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7600 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007602 conf->hs_timeout_min = min;
7603 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007604}
7605#endif
7606
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007607void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007609 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007610}
7611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007613void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007614 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007615 void *p_vrfy )
7616{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007617 conf->f_vrfy = f_vrfy;
7618 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007619}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007621
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007622void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007623 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007624 void *p_rng )
7625{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007626 conf->f_rng = f_rng;
7627 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007628}
7629
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007630void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007631 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007632 void *p_dbg )
7633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007634 conf->f_dbg = f_dbg;
7635 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007636}
7637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007639 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007640 mbedtls_ssl_send_t *f_send,
7641 mbedtls_ssl_recv_t *f_recv,
7642 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007643{
7644 ssl->p_bio = p_bio;
7645 ssl->f_send = f_send;
7646 ssl->f_recv = f_recv;
7647 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007648}
7649
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007650#if defined(MBEDTLS_SSL_PROTO_DTLS)
7651void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7652{
7653 ssl->mtu = mtu;
7654}
7655#endif
7656
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007657void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007658{
7659 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007660}
7661
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007662void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7663 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007664 mbedtls_ssl_set_timer_t *f_set_timer,
7665 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007666{
7667 ssl->p_timer = p_timer;
7668 ssl->f_set_timer = f_set_timer;
7669 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007670
7671 /* Make sure we start with no timer running */
7672 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007673}
7674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007676void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007677 void *p_cache,
7678 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7679 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007680{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007681 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007682 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007683 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687#if defined(MBEDTLS_SSL_CLI_C)
7688int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007689{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007690 int ret;
7691
7692 if( ssl == NULL ||
7693 session == NULL ||
7694 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007695 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007698 }
7699
Hanno Becker52055ae2019-02-06 14:30:46 +00007700 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7701 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007702 return( ret );
7703
Paul Bakker0a597072012-09-25 21:55:46 +00007704 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007705
7706 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007709
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007710void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007711 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007712{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007713 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7714 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7715 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7716 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007717}
7718
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007719void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007720 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007721 int major, int minor )
7722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007724 return;
7725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007727 return;
7728
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007729 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007730}
7731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007733void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007734 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007735{
7736 conf->cert_profile = profile;
7737}
7738
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007739/* Append a new keycert entry to a (possibly empty) list */
7740static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7741 mbedtls_x509_crt *cert,
7742 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007743{
niisato8ee24222018-06-25 19:05:48 +09007744 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007745
niisato8ee24222018-06-25 19:05:48 +09007746 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7747 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007748 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007749
niisato8ee24222018-06-25 19:05:48 +09007750 new_cert->cert = cert;
7751 new_cert->key = key;
7752 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007753
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007754 /* Update head is the list was null, else add to the end */
7755 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007756 {
niisato8ee24222018-06-25 19:05:48 +09007757 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007758 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007759 else
7760 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007761 mbedtls_ssl_key_cert *cur = *head;
7762 while( cur->next != NULL )
7763 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007764 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007765 }
7766
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007767 return( 0 );
7768}
7769
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007770int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007771 mbedtls_x509_crt *own_cert,
7772 mbedtls_pk_context *pk_key )
7773{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007774 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007775}
7776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007777void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007778 mbedtls_x509_crt *ca_chain,
7779 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007780{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007781 conf->ca_chain = ca_chain;
7782 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007785
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007786#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7787int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7788 mbedtls_x509_crt *own_cert,
7789 mbedtls_pk_context *pk_key )
7790{
7791 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7792 own_cert, pk_key ) );
7793}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007794
7795void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7796 mbedtls_x509_crt *ca_chain,
7797 mbedtls_x509_crl *ca_crl )
7798{
7799 ssl->handshake->sni_ca_chain = ca_chain;
7800 ssl->handshake->sni_ca_crl = ca_crl;
7801}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007802
7803void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7804 int authmode )
7805{
7806 ssl->handshake->sni_authmode = authmode;
7807}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007808#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7809
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007810#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007811/*
7812 * Set EC J-PAKE password for current handshake
7813 */
7814int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7815 const unsigned char *pw,
7816 size_t pw_len )
7817{
7818 mbedtls_ecjpake_role role;
7819
Janos Follath8eb64132016-06-03 15:40:57 +01007820 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7822
7823 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7824 role = MBEDTLS_ECJPAKE_SERVER;
7825 else
7826 role = MBEDTLS_ECJPAKE_CLIENT;
7827
7828 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7829 role,
7830 MBEDTLS_MD_SHA256,
7831 MBEDTLS_ECP_DP_SECP256R1,
7832 pw, pw_len ) );
7833}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007834#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007837
7838static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7839{
7840 /* Remove reference to existing PSK, if any. */
7841#if defined(MBEDTLS_USE_PSA_CRYPTO)
7842 if( conf->psk_opaque != 0 )
7843 {
7844 /* The maintenance of the PSK key slot is the
7845 * user's responsibility. */
7846 conf->psk_opaque = 0;
7847 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007848 /* This and the following branch should never
7849 * be taken simultaenously as we maintain the
7850 * invariant that raw and opaque PSKs are never
7851 * configured simultaneously. As a safeguard,
7852 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007853#endif /* MBEDTLS_USE_PSA_CRYPTO */
7854 if( conf->psk != NULL )
7855 {
7856 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7857
7858 mbedtls_free( conf->psk );
7859 conf->psk = NULL;
7860 conf->psk_len = 0;
7861 }
7862
7863 /* Remove reference to PSK identity, if any. */
7864 if( conf->psk_identity != NULL )
7865 {
7866 mbedtls_free( conf->psk_identity );
7867 conf->psk_identity = NULL;
7868 conf->psk_identity_len = 0;
7869 }
7870}
7871
Hanno Becker7390c712018-11-15 13:33:04 +00007872/* This function assumes that PSK identity in the SSL config is unset.
7873 * It checks that the provided identity is well-formed and attempts
7874 * to make a copy of it in the SSL config.
7875 * On failure, the PSK identity in the config remains unset. */
7876static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7877 unsigned char const *psk_identity,
7878 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007879{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007880 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007881 if( psk_identity == NULL ||
7882 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007883 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007884 {
7885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7886 }
7887
Hanno Becker7390c712018-11-15 13:33:04 +00007888 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7889 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007890 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007891
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007892 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007893 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007894
7895 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007896}
7897
Hanno Becker7390c712018-11-15 13:33:04 +00007898int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7899 const unsigned char *psk, size_t psk_len,
7900 const unsigned char *psk_identity, size_t psk_identity_len )
7901{
7902 int ret;
7903 /* Remove opaque/raw PSK + PSK Identity */
7904 ssl_conf_remove_psk( conf );
7905
7906 /* Check and set raw PSK */
7907 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7909 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7910 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7911 conf->psk_len = psk_len;
7912 memcpy( conf->psk, psk, conf->psk_len );
7913
7914 /* Check and set PSK Identity */
7915 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7916 if( ret != 0 )
7917 ssl_conf_remove_psk( conf );
7918
7919 return( ret );
7920}
7921
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007922static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7923{
7924#if defined(MBEDTLS_USE_PSA_CRYPTO)
7925 if( ssl->handshake->psk_opaque != 0 )
7926 {
7927 ssl->handshake->psk_opaque = 0;
7928 }
7929 else
7930#endif /* MBEDTLS_USE_PSA_CRYPTO */
7931 if( ssl->handshake->psk != NULL )
7932 {
7933 mbedtls_platform_zeroize( ssl->handshake->psk,
7934 ssl->handshake->psk_len );
7935 mbedtls_free( ssl->handshake->psk );
7936 ssl->handshake->psk_len = 0;
7937 }
7938}
7939
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007940int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7941 const unsigned char *psk, size_t psk_len )
7942{
7943 if( psk == NULL || ssl->handshake == NULL )
7944 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7945
7946 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7948
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007949 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007950
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007951 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007952 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007953
7954 ssl->handshake->psk_len = psk_len;
7955 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7956
7957 return( 0 );
7958}
7959
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007960#if defined(MBEDTLS_USE_PSA_CRYPTO)
7961int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007962 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007963 const unsigned char *psk_identity,
7964 size_t psk_identity_len )
7965{
Hanno Becker7390c712018-11-15 13:33:04 +00007966 int ret;
7967 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007968 ssl_conf_remove_psk( conf );
7969
Hanno Becker7390c712018-11-15 13:33:04 +00007970 /* Check and set opaque PSK */
7971 if( psk_slot == 0 )
7972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007973 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007974
7975 /* Check and set PSK Identity */
7976 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7977 psk_identity_len );
7978 if( ret != 0 )
7979 ssl_conf_remove_psk( conf );
7980
7981 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007982}
7983
7984int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007985 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007986{
7987 if( psk_slot == 0 || ssl->handshake == NULL )
7988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7989
7990 ssl_remove_psk( ssl );
7991 ssl->handshake->psk_opaque = psk_slot;
7992 return( 0 );
7993}
7994#endif /* MBEDTLS_USE_PSA_CRYPTO */
7995
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007996void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007998 size_t),
7999 void *p_psk )
8000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008001 conf->f_psk = f_psk;
8002 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008005
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008006#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008007
8008#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008009int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008010{
8011 int ret;
8012
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008013 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8014 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8015 {
8016 mbedtls_mpi_free( &conf->dhm_P );
8017 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008018 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008019 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008020
8021 return( 0 );
8022}
Hanno Becker470a8c42017-10-04 15:28:46 +01008023#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008024
Hanno Beckera90658f2017-10-04 15:29:08 +01008025int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8026 const unsigned char *dhm_P, size_t P_len,
8027 const unsigned char *dhm_G, size_t G_len )
8028{
8029 int ret;
8030
8031 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8032 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8033 {
8034 mbedtls_mpi_free( &conf->dhm_P );
8035 mbedtls_mpi_free( &conf->dhm_G );
8036 return( ret );
8037 }
8038
8039 return( 0 );
8040}
Paul Bakker5121ce52009-01-03 21:22:43 +00008041
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008042int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008043{
8044 int ret;
8045
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008046 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8047 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8048 {
8049 mbedtls_mpi_free( &conf->dhm_P );
8050 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008051 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008052 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008053
8054 return( 0 );
8055}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008056#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008057
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008058#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8059/*
8060 * Set the minimum length for Diffie-Hellman parameters
8061 */
8062void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8063 unsigned int bitlen )
8064{
8065 conf->dhm_min_bitlen = bitlen;
8066}
8067#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8068
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008069#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008070/*
8071 * Set allowed/preferred hashes for handshake signatures
8072 */
8073void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8074 const int *hashes )
8075{
8076 conf->sig_hashes = hashes;
8077}
Hanno Becker947194e2017-04-07 13:25:49 +01008078#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008079
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008080#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008081/*
8082 * Set the allowed elliptic curves
8083 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008084void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008085 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008087 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008088}
Hanno Becker947194e2017-04-07 13:25:49 +01008089#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008090
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008093{
Hanno Becker947194e2017-04-07 13:25:49 +01008094 /* Initialize to suppress unnecessary compiler warning */
8095 size_t hostname_len = 0;
8096
8097 /* Check if new hostname is valid before
8098 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008099 if( hostname != NULL )
8100 {
8101 hostname_len = strlen( hostname );
8102
8103 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8105 }
8106
8107 /* Now it's clear that we will overwrite the old hostname,
8108 * so we can free it safely */
8109
8110 if( ssl->hostname != NULL )
8111 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008112 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008113 mbedtls_free( ssl->hostname );
8114 }
8115
8116 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008117
Paul Bakker5121ce52009-01-03 21:22:43 +00008118 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008119 {
8120 ssl->hostname = NULL;
8121 }
8122 else
8123 {
8124 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008125 if( ssl->hostname == NULL )
8126 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008127
Hanno Becker947194e2017-04-07 13:25:49 +01008128 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008129
Hanno Becker947194e2017-04-07 13:25:49 +01008130 ssl->hostname[hostname_len] = '\0';
8131 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008132
8133 return( 0 );
8134}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008135#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008136
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008137#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008138void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008140 const unsigned char *, size_t),
8141 void *p_sni )
8142{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008143 conf->f_sni = f_sni;
8144 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008149int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008150{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008151 size_t cur_len, tot_len;
8152 const char **p;
8153
8154 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008155 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8156 * MUST NOT be truncated."
8157 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008158 */
8159 tot_len = 0;
8160 for( p = protos; *p != NULL; p++ )
8161 {
8162 cur_len = strlen( *p );
8163 tot_len += cur_len;
8164
8165 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008167 }
8168
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008169 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008170
8171 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008172}
8173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008175{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008176 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008179
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008180void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008182 conf->max_major_ver = major;
8183 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008184}
8185
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008186void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008187{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008188 conf->min_major_ver = major;
8189 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008190}
8191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008193void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008194{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008195 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008196}
8197#endif
8198
Janos Follath088ce432017-04-10 12:42:31 +01008199#if defined(MBEDTLS_SSL_SRV_C)
8200void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8201 char cert_req_ca_list )
8202{
8203 conf->cert_req_ca_list = cert_req_ca_list;
8204}
8205#endif
8206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008208void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008209{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008210 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008211}
8212#endif
8213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008215void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008216{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008217 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008218}
8219#endif
8220
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008221#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008222void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008224 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008225}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008226#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008229int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008232 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008235 }
8236
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008237 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008238
8239 return( 0 );
8240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008244void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008246 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008251void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008252{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008253 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008254}
8255#endif
8256
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008257void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008259 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008260}
8261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008263void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008265 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008266}
8267
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008268void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008270 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008271}
8272
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008273void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008274 const unsigned char period[8] )
8275{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008276 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008281#if defined(MBEDTLS_SSL_CLI_C)
8282void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008283{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008284 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008285}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008286#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008287
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008288#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008289void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8290 mbedtls_ssl_ticket_write_t *f_ticket_write,
8291 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8292 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008293{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008294 conf->f_ticket_write = f_ticket_write;
8295 conf->f_ticket_parse = f_ticket_parse;
8296 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008297}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008300
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008301#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8302void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8303 mbedtls_ssl_export_keys_t *f_export_keys,
8304 void *p_export_keys )
8305{
8306 conf->f_export_keys = f_export_keys;
8307 conf->p_export_keys = p_export_keys;
8308}
8309#endif
8310
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008311#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008312void mbedtls_ssl_conf_async_private_cb(
8313 mbedtls_ssl_config *conf,
8314 mbedtls_ssl_async_sign_t *f_async_sign,
8315 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8316 mbedtls_ssl_async_resume_t *f_async_resume,
8317 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008318 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008319{
8320 conf->f_async_sign_start = f_async_sign;
8321 conf->f_async_decrypt_start = f_async_decrypt;
8322 conf->f_async_resume = f_async_resume;
8323 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008324 conf->p_async_config_data = async_config_data;
8325}
8326
Gilles Peskine8f97af72018-04-26 11:46:10 +02008327void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8328{
8329 return( conf->p_async_config_data );
8330}
8331
Gilles Peskine1febfef2018-04-30 11:54:39 +02008332void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008333{
8334 if( ssl->handshake == NULL )
8335 return( NULL );
8336 else
8337 return( ssl->handshake->user_async_ctx );
8338}
8339
Gilles Peskine1febfef2018-04-30 11:54:39 +02008340void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008341 void *ctx )
8342{
8343 if( ssl->handshake != NULL )
8344 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008345}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008346#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008347
Paul Bakker5121ce52009-01-03 21:22:43 +00008348/*
8349 * SSL get accessors
8350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008352{
8353 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8354}
8355
Hanno Becker8b170a02017-10-10 11:51:19 +01008356int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8357{
8358 /*
8359 * Case A: We're currently holding back
8360 * a message for further processing.
8361 */
8362
8363 if( ssl->keep_current_message == 1 )
8364 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008365 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008366 return( 1 );
8367 }
8368
8369 /*
8370 * Case B: Further records are pending in the current datagram.
8371 */
8372
8373#if defined(MBEDTLS_SSL_PROTO_DTLS)
8374 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8375 ssl->in_left > ssl->next_record_offset )
8376 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008378 return( 1 );
8379 }
8380#endif /* MBEDTLS_SSL_PROTO_DTLS */
8381
8382 /*
8383 * Case C: A handshake message is being processed.
8384 */
8385
Hanno Becker8b170a02017-10-10 11:51:19 +01008386 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8387 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008389 return( 1 );
8390 }
8391
8392 /*
8393 * Case D: An application data message is being processed
8394 */
8395 if( ssl->in_offt != NULL )
8396 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008398 return( 1 );
8399 }
8400
8401 /*
8402 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008403 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008404 * we implement support for multiple alerts in single records.
8405 */
8406
8407 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8408 return( 0 );
8409}
8410
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008411uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008412{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008413 if( ssl->session != NULL )
8414 return( ssl->session->verify_result );
8415
8416 if( ssl->session_negotiate != NULL )
8417 return( ssl->session_negotiate->verify_result );
8418
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008419 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008420}
8421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008423{
Paul Bakker926c8e42013-03-06 10:23:34 +01008424 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008425 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008428}
8429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008432#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008434 {
8435 switch( ssl->minor_ver )
8436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008438 return( "DTLSv1.0" );
8439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008441 return( "DTLSv1.2" );
8442
8443 default:
8444 return( "unknown (DTLS)" );
8445 }
8446 }
8447#endif
8448
Paul Bakker43ca69c2011-01-15 17:35:19 +00008449 switch( ssl->minor_ver )
8450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008452 return( "SSLv3.0" );
8453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008455 return( "TLSv1.0" );
8456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008458 return( "TLSv1.1" );
8459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008461 return( "TLSv1.2" );
8462
Paul Bakker43ca69c2011-01-15 17:35:19 +00008463 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008464 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008465 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008466}
8467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008469{
Hanno Becker3136ede2018-08-17 15:28:19 +01008470 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008472 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008473
Hanno Becker78640902018-08-13 16:35:15 +01008474 if( transform == NULL )
8475 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477#if defined(MBEDTLS_ZLIB_SUPPORT)
8478 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8479 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008480#endif
8481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008484 case MBEDTLS_MODE_GCM:
8485 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008486 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008488 transform_expansion = transform->minlen;
8489 break;
8490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008492
8493 block_size = mbedtls_cipher_get_block_size(
8494 &transform->cipher_ctx_enc );
8495
Hanno Becker3136ede2018-08-17 15:28:19 +01008496 /* Expansion due to the addition of the MAC. */
8497 transform_expansion += transform->maclen;
8498
8499 /* Expansion due to the addition of CBC padding;
8500 * Theoretically up to 256 bytes, but we never use
8501 * more than the block size of the underlying cipher. */
8502 transform_expansion += block_size;
8503
8504 /* For TLS 1.1 or higher, an explicit IV is added
8505 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008506#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8507 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008508 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008509#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008510
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008511 break;
8512
8513 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008516 }
8517
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008518 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008519}
8520
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008521#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8522size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8523{
8524 size_t max_len;
8525
8526 /*
8527 * Assume mfl_code is correct since it was checked when set
8528 */
Angus Grattond8213d02016-05-25 20:56:48 +10008529 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008530
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008531 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008532 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008533 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008534 {
Angus Grattond8213d02016-05-25 20:56:48 +10008535 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008536 }
8537
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008538 /* During a handshake, use the value being negotiated */
8539 if( ssl->session_negotiate != NULL &&
8540 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8541 {
8542 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8543 }
8544
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008545 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008546}
8547#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8548
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008549#if defined(MBEDTLS_SSL_PROTO_DTLS)
8550static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8551{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008552 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8553 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8554 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8555 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8556 return ( 0 );
8557
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008558 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8559 return( ssl->mtu );
8560
8561 if( ssl->mtu == 0 )
8562 return( ssl->handshake->mtu );
8563
8564 return( ssl->mtu < ssl->handshake->mtu ?
8565 ssl->mtu : ssl->handshake->mtu );
8566}
8567#endif /* MBEDTLS_SSL_PROTO_DTLS */
8568
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008569int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8570{
8571 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8572
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008573#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8574 !defined(MBEDTLS_SSL_PROTO_DTLS)
8575 (void) ssl;
8576#endif
8577
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008578#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8579 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8580
8581 if( max_len > mfl )
8582 max_len = mfl;
8583#endif
8584
8585#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008586 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008587 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008588 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008589 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8590 const size_t overhead = (size_t) ret;
8591
8592 if( ret < 0 )
8593 return( ret );
8594
8595 if( mtu <= overhead )
8596 {
8597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8598 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8599 }
8600
8601 if( max_len > mtu - overhead )
8602 max_len = mtu - overhead;
8603 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008604#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008605
Hanno Becker0defedb2018-08-10 12:35:02 +01008606#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8607 !defined(MBEDTLS_SSL_PROTO_DTLS)
8608 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008609#endif
8610
8611 return( (int) max_len );
8612}
8613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614#if defined(MBEDTLS_X509_CRT_PARSE_C)
8615const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008616{
8617 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008618 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008619
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008620 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008625int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8626 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008627{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008628 if( ssl == NULL ||
8629 dst == NULL ||
8630 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008631 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008634 }
8635
Hanno Becker52055ae2019-02-06 14:30:46 +00008636 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008639
Paul Bakker5121ce52009-01-03 21:22:43 +00008640/*
Paul Bakker1961b702013-01-25 14:49:24 +01008641 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008646
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008647 if( ssl == NULL || ssl->conf == NULL )
8648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008651 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008657#endif
8658
Paul Bakker1961b702013-01-25 14:49:24 +01008659 return( ret );
8660}
8661
8662/*
8663 * Perform the SSL handshake
8664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008666{
8667 int ret = 0;
8668
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008669 if( ssl == NULL || ssl->conf == NULL )
8670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008677
8678 if( ret != 0 )
8679 break;
8680 }
8681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008683
8684 return( ret );
8685}
8686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687#if defined(MBEDTLS_SSL_RENEGOTIATION)
8688#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008689/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008690 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008693{
8694 int ret;
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008697
8698 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8700 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008701
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008702 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008703 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008705 return( ret );
8706 }
8707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008709
8710 return( 0 );
8711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008713
8714/*
8715 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 * - any side: calling mbedtls_ssl_renegotiate(),
8717 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8718 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008719 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008720 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008724{
8725 int ret;
8726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008728
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008729 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8730 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008731
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008732 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8733 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008735 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008737 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008739 ssl->handshake->out_msg_seq = 1;
8740 else
8741 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008742 }
8743#endif
8744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8746 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008748 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008751 return( ret );
8752 }
8753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008755
8756 return( 0 );
8757}
8758
8759/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008760 * Renegotiate current connection on client,
8761 * or request renegotiation on server
8762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008766
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008767 if( ssl == NULL || ssl->conf == NULL )
8768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008771 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008772 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008778
8779 /* Did we already try/start sending HelloRequest? */
8780 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008782
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008783 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008788 /*
8789 * On client, either start the renegotiation process or,
8790 * if already in progress, continue the handshake
8791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008796
8797 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008800 return( ret );
8801 }
8802 }
8803 else
8804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008808 return( ret );
8809 }
8810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008812
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008813 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008814}
8815
8816/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008817 * Check record counters and renegotiate if they're above the limit.
8818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008819static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008820{
Andres AG2196c7f2016-12-15 17:01:16 +00008821 size_t ep_len = ssl_ep_len( ssl );
8822 int in_ctr_cmp;
8823 int out_ctr_cmp;
8824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8826 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008827 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008828 {
8829 return( 0 );
8830 }
8831
Andres AG2196c7f2016-12-15 17:01:16 +00008832 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8833 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008834 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008835 ssl->conf->renego_period + ep_len, 8 - ep_len );
8836
8837 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008838 {
8839 return( 0 );
8840 }
8841
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008846
8847/*
8848 * Receive application data decrypted from the SSL layer
8849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008851{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008852 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008853 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008854
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008855 if( ssl == NULL || ssl->conf == NULL )
8856 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008864 return( ret );
8865
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008866 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008868 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008869 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008870 return( ret );
8871 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008872 }
8873#endif
8874
Hanno Becker4a810fb2017-05-24 16:27:30 +01008875 /*
8876 * Check if renegotiation is necessary and/or handshake is
8877 * in process. If yes, perform/continue, and fall through
8878 * if an unexpected packet is received while the client
8879 * is waiting for the ServerHello.
8880 *
8881 * (There is no equivalent to the last condition on
8882 * the server-side as it is not treated as within
8883 * a handshake while waiting for the ClientHello
8884 * after a renegotiation request.)
8885 */
8886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008888 ret = ssl_check_ctr_renegotiate( ssl );
8889 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8890 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008893 return( ret );
8894 }
8895#endif
8896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008899 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008900 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8901 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008904 return( ret );
8905 }
8906 }
8907
Hanno Beckere41158b2017-10-23 13:30:32 +01008908 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008909 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008910 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008911 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008912 if( ssl->f_get_timer != NULL &&
8913 ssl->f_get_timer( ssl->p_timer ) == -1 )
8914 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008915 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008916 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008917
Hanno Becker327c93b2018-08-15 13:56:18 +01008918 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008919 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008920 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8921 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008922
Hanno Becker4a810fb2017-05-24 16:27:30 +01008923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8924 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008925 }
8926
8927 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008929 {
8930 /*
8931 * OpenSSL sends empty messages to randomize the IV
8932 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008933 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008935 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008936 return( 0 );
8937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008939 return( ret );
8940 }
8941 }
8942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008946
Hanno Becker4a810fb2017-05-24 16:27:30 +01008947 /*
8948 * - For client-side, expect SERVER_HELLO_REQUEST.
8949 * - For server-side, expect CLIENT_HELLO.
8950 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8951 */
8952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008956 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008959
8960 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008963 {
8964 continue;
8965 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008967 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008968 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008969#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008970
Hanno Becker4a810fb2017-05-24 16:27:30 +01008971#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008972 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008976
8977 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008979 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008980 {
8981 continue;
8982 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008985 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008986#endif /* MBEDTLS_SSL_SRV_C */
8987
Hanno Becker21df7f92017-10-17 11:03:26 +01008988#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008989 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008990 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8991 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8992 ssl->conf->allow_legacy_renegotiation ==
8993 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8994 {
8995 /*
8996 * Accept renegotiation request
8997 */
Paul Bakker48916f92012-09-16 19:57:18 +00008998
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008999 /* DTLS clients need to know renego is server-initiated */
9000#if defined(MBEDTLS_SSL_PROTO_DTLS)
9001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9002 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9003 {
9004 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9005 }
9006#endif
9007 ret = ssl_start_renegotiation( ssl );
9008 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9009 ret != 0 )
9010 {
9011 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9012 return( ret );
9013 }
9014 }
9015 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009016#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009017 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009018 /*
9019 * Refuse renegotiation
9020 */
9021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024#if defined(MBEDTLS_SSL_PROTO_SSL3)
9025 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009026 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009027 /* SSLv3 does not have a "no_renegotiation" warning, so
9028 we send a fatal alert and abort the connection. */
9029 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9030 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9031 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009032 }
9033 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9035#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9036 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9037 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9040 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9041 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009042 {
9043 return( ret );
9044 }
Paul Bakker48916f92012-09-16 19:57:18 +00009045 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009046 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9048 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009052 }
Paul Bakker48916f92012-09-16 19:57:18 +00009053 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009054
Hanno Becker90333da2017-10-10 11:27:13 +01009055 /* At this point, we don't know whether the renegotiation has been
9056 * completed or not. The cases to consider are the following:
9057 * 1) The renegotiation is complete. In this case, no new record
9058 * has been read yet.
9059 * 2) The renegotiation is incomplete because the client received
9060 * an application data record while awaiting the ServerHello.
9061 * 3) The renegotiation is incomplete because the client received
9062 * a non-handshake, non-application data message while awaiting
9063 * the ServerHello.
9064 * In each of these case, looping will be the proper action:
9065 * - For 1), the next iteration will read a new record and check
9066 * if it's application data.
9067 * - For 2), the loop condition isn't satisfied as application data
9068 * is present, hence continue is the same as break
9069 * - For 3), the loop condition is satisfied and read_record
9070 * will re-deliver the message that was held back by the client
9071 * when expecting the ServerHello.
9072 */
9073 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009074 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009075#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009077 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009078 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009079 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009080 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009083 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009085 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009086 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009087 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9091 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009094 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009095 }
9096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9100 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009101 }
9102
9103 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009104
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009105 /* We're going to return something now, cancel timer,
9106 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009108 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009111 /* If we requested renego but received AppData, resend HelloRequest.
9112 * Do it now, after setting in_offt, to avoid taking this branch
9113 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009117 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009118 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009121 return( ret );
9122 }
9123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009125#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009126 }
9127
9128 n = ( len < ssl->in_msglen )
9129 ? len : ssl->in_msglen;
9130
9131 memcpy( buf, ssl->in_offt, n );
9132 ssl->in_msglen -= n;
9133
9134 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009135 {
9136 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009137 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009138 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009140 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009141 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009142 /* more data available */
9143 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009147
Paul Bakker23986e52011-04-24 08:57:21 +00009148 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009149}
9150
9151/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009152 * Send application data to be encrypted by the SSL layer, taking care of max
9153 * fragment length and buffer size.
9154 *
9155 * According to RFC 5246 Section 6.2.1:
9156 *
9157 * Zero-length fragments of Application data MAY be sent as they are
9158 * potentially useful as a traffic analysis countermeasure.
9159 *
9160 * Therefore, it is possible that the input message length is 0 and the
9161 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009162 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009163static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009164 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009165{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009166 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9167 const size_t max_len = (size_t) ret;
9168
9169 if( ret < 0 )
9170 {
9171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9172 return( ret );
9173 }
9174
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009175 if( len > max_len )
9176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009181 "maximum fragment length: %d > %d",
9182 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009184 }
9185 else
9186#endif
9187 len = max_len;
9188 }
Paul Bakker887bd502011-06-08 13:10:54 +00009189
Paul Bakker5121ce52009-01-03 21:22:43 +00009190 if( ssl->out_left != 0 )
9191 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009192 /*
9193 * The user has previously tried to send the data and
9194 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9195 * written. In this case, we expect the high-level write function
9196 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009201 return( ret );
9202 }
9203 }
Paul Bakker887bd502011-06-08 13:10:54 +00009204 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009205 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009206 /*
9207 * The user is trying to send a message the first time, so we need to
9208 * copy the data into the internal buffers and setup the data structure
9209 * to keep track of partial writes
9210 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009211 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009213 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009214
Hanno Becker67bc7c32018-08-06 11:33:50 +01009215 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009218 return( ret );
9219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009220 }
9221
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009222 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009223}
9224
9225/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009226 * Write application data, doing 1/n-1 splitting if necessary.
9227 *
9228 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009229 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009230 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009233static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009234 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009235{
9236 int ret;
9237
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009238 if( ssl->conf->cbc_record_splitting ==
9239 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009240 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9242 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9243 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009244 {
9245 return( ssl_write_real( ssl, buf, len ) );
9246 }
9247
9248 if( ssl->split_done == 0 )
9249 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009250 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009251 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009252 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009253 }
9254
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009255 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9256 return( ret );
9257 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009258
9259 return( ret + 1 );
9260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009262
9263/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009264 * Write application data (public-facing wrapper)
9265 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009266int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009267{
9268 int ret;
9269
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009271
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009272 if( ssl == NULL || ssl->conf == NULL )
9273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9274
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009275#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009276 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9277 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009279 return( ret );
9280 }
9281#endif
9282
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009283 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009284 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009285 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009286 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009288 return( ret );
9289 }
9290 }
9291
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009292#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009293 ret = ssl_write_split( ssl, buf, len );
9294#else
9295 ret = ssl_write_real( ssl, buf, len );
9296#endif
9297
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009299
9300 return( ret );
9301}
9302
9303/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009304 * Notify the peer that the connection is being closed
9305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009307{
9308 int ret;
9309
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009310 if( ssl == NULL || ssl->conf == NULL )
9311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009314
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009315 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9321 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9322 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009325 return( ret );
9326 }
9327 }
9328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009330
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009331 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009332}
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009335{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009336 if( transform == NULL )
9337 return;
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009340 deflateEnd( &transform->ctx_deflate );
9341 inflateEnd( &transform->ctx_inflate );
9342#endif
9343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9345 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 mbedtls_md_free( &transform->md_ctx_enc );
9348 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009349
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009350 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009351}
9352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353#if defined(MBEDTLS_X509_CRT_PARSE_C)
9354static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009357
9358 while( cur != NULL )
9359 {
9360 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009362 cur = next;
9363 }
9364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009366
Hanno Becker0271f962018-08-16 13:23:47 +01009367#if defined(MBEDTLS_SSL_PROTO_DTLS)
9368
9369static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9370{
9371 unsigned offset;
9372 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9373
9374 if( hs == NULL )
9375 return;
9376
Hanno Becker283f5ef2018-08-24 09:34:47 +01009377 ssl_free_buffered_record( ssl );
9378
Hanno Becker0271f962018-08-16 13:23:47 +01009379 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009380 ssl_buffering_free_slot( ssl, offset );
9381}
9382
9383static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9384 uint8_t slot )
9385{
9386 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9387 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009388
9389 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9390 return;
9391
Hanno Beckere605b192018-08-21 15:59:07 +01009392 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009393 {
Hanno Beckere605b192018-08-21 15:59:07 +01009394 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009395 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009396 mbedtls_free( hs_buf->data );
9397 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009398 }
9399}
9400
9401#endif /* MBEDTLS_SSL_PROTO_DTLS */
9402
Gilles Peskine9b562d52018-04-25 20:32:43 +02009403void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009404{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009405 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9406
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009407 if( handshake == NULL )
9408 return;
9409
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009410#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9411 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9412 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009413 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009414 handshake->async_in_progress = 0;
9415 }
9416#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9417
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009418#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9419 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9420 mbedtls_md5_free( &handshake->fin_md5 );
9421 mbedtls_sha1_free( &handshake->fin_sha1 );
9422#endif
9423#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9424#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009425#if defined(MBEDTLS_USE_PSA_CRYPTO)
9426 psa_hash_abort( &handshake->fin_sha256_psa );
9427#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009428 mbedtls_sha256_free( &handshake->fin_sha256 );
9429#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009430#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009431#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009432#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009433 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009434#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009435 mbedtls_sha512_free( &handshake->fin_sha512 );
9436#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009437#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440#if defined(MBEDTLS_DHM_C)
9441 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009442#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443#if defined(MBEDTLS_ECDH_C)
9444 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009445#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009446#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009447 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009448#if defined(MBEDTLS_SSL_CLI_C)
9449 mbedtls_free( handshake->ecjpake_cache );
9450 handshake->ecjpake_cache = NULL;
9451 handshake->ecjpake_cache_len = 0;
9452#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009453#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009454
Janos Follath4ae5c292016-02-10 11:27:43 +00009455#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9456 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009457 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009459#endif
9460
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009461#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9462 if( handshake->psk != NULL )
9463 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009464 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009465 mbedtls_free( handshake->psk );
9466 }
9467#endif
9468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9470 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009471 /*
9472 * Free only the linked list wrapper, not the keys themselves
9473 * since the belong to the SNI callback
9474 */
9475 if( handshake->sni_key_cert != NULL )
9476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009478
9479 while( cur != NULL )
9480 {
9481 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009483 cur = next;
9484 }
9485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009487
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009488#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009489 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009490#endif
9491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#if defined(MBEDTLS_SSL_PROTO_DTLS)
9493 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009494 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009495 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009496#endif
9497
Hanno Becker4a63ed42019-01-08 11:39:35 +00009498#if defined(MBEDTLS_ECDH_C) && \
9499 defined(MBEDTLS_USE_PSA_CRYPTO)
9500 psa_destroy_key( handshake->ecdh_psa_privkey );
9501#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9502
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009503 mbedtls_platform_zeroize( handshake,
9504 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009505}
9506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009508{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009509 if( session == NULL )
9510 return;
9511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009513 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009514#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009515
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009516#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009518#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009519
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009520 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009521}
9522
Paul Bakker5121ce52009-01-03 21:22:43 +00009523/*
9524 * Free an SSL context
9525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009527{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009528 if( ssl == NULL )
9529 return;
9530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009532
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009533 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009534 {
Angus Grattond8213d02016-05-25 20:56:48 +10009535 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009537 }
9538
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009539 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009540 {
Angus Grattond8213d02016-05-25 20:56:48 +10009541 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009543 }
9544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009546 if( ssl->compress_buf != NULL )
9547 {
Angus Grattond8213d02016-05-25 20:56:48 +10009548 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009550 }
9551#endif
9552
Paul Bakker48916f92012-09-16 19:57:18 +00009553 if( ssl->transform )
9554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 mbedtls_ssl_transform_free( ssl->transform );
9556 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009557 }
9558
9559 if( ssl->handshake )
9560 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009561 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9563 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 mbedtls_free( ssl->handshake );
9566 mbedtls_free( ssl->transform_negotiate );
9567 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009568 }
9569
Paul Bakkerc0463502013-02-14 11:19:38 +01009570 if( ssl->session )
9571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 mbedtls_ssl_session_free( ssl->session );
9573 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009574 }
9575
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009576#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009577 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009578 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009579 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009581 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9585 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9588 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009589 }
9590#endif
9591
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009592#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009594#endif
9595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009597
Paul Bakker86f04f42013-02-14 11:20:09 +01009598 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009599 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009600}
9601
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009602/*
9603 * Initialze mbedtls_ssl_config
9604 */
9605void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9606{
9607 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9608}
9609
Simon Butcherc97b6972015-12-27 23:48:17 +00009610#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009611static int ssl_preset_default_hashes[] = {
9612#if defined(MBEDTLS_SHA512_C)
9613 MBEDTLS_MD_SHA512,
9614 MBEDTLS_MD_SHA384,
9615#endif
9616#if defined(MBEDTLS_SHA256_C)
9617 MBEDTLS_MD_SHA256,
9618 MBEDTLS_MD_SHA224,
9619#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009620#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009621 MBEDTLS_MD_SHA1,
9622#endif
9623 MBEDTLS_MD_NONE
9624};
Simon Butcherc97b6972015-12-27 23:48:17 +00009625#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009626
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009627static int ssl_preset_suiteb_ciphersuites[] = {
9628 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9629 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9630 0
9631};
9632
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009633#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009634static int ssl_preset_suiteb_hashes[] = {
9635 MBEDTLS_MD_SHA256,
9636 MBEDTLS_MD_SHA384,
9637 MBEDTLS_MD_NONE
9638};
9639#endif
9640
9641#if defined(MBEDTLS_ECP_C)
9642static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9643 MBEDTLS_ECP_DP_SECP256R1,
9644 MBEDTLS_ECP_DP_SECP384R1,
9645 MBEDTLS_ECP_DP_NONE
9646};
9647#endif
9648
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009649/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009650 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009651 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009652int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009653 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009654{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009655#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009656 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009657#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009658
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009659 /* Use the functions here so that they are covered in tests,
9660 * but otherwise access member directly for efficiency */
9661 mbedtls_ssl_conf_endpoint( conf, endpoint );
9662 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009663
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009664 /*
9665 * Things that are common to all presets
9666 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009667#if defined(MBEDTLS_SSL_CLI_C)
9668 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9669 {
9670 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9671#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9672 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9673#endif
9674 }
9675#endif
9676
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009677#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009678 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009679#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009680
9681#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9682 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9683#endif
9684
9685#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9686 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9687#endif
9688
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009689#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9690 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9691#endif
9692
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009693#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009694 conf->f_cookie_write = ssl_cookie_write_dummy;
9695 conf->f_cookie_check = ssl_cookie_check_dummy;
9696#endif
9697
9698#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9699 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9700#endif
9701
Janos Follath088ce432017-04-10 12:42:31 +01009702#if defined(MBEDTLS_SSL_SRV_C)
9703 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9704#endif
9705
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009706#if defined(MBEDTLS_SSL_PROTO_DTLS)
9707 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9708 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9709#endif
9710
9711#if defined(MBEDTLS_SSL_RENEGOTIATION)
9712 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009713 memset( conf->renego_period, 0x00, 2 );
9714 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009715#endif
9716
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009717#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9718 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9719 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009720 const unsigned char dhm_p[] =
9721 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9722 const unsigned char dhm_g[] =
9723 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9724
Hanno Beckera90658f2017-10-04 15:29:08 +01009725 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9726 dhm_p, sizeof( dhm_p ),
9727 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009728 {
9729 return( ret );
9730 }
9731 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009732#endif
9733
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009734 /*
9735 * Preset-specific defaults
9736 */
9737 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009738 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009739 /*
9740 * NSA Suite B
9741 */
9742 case MBEDTLS_SSL_PRESET_SUITEB:
9743 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9744 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9745 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9746 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9747
9748 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9749 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9750 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9751 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9752 ssl_preset_suiteb_ciphersuites;
9753
9754#if defined(MBEDTLS_X509_CRT_PARSE_C)
9755 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009756#endif
9757
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009758#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009759 conf->sig_hashes = ssl_preset_suiteb_hashes;
9760#endif
9761
9762#if defined(MBEDTLS_ECP_C)
9763 conf->curve_list = ssl_preset_suiteb_curves;
9764#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009765 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009766
9767 /*
9768 * Default
9769 */
9770 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009771 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9772 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9773 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9774 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9775 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9776 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9777 MBEDTLS_SSL_MIN_MINOR_VERSION :
9778 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009779 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9780 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9781
9782#if defined(MBEDTLS_SSL_PROTO_DTLS)
9783 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9784 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9785#endif
9786
9787 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9788 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9789 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9790 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9791 mbedtls_ssl_list_ciphersuites();
9792
9793#if defined(MBEDTLS_X509_CRT_PARSE_C)
9794 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9795#endif
9796
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009797#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009798 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009799#endif
9800
9801#if defined(MBEDTLS_ECP_C)
9802 conf->curve_list = mbedtls_ecp_grp_id_list();
9803#endif
9804
9805#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9806 conf->dhm_min_bitlen = 1024;
9807#endif
9808 }
9809
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009810 return( 0 );
9811}
9812
9813/*
9814 * Free mbedtls_ssl_config
9815 */
9816void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9817{
9818#if defined(MBEDTLS_DHM_C)
9819 mbedtls_mpi_free( &conf->dhm_P );
9820 mbedtls_mpi_free( &conf->dhm_G );
9821#endif
9822
9823#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9824 if( conf->psk != NULL )
9825 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009826 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009827 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009828 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009829 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009830 }
9831
9832 if( conf->psk_identity != NULL )
9833 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009834 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009835 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009836 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009837 conf->psk_identity_len = 0;
9838 }
9839#endif
9840
9841#if defined(MBEDTLS_X509_CRT_PARSE_C)
9842 ssl_key_cert_free( conf->key_cert );
9843#endif
9844
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009845 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009846}
9847
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009848#if defined(MBEDTLS_PK_C) && \
9849 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009850/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#if defined(MBEDTLS_RSA_C)
9856 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9857 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859#if defined(MBEDTLS_ECDSA_C)
9860 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9861 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009864}
9865
Hanno Becker7e5437a2017-04-28 17:15:26 +01009866unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9867{
9868 switch( type ) {
9869 case MBEDTLS_PK_RSA:
9870 return( MBEDTLS_SSL_SIG_RSA );
9871 case MBEDTLS_PK_ECDSA:
9872 case MBEDTLS_PK_ECKEY:
9873 return( MBEDTLS_SSL_SIG_ECDSA );
9874 default:
9875 return( MBEDTLS_SSL_SIG_ANON );
9876 }
9877}
9878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009880{
9881 switch( sig )
9882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#if defined(MBEDTLS_RSA_C)
9884 case MBEDTLS_SSL_SIG_RSA:
9885 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887#if defined(MBEDTLS_ECDSA_C)
9888 case MBEDTLS_SSL_SIG_ECDSA:
9889 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009890#endif
9891 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009893 }
9894}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009895#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009896
Hanno Becker7e5437a2017-04-28 17:15:26 +01009897#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9898 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9899
9900/* Find an entry in a signature-hash set matching a given hash algorithm. */
9901mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9902 mbedtls_pk_type_t sig_alg )
9903{
9904 switch( sig_alg )
9905 {
9906 case MBEDTLS_PK_RSA:
9907 return( set->rsa );
9908 case MBEDTLS_PK_ECDSA:
9909 return( set->ecdsa );
9910 default:
9911 return( MBEDTLS_MD_NONE );
9912 }
9913}
9914
9915/* Add a signature-hash-pair to a signature-hash set */
9916void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9917 mbedtls_pk_type_t sig_alg,
9918 mbedtls_md_type_t md_alg )
9919{
9920 switch( sig_alg )
9921 {
9922 case MBEDTLS_PK_RSA:
9923 if( set->rsa == MBEDTLS_MD_NONE )
9924 set->rsa = md_alg;
9925 break;
9926
9927 case MBEDTLS_PK_ECDSA:
9928 if( set->ecdsa == MBEDTLS_MD_NONE )
9929 set->ecdsa = md_alg;
9930 break;
9931
9932 default:
9933 break;
9934 }
9935}
9936
9937/* Allow exactly one hash algorithm for each signature. */
9938void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9939 mbedtls_md_type_t md_alg )
9940{
9941 set->rsa = md_alg;
9942 set->ecdsa = md_alg;
9943}
9944
9945#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9946 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9947
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009948/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009949 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009952{
9953 switch( hash )
9954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955#if defined(MBEDTLS_MD5_C)
9956 case MBEDTLS_SSL_HASH_MD5:
9957 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959#if defined(MBEDTLS_SHA1_C)
9960 case MBEDTLS_SSL_HASH_SHA1:
9961 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963#if defined(MBEDTLS_SHA256_C)
9964 case MBEDTLS_SSL_HASH_SHA224:
9965 return( MBEDTLS_MD_SHA224 );
9966 case MBEDTLS_SSL_HASH_SHA256:
9967 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969#if defined(MBEDTLS_SHA512_C)
9970 case MBEDTLS_SSL_HASH_SHA384:
9971 return( MBEDTLS_MD_SHA384 );
9972 case MBEDTLS_SSL_HASH_SHA512:
9973 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009974#endif
9975 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009977 }
9978}
9979
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009980/*
9981 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9982 */
9983unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9984{
9985 switch( md )
9986 {
9987#if defined(MBEDTLS_MD5_C)
9988 case MBEDTLS_MD_MD5:
9989 return( MBEDTLS_SSL_HASH_MD5 );
9990#endif
9991#if defined(MBEDTLS_SHA1_C)
9992 case MBEDTLS_MD_SHA1:
9993 return( MBEDTLS_SSL_HASH_SHA1 );
9994#endif
9995#if defined(MBEDTLS_SHA256_C)
9996 case MBEDTLS_MD_SHA224:
9997 return( MBEDTLS_SSL_HASH_SHA224 );
9998 case MBEDTLS_MD_SHA256:
9999 return( MBEDTLS_SSL_HASH_SHA256 );
10000#endif
10001#if defined(MBEDTLS_SHA512_C)
10002 case MBEDTLS_MD_SHA384:
10003 return( MBEDTLS_SSL_HASH_SHA384 );
10004 case MBEDTLS_MD_SHA512:
10005 return( MBEDTLS_SSL_HASH_SHA512 );
10006#endif
10007 default:
10008 return( MBEDTLS_SSL_HASH_NONE );
10009 }
10010}
10011
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010012#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010013/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010014 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010015 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010016 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010017int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010020
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010021 if( ssl->conf->curve_list == NULL )
10022 return( -1 );
10023
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010024 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010025 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010026 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010027
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010028 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010029}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010030#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010031
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010032#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010033/*
10034 * Check if a hash proposed by the peer is in our list.
10035 * Return 0 if we're willing to use it, -1 otherwise.
10036 */
10037int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10038 mbedtls_md_type_t md )
10039{
10040 const int *cur;
10041
10042 if( ssl->conf->sig_hashes == NULL )
10043 return( -1 );
10044
10045 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10046 if( *cur == (int) md )
10047 return( 0 );
10048
10049 return( -1 );
10050}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010051#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
10054int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10055 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010056 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010057 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010058{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010059 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010061 int usage = 0;
10062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010064 const char *ext_oid;
10065 size_t ext_len;
10066#endif
10067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10069 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010070 ((void) cert);
10071 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010072 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010073#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10076 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010077 {
10078 /* Server part of the key exchange */
10079 switch( ciphersuite->key_exchange )
10080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 case MBEDTLS_KEY_EXCHANGE_RSA:
10082 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010083 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010084 break;
10085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10087 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10088 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10089 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010090 break;
10091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10093 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010094 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010095 break;
10096
10097 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 case MBEDTLS_KEY_EXCHANGE_NONE:
10099 case MBEDTLS_KEY_EXCHANGE_PSK:
10100 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10101 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010102 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010103 usage = 0;
10104 }
10105 }
10106 else
10107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10109 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010110 }
10111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010113 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010114 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010115 ret = -1;
10116 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010117#else
10118 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10122 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10125 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010126 }
10127 else
10128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10130 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010131 }
10132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010134 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010135 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010136 ret = -1;
10137 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010139
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010140 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010143
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010144/*
10145 * Convert version numbers to/from wire format
10146 * and, for DTLS, to/from TLS equivalent.
10147 *
10148 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010149 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010150 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10151 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010154 unsigned char ver[2] )
10155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010156#if defined(MBEDTLS_SSL_PROTO_DTLS)
10157 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010160 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10161
10162 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10163 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10164 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010165 else
10166#else
10167 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010168#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010169 {
10170 ver[0] = (unsigned char) major;
10171 ver[1] = (unsigned char) minor;
10172 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010173}
10174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010175void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010176 const unsigned char ver[2] )
10177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178#if defined(MBEDTLS_SSL_PROTO_DTLS)
10179 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010180 {
10181 *major = 255 - ver[0] + 2;
10182 *minor = 255 - ver[1] + 1;
10183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010185 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10186 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010187 else
10188#else
10189 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010190#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010191 {
10192 *major = ver[0];
10193 *minor = ver[1];
10194 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010195}
10196
Simon Butcher99000142016-10-13 17:21:01 +010010197int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10198{
10199#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10200 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10201 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10202
10203 switch( md )
10204 {
10205#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10206#if defined(MBEDTLS_MD5_C)
10207 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010208 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010209#endif
10210#if defined(MBEDTLS_SHA1_C)
10211 case MBEDTLS_SSL_HASH_SHA1:
10212 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10213 break;
10214#endif
10215#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10216#if defined(MBEDTLS_SHA512_C)
10217 case MBEDTLS_SSL_HASH_SHA384:
10218 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10219 break;
10220#endif
10221#if defined(MBEDTLS_SHA256_C)
10222 case MBEDTLS_SSL_HASH_SHA256:
10223 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10224 break;
10225#endif
10226 default:
10227 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10228 }
10229
10230 return 0;
10231#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10232 (void) ssl;
10233 (void) md;
10234
10235 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10237}
10238
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010239#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10240 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10241int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10242 unsigned char *output,
10243 unsigned char *data, size_t data_len )
10244{
10245 int ret = 0;
10246 mbedtls_md5_context mbedtls_md5;
10247 mbedtls_sha1_context mbedtls_sha1;
10248
10249 mbedtls_md5_init( &mbedtls_md5 );
10250 mbedtls_sha1_init( &mbedtls_sha1 );
10251
10252 /*
10253 * digitally-signed struct {
10254 * opaque md5_hash[16];
10255 * opaque sha_hash[20];
10256 * };
10257 *
10258 * md5_hash
10259 * MD5(ClientHello.random + ServerHello.random
10260 * + ServerParams);
10261 * sha_hash
10262 * SHA(ClientHello.random + ServerHello.random
10263 * + ServerParams);
10264 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010265 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010266 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010268 goto exit;
10269 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010270 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010271 ssl->handshake->randbytes, 64 ) ) != 0 )
10272 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010274 goto exit;
10275 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010276 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010277 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010279 goto exit;
10280 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010281 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010282 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010284 goto exit;
10285 }
10286
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010287 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010288 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010290 goto exit;
10291 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010292 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010293 ssl->handshake->randbytes, 64 ) ) != 0 )
10294 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010296 goto exit;
10297 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010298 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010299 data_len ) ) != 0 )
10300 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010302 goto exit;
10303 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010304 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010305 output + 16 ) ) != 0 )
10306 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010308 goto exit;
10309 }
10310
10311exit:
10312 mbedtls_md5_free( &mbedtls_md5 );
10313 mbedtls_sha1_free( &mbedtls_sha1 );
10314
10315 if( ret != 0 )
10316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10317 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10318
10319 return( ret );
10320
10321}
10322#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10323 MBEDTLS_SSL_PROTO_TLS1_1 */
10324
10325#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10326 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010327
10328#if defined(MBEDTLS_USE_PSA_CRYPTO)
10329int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10330 unsigned char *hash, size_t *hashlen,
10331 unsigned char *data, size_t data_len,
10332 mbedtls_md_type_t md_alg )
10333{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010334 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010335 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010336 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10337
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010339
10340 if( ( status = psa_hash_setup( &hash_operation,
10341 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010342 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010343 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010344 goto exit;
10345 }
10346
Andrzej Kurek814feff2019-01-14 04:35:19 -050010347 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10348 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010349 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010350 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010351 goto exit;
10352 }
10353
Andrzej Kurek814feff2019-01-14 04:35:19 -050010354 if( ( status = psa_hash_update( &hash_operation,
10355 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010356 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010357 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010358 goto exit;
10359 }
10360
Andrzej Kurek814feff2019-01-14 04:35:19 -050010361 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10362 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010363 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010364 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010365 goto exit;
10366 }
10367
10368exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010369 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010370 {
10371 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10372 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010373 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010374 {
10375 case PSA_ERROR_NOT_SUPPORTED:
10376 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010377 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010378 case PSA_ERROR_BUFFER_TOO_SMALL:
10379 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10380 case PSA_ERROR_INSUFFICIENT_MEMORY:
10381 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10382 default:
10383 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10384 }
10385 }
10386 return( 0 );
10387}
10388
10389#else
10390
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010391int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010392 unsigned char *hash, size_t *hashlen,
10393 unsigned char *data, size_t data_len,
10394 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010395{
10396 int ret = 0;
10397 mbedtls_md_context_t ctx;
10398 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010399 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010400
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010402
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010403 mbedtls_md_init( &ctx );
10404
10405 /*
10406 * digitally-signed struct {
10407 * opaque client_random[32];
10408 * opaque server_random[32];
10409 * ServerDHParams params;
10410 * };
10411 */
10412 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10413 {
10414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10415 goto exit;
10416 }
10417 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10418 {
10419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10420 goto exit;
10421 }
10422 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10423 {
10424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10425 goto exit;
10426 }
10427 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10428 {
10429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10430 goto exit;
10431 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010432 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010433 {
10434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10435 goto exit;
10436 }
10437
10438exit:
10439 mbedtls_md_free( &ctx );
10440
10441 if( ret != 0 )
10442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10443 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10444
10445 return( ret );
10446}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010447#endif /* MBEDTLS_USE_PSA_CRYPTO */
10448
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010449#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10450 MBEDTLS_SSL_PROTO_TLS1_2 */
10451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452#endif /* MBEDTLS_SSL_TLS_C */