blob: 26832bc96cc2f82922b0bb744ce8aa4802b8f8a8 [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 Beckerdef9bdc2019-01-30 14:46:46 +00005728static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5729 unsigned char *crt_buf,
5730 size_t crt_buf_len )
5731{
5732 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5733
5734 if( peer_crt == NULL )
5735 return( -1 );
5736
5737 if( peer_crt->raw.len != crt_buf_len )
5738 return( -1 );
5739
Hanno Becker46f34d02019-02-08 14:00:04 +00005740 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005741}
Hanno Becker84879e32019-01-31 07:44:03 +00005742#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005743
Hanno Becker1294a0b2019-02-05 12:38:15 +00005744static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5745{
5746 if( session->peer_cert != NULL )
5747 {
5748 mbedtls_x509_crt_free( session->peer_cert );
5749 mbedtls_free( session->peer_cert );
5750 session->peer_cert = NULL;
5751 }
Hanno Becker9198ad12019-02-05 17:00:50 +00005752
5753#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5754 if( session->peer_cert_digest != NULL )
5755 {
5756 /* Zeroization is not necessary. */
5757 mbedtls_free( session->peer_cert_digest );
5758 session->peer_cert_digest = NULL;
5759 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5760 session->peer_cert_digest_len = 0;
5761 }
5762#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker1294a0b2019-02-05 12:38:15 +00005763}
5764
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005765/*
5766 * Once the certificate message is read, parse it into a cert chain and
5767 * perform basic checks, but leave actual verification to the caller
5768 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005769static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5770 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005771{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005772 int ret;
5773#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5774 int crt_cnt=0;
5775#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005776 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005777 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005782 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5783 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005785 }
5786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5788 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005791 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5792 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005794 }
5795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005797
Paul Bakker5121ce52009-01-03 21:22:43 +00005798 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005800 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005801 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005802
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005803 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005807 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5808 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005810 }
5811
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005812 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5813 i += 3;
5814
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005815 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 while( i < ssl->in_hslen )
5817 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005818 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005819 if ( i + 3 > ssl->in_hslen ) {
5820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005821 mbedtls_ssl_send_alert_message( ssl,
5822 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005824 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5825 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005826 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5827 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 if( ssl->in_msg[i] != 0 )
5829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005831 mbedtls_ssl_send_alert_message( ssl,
5832 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5833 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005835 }
5836
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005837 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005838 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5839 | (unsigned int) ssl->in_msg[i + 2];
5840 i += 3;
5841
5842 if( n < 128 || i + n > ssl->in_hslen )
5843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005845 mbedtls_ssl_send_alert_message( ssl,
5846 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5847 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005849 }
5850
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005851 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005852#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5853 if( crt_cnt++ == 0 &&
5854 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5855 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005856 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005857 /* During client-side renegotiation, check that the server's
5858 * end-CRTs hasn't changed compared to the initial handshake,
5859 * mitigating the triple handshake attack. On success, reuse
5860 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005861 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5862 if( ssl_check_peer_crt_unchanged( ssl,
5863 &ssl->in_msg[i],
5864 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005865 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5867 mbedtls_ssl_send_alert_message( ssl,
5868 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5869 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5870 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005871 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005872
5873 /* Now we can safely free the original chain. */
5874 ssl_clear_peer_cert( ssl->session );
5875 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005876#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5877
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005878 /* Parse the next certificate in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005879 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005880 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005881 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005882 case 0: /*ok*/
5883 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5884 /* Ignore certificate with an unknown algorithm: maybe a
5885 prior certificate was already trusted. */
5886 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005887
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005888 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5889 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5890 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005891
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005892 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5893 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5894 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005895
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005896 default:
5897 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5898 crt_parse_der_failed:
5899 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5900 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5901 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005902 }
5903
5904 i += n;
5905 }
5906
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005907 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005908 return( 0 );
5909}
5910
Hanno Becker4a55f632019-02-05 12:49:06 +00005911#if defined(MBEDTLS_SSL_SRV_C)
5912static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5913{
5914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5915 return( -1 );
5916
5917#if defined(MBEDTLS_SSL_PROTO_SSL3)
5918 /*
5919 * Check if the client sent an empty certificate
5920 */
5921 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5922 {
5923 if( ssl->in_msglen == 2 &&
5924 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5925 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5926 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5927 {
5928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5929 return( 0 );
5930 }
5931
5932 return( -1 );
5933 }
5934#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5935
5936#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5937 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5938 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5939 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5940 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5941 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5942 {
5943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5944 return( 0 );
5945 }
5946
5947 return( -1 );
5948#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5949 MBEDTLS_SSL_PROTO_TLS1_2 */
5950}
5951#endif /* MBEDTLS_SSL_SRV_C */
5952
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005953/* Check if a certificate message is expected.
5954 * Return either
5955 * - SSL_CERTIFICATE_EXPECTED, or
5956 * - SSL_CERTIFICATE_SKIP
5957 * indicating whether a Certificate message is expected or not.
5958 */
5959#define SSL_CERTIFICATE_EXPECTED 0
5960#define SSL_CERTIFICATE_SKIP 1
5961static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5962 int authmode )
5963{
5964 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5965 ssl->transform_negotiate->ciphersuite_info;
5966
5967 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5968 return( SSL_CERTIFICATE_SKIP );
5969
5970#if defined(MBEDTLS_SSL_SRV_C)
5971 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5972 {
5973 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5974 return( SSL_CERTIFICATE_SKIP );
5975
5976 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5977 {
5978 /* NOTE: Is it intentional that we set verify_result
5979 * to SKIP_VERIFY on server-side only? */
5980 ssl->session_negotiate->verify_result =
5981 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5982 return( SSL_CERTIFICATE_SKIP );
5983 }
5984 }
5985#endif /* MBEDTLS_SSL_SRV_C */
5986
5987 return( SSL_CERTIFICATE_EXPECTED );
5988}
5989
Hanno Becker68636192019-02-05 14:36:34 +00005990static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5991 int authmode,
5992 mbedtls_x509_crt *chain,
5993 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005994{
Hanno Becker6bdfab22019-02-05 13:11:17 +00005995 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005996 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5997 ssl->transform_negotiate->ciphersuite_info;
Hanno Becker68636192019-02-05 14:36:34 +00005998 mbedtls_x509_crt *ca_chain;
5999 mbedtls_x509_crl *ca_crl;
6000
6001 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6002 return( 0 );
6003
6004#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6005 if( ssl->handshake->sni_ca_chain != NULL )
6006 {
6007 ca_chain = ssl->handshake->sni_ca_chain;
6008 ca_crl = ssl->handshake->sni_ca_crl;
6009 }
6010 else
6011#endif
6012 {
6013 ca_chain = ssl->conf->ca_chain;
6014 ca_crl = ssl->conf->ca_crl;
6015 }
6016
6017 /*
6018 * Main check: verify certificate
6019 */
6020 ret = mbedtls_x509_crt_verify_restartable(
6021 chain,
6022 ca_chain, ca_crl,
6023 ssl->conf->cert_profile,
6024 ssl->hostname,
6025 &ssl->session_negotiate->verify_result,
6026 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6027
6028 if( ret != 0 )
6029 {
6030 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6031 }
6032
6033#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6034 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6035 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6036#endif
6037
6038 /*
6039 * Secondary checks: always done, but change 'ret' only if it was 0
6040 */
6041
6042#if defined(MBEDTLS_ECP_C)
6043 {
6044 const mbedtls_pk_context *pk = &chain->pk;
6045
6046 /* If certificate uses an EC key, make sure the curve is OK */
6047 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6048 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6049 {
6050 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6051
6052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6053 if( ret == 0 )
6054 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6055 }
6056 }
6057#endif /* MBEDTLS_ECP_C */
6058
6059 if( mbedtls_ssl_check_cert_usage( chain,
6060 ciphersuite_info,
6061 ! ssl->conf->endpoint,
6062 &ssl->session_negotiate->verify_result ) != 0 )
6063 {
6064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6065 if( ret == 0 )
6066 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6067 }
6068
6069 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6070 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6071 * with details encoded in the verification flags. All other kinds
6072 * of error codes, including those from the user provided f_vrfy
6073 * functions, are treated as fatal and lead to a failure of
6074 * ssl_parse_certificate even if verification was optional. */
6075 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6076 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6077 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6078 {
6079 ret = 0;
6080 }
6081
6082 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6083 {
6084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6085 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6086 }
6087
6088 if( ret != 0 )
6089 {
6090 uint8_t alert;
6091
6092 /* The certificate may have been rejected for several reasons.
6093 Pick one and send the corresponding alert. Which alert to send
6094 may be a subject of debate in some cases. */
6095 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6096 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6097 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6098 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6099 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6100 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6101 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6102 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6103 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6104 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6105 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6106 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6107 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6108 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6109 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6110 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6111 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6112 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6113 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6114 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6115 else
6116 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6117 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6118 alert );
6119 }
6120
6121#if defined(MBEDTLS_DEBUG_C)
6122 if( ssl->session_negotiate->verify_result != 0 )
6123 {
6124 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6125 ssl->session_negotiate->verify_result ) );
6126 }
6127 else
6128 {
6129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6130 }
6131#endif /* MBEDTLS_DEBUG_C */
6132
6133 return( ret );
6134}
6135
6136int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6137{
6138 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006139 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006140#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6141 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6142 ? ssl->handshake->sni_authmode
6143 : ssl->conf->authmode;
6144#else
6145 const int authmode = ssl->conf->authmode;
6146#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006147 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006148
6149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6150
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006151 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6152 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006153 {
6154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006155 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006156 }
6157
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006158#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6159 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006160 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006161 {
6162 goto crt_verify;
6163 }
6164#endif
6165
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006166 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006167 {
6168 /* mbedtls_ssl_read_record may have sent an alert already. We
6169 let it decide whether to alert. */
6170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6171 return( ret );
6172 }
6173
Hanno Becker4a55f632019-02-05 12:49:06 +00006174#if defined(MBEDTLS_SSL_SRV_C)
6175 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6176 {
6177 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006178
6179 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006180 ret = 0;
6181 else
6182 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006183
Hanno Becker6bdfab22019-02-05 13:11:17 +00006184 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006185 }
6186#endif /* MBEDTLS_SSL_SRV_C */
6187
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006188 /* Clear existing peer CRT structure in case we tried to
6189 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006190 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006191 ssl->session_negotiate->peer_cert =
6192 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6193 if( ssl->session_negotiate->peer_cert == NULL )
6194 {
6195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6196 sizeof( mbedtls_x509_crt ) ) );
6197 mbedtls_ssl_send_alert_message( ssl,
6198 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6199 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6200 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6201 }
6202 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Hanno Becker7a955a02019-02-05 13:08:01 +00006203
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006204 ret = ssl_parse_certificate_chain( ssl, ssl->session_negotiate->peer_cert );
6205 if( ret != 0 )
Hanno Beckerfcd9e712019-02-05 14:35:46 +00006206 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006207
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006208#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6209 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006210 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006211
6212crt_verify:
6213 if( ssl->handshake->ecrs_enabled)
6214 rs_ctx = &ssl->handshake->ecrs_ctx;
6215#endif
6216
Hanno Becker68636192019-02-05 14:36:34 +00006217 ret = ssl_parse_certificate_verify( ssl, authmode,
6218 ssl->session_negotiate->peer_cert,
6219 rs_ctx );
6220 if( ret != 0 )
6221 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006224
Hanno Becker6bdfab22019-02-05 13:11:17 +00006225exit:
6226
6227 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 return( ret );
6229}
Hanno Becker21489932019-02-05 13:20:55 +00006230#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006233{
6234 int ret;
6235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 ssl->out_msglen = 1;
6240 ssl->out_msg[0] = 1;
6241
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 ssl->state++;
6243
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006244 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006245 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 return( ret );
6248 }
6249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006251
6252 return( 0 );
6253}
6254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006256{
6257 int ret;
6258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006260
Hanno Becker327c93b2018-08-15 13:56:18 +01006261 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006264 return( ret );
6265 }
6266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006270 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6271 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006273 }
6274
Hanno Beckere678eaa2018-08-21 14:57:46 +01006275 /* CCS records are only accepted if they have length 1 and content '1',
6276 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006277
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006278 /*
6279 * Switch to our negotiated transform and session parameters for inbound
6280 * data.
6281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006283 ssl->transform_in = ssl->transform_negotiate;
6284 ssl->session_in = ssl->session_negotiate;
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006287 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006290 ssl_dtls_replay_reset( ssl );
6291#endif
6292
6293 /* Increment epoch */
6294 if( ++ssl->in_epoch == 0 )
6295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006297 /* This is highly unlikely to happen for legitimate reasons, so
6298 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006300 }
6301 }
6302 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006304 memset( ssl->in_ctr, 0, 8 );
6305
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006306 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6309 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006314 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6315 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006317 }
6318 }
6319#endif
6320
Paul Bakker5121ce52009-01-03 21:22:43 +00006321 ssl->state++;
6322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006324
6325 return( 0 );
6326}
6327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6329 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006330{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006331 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6334 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6335 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006336 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006337 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6340#if defined(MBEDTLS_SHA512_C)
6341 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006342 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6343 else
6344#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345#if defined(MBEDTLS_SHA256_C)
6346 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006347 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006348 else
6349#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006353 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006354 }
Paul Bakker380da532012-04-18 16:10:25 +00006355}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6360 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006361 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6362 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6365#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006367 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006368 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6369#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006370 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006371#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006374#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006375 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006376 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006377#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006378 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006379#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006382}
6383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006385 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6388 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006389 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6390 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6393#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006394#if defined(MBEDTLS_USE_PSA_CRYPTO)
6395 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6396#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006397 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006398#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006401#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006402 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006403#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006404 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006405#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006408}
6409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6411 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6412static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006413 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006414{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006415 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6416 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006417}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006418#endif
Paul Bakker380da532012-04-18 16:10:25 +00006419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6421#if defined(MBEDTLS_SHA256_C)
6422static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006423 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006424{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006425#if defined(MBEDTLS_USE_PSA_CRYPTO)
6426 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6427#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006428 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006429#endif
Paul Bakker380da532012-04-18 16:10:25 +00006430}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006431#endif
Paul Bakker380da532012-04-18 16:10:25 +00006432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006433#if defined(MBEDTLS_SHA512_C)
6434static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006435 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006436{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006437#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006438 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006439#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006440 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006441#endif
Paul Bakker380da532012-04-18 16:10:25 +00006442}
Paul Bakker769075d2012-11-24 11:26:46 +01006443#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006447static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006449{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006450 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006451 mbedtls_md5_context md5;
6452 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006453
Paul Bakker5121ce52009-01-03 21:22:43 +00006454 unsigned char padbuf[48];
6455 unsigned char md5sum[16];
6456 unsigned char sha1sum[20];
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006459 if( !session )
6460 session = ssl->session;
6461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006463
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006464 mbedtls_md5_init( &md5 );
6465 mbedtls_sha1_init( &sha1 );
6466
6467 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6468 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006469
6470 /*
6471 * SSLv3:
6472 * hash =
6473 * MD5( master + pad2 +
6474 * MD5( handshake + sender + master + pad1 ) )
6475 * + SHA1( master + pad2 +
6476 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006477 */
6478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006480 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6481 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006482#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006485 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6486 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006487#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006490 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006491
Paul Bakker1ef83d62012-04-11 12:09:53 +00006492 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006493
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006494 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6495 mbedtls_md5_update_ret( &md5, session->master, 48 );
6496 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6497 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006498
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006499 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6500 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6501 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6502 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006503
Paul Bakker1ef83d62012-04-11 12:09:53 +00006504 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006505
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006506 mbedtls_md5_starts_ret( &md5 );
6507 mbedtls_md5_update_ret( &md5, session->master, 48 );
6508 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6509 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6510 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006511
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006512 mbedtls_sha1_starts_ret( &sha1 );
6513 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6514 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6515 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6516 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006519
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006520 mbedtls_md5_free( &md5 );
6521 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006522
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006523 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6524 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6525 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006532static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006534{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006535 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006536 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006537 mbedtls_md5_context md5;
6538 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006539 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006542 if( !session )
6543 session = ssl->session;
6544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006546
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006547 mbedtls_md5_init( &md5 );
6548 mbedtls_sha1_init( &sha1 );
6549
6550 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6551 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006552
Paul Bakker1ef83d62012-04-11 12:09:53 +00006553 /*
6554 * TLSv1:
6555 * hash = PRF( master, finished_label,
6556 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6557 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006560 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6561 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006562#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006565 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6566 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006567#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006570 ? "client finished"
6571 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006572
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006573 mbedtls_md5_finish_ret( &md5, padbuf );
6574 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006575
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006576 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006577 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006580
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006581 mbedtls_md5_free( &md5 );
6582 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006583
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006584 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6591#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006592static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006594{
6595 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006596 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006597 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006598#if defined(MBEDTLS_USE_PSA_CRYPTO)
6599 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006600 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006601 psa_status_t status;
6602#else
6603 mbedtls_sha256_context sha256;
6604#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006607 if( !session )
6608 session = ssl->session;
6609
Andrzej Kurekeb342242019-01-29 09:14:33 -05006610 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6611 ? "client finished"
6612 : "server finished";
6613
6614#if defined(MBEDTLS_USE_PSA_CRYPTO)
6615 sha256_psa = psa_hash_operation_init();
6616
6617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6618
6619 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6620 if( status != PSA_SUCCESS )
6621 {
6622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6623 return;
6624 }
6625
6626 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6627 if( status != PSA_SUCCESS )
6628 {
6629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6630 return;
6631 }
6632 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6633#else
6634
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006635 mbedtls_sha256_init( &sha256 );
6636
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006638
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006639 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006640
6641 /*
6642 * TLSv1.2:
6643 * hash = PRF( master, finished_label,
6644 * Hash( handshake ) )[0.11]
6645 */
6646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#if !defined(MBEDTLS_SHA256_ALT)
6648 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006649 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006650#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006651
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006652 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006653 mbedtls_sha256_free( &sha256 );
6654#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006655
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006656 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006657 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006660
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006661 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006668static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006670{
6671 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006672 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006673 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006674#if defined(MBEDTLS_USE_PSA_CRYPTO)
6675 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006676 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006677 psa_status_t status;
6678#else
6679 mbedtls_sha512_context sha512;
6680#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006683 if( !session )
6684 session = ssl->session;
6685
Andrzej Kurekeb342242019-01-29 09:14:33 -05006686 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6687 ? "client finished"
6688 : "server finished";
6689
6690#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006691 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006692
6693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6694
Andrzej Kurek972fba52019-01-30 03:29:12 -05006695 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006696 if( status != PSA_SUCCESS )
6697 {
6698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6699 return;
6700 }
6701
Andrzej Kurek972fba52019-01-30 03:29:12 -05006702 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006703 if( status != PSA_SUCCESS )
6704 {
6705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6706 return;
6707 }
6708 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6709#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006710 mbedtls_sha512_init( &sha512 );
6711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006713
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006714 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006715
6716 /*
6717 * TLSv1.2:
6718 * hash = PRF( master, finished_label,
6719 * Hash( handshake ) )[0.11]
6720 */
6721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006723 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6724 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006725#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006726
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006727 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006728 mbedtls_sha512_free( &sha512 );
6729#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006730
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006731 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006732 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006735
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006736 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#endif /* MBEDTLS_SHA512_C */
6741#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006746
6747 /*
6748 * Free our handshake params
6749 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006750 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006752 ssl->handshake = NULL;
6753
6754 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006755 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006756 */
6757 if( ssl->transform )
6758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 mbedtls_ssl_transform_free( ssl->transform );
6760 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006761 }
6762 ssl->transform = ssl->transform_negotiate;
6763 ssl->transform_negotiate = NULL;
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006766}
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006769{
6770 int resume = ssl->handshake->resume;
6771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774#if defined(MBEDTLS_SSL_RENEGOTIATION)
6775 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006778 ssl->renego_records_seen = 0;
6779 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006780#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006781
6782 /*
6783 * Free the previous session and switch in the current one
6784 */
Paul Bakker0a597072012-09-25 21:55:46 +00006785 if( ssl->session )
6786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006788 /* RFC 7366 3.1: keep the EtM state */
6789 ssl->session_negotiate->encrypt_then_mac =
6790 ssl->session->encrypt_then_mac;
6791#endif
6792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 mbedtls_ssl_session_free( ssl->session );
6794 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006795 }
6796 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006797 ssl->session_negotiate = NULL;
6798
Paul Bakker0a597072012-09-25 21:55:46 +00006799 /*
6800 * Add cache entry
6801 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006802 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006803 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006804 resume == 0 )
6805 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006806 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006808 }
Paul Bakker0a597072012-09-25 21:55:46 +00006809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006811 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006812 ssl->handshake->flight != NULL )
6813 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006814 /* Cancel handshake timer */
6815 ssl_set_timer( ssl, 0 );
6816
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006817 /* Keep last flight around in case we need to resend it:
6818 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006820 }
6821 else
6822#endif
6823 ssl_handshake_wrapup_free_hs_transform( ssl );
6824
Paul Bakker48916f92012-09-16 19:57:18 +00006825 ssl->state++;
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006828}
6829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006831{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006832 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006835
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006836 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006837
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006838 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006840 /*
6841 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6842 * may define some other value. Currently (early 2016), no defined
6843 * ciphersuite does this (and this is unlikely to change as activity has
6844 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006849 ssl->verify_data_len = hash_len;
6850 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006851#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006852
Paul Bakker5121ce52009-01-03 21:22:43 +00006853 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6855 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
6857 /*
6858 * In case of session resuming, invert the client and server
6859 * ChangeCipherSpec messages order.
6860 */
Paul Bakker0a597072012-09-25 21:55:46 +00006861 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006864 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006870#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006871 }
6872 else
6873 ssl->state++;
6874
Paul Bakker48916f92012-09-16 19:57:18 +00006875 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006876 * Switch to our negotiated transform and session parameters for outbound
6877 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006883 {
6884 unsigned char i;
6885
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006886 /* Remember current epoch settings for resending */
6887 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006888 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006889
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006890 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006891 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006892
6893 /* Increment epoch */
6894 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006895 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006896 break;
6897
6898 /* The loop goes to its end iff the counter is wrapping */
6899 if( i == 0 )
6900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6902 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006903 }
6904 }
6905 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006907 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006908
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006909 ssl->transform_out = ssl->transform_negotiate;
6910 ssl->session_out = ssl->session_negotiate;
6911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6913 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6918 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006919 }
6920 }
6921#endif
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006924 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006926#endif
6927
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006928 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006931 return( ret );
6932 }
6933
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006934#if defined(MBEDTLS_SSL_PROTO_DTLS)
6935 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6936 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6937 {
6938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6939 return( ret );
6940 }
6941#endif
6942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006944
6945 return( 0 );
6946}
6947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006949#define SSL_MAX_HASH_LEN 36
6950#else
6951#define SSL_MAX_HASH_LEN 12
6952#endif
6953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006955{
Paul Bakker23986e52011-04-24 08:57:21 +00006956 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006957 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006958 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006961
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006962 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006963
Hanno Becker327c93b2018-08-15 13:56:18 +01006964 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006967 return( ret );
6968 }
6969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006973 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6974 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 }
6977
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006978 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_SSL_PROTO_SSL3)
6980 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006981 hash_len = 36;
6982 else
6983#endif
6984 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6987 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006990 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6991 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006993 }
6994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006996 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006999 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7000 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007002 }
7003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007005 ssl->verify_data_len = hash_len;
7006 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007007#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007008
Paul Bakker0a597072012-09-25 21:55:46 +00007009 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007012 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007016 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007018#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007019 }
7020 else
7021 ssl->state++;
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007026#endif
7027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007029
7030 return( 0 );
7031}
7032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7038 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7039 mbedtls_md5_init( &handshake->fin_md5 );
7040 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007041 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7042 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7045#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007046#if defined(MBEDTLS_USE_PSA_CRYPTO)
7047 handshake->fin_sha256_psa = psa_hash_operation_init();
7048 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007051 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007052#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007055#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007056 handshake->fin_sha384_psa = psa_hash_operation_init();
7057 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007058#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007060 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007061#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007064
7065 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007066
7067#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7068 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7069 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7070#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072#if defined(MBEDTLS_DHM_C)
7073 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075#if defined(MBEDTLS_ECDH_C)
7076 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007077#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007078#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007079 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007080#if defined(MBEDTLS_SSL_CLI_C)
7081 handshake->ecjpake_cache = NULL;
7082 handshake->ecjpake_cache_len = 0;
7083#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007084#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007085
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007086#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007087 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007088#endif
7089
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007090#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7091 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7092#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007093}
7094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7100 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 mbedtls_md_init( &transform->md_ctx_enc );
7103 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007104}
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007109}
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007112{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007113 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007114 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007116 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007118 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007119 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007120
7121 /*
7122 * Either the pointers are now NULL or cleared properly and can be freed.
7123 * Now allocate missing structures.
7124 */
7125 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007126 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007127 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007128 }
Paul Bakker48916f92012-09-16 19:57:18 +00007129
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007130 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007131 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007132 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007133 }
Paul Bakker48916f92012-09-16 19:57:18 +00007134
Paul Bakker82788fb2014-10-20 13:59:19 +02007135 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007136 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007137 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007138 }
Paul Bakker48916f92012-09-16 19:57:18 +00007139
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007140 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007141 if( ssl->handshake == NULL ||
7142 ssl->transform_negotiate == NULL ||
7143 ssl->session_negotiate == NULL )
7144 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 mbedtls_free( ssl->handshake );
7148 mbedtls_free( ssl->transform_negotiate );
7149 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007150
7151 ssl->handshake = NULL;
7152 ssl->transform_negotiate = NULL;
7153 ssl->session_negotiate = NULL;
7154
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007155 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007156 }
7157
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007158 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007160 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007161 ssl_handshake_params_init( ssl->handshake );
7162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007164 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7165 {
7166 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007167
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007168 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7169 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7170 else
7171 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007172
7173 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007174 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007175#endif
7176
Paul Bakker48916f92012-09-16 19:57:18 +00007177 return( 0 );
7178}
7179
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007180#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007181/* Dummy cookie callbacks for defaults */
7182static int ssl_cookie_write_dummy( void *ctx,
7183 unsigned char **p, unsigned char *end,
7184 const unsigned char *cli_id, size_t cli_id_len )
7185{
7186 ((void) ctx);
7187 ((void) p);
7188 ((void) end);
7189 ((void) cli_id);
7190 ((void) cli_id_len);
7191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007193}
7194
7195static int ssl_cookie_check_dummy( void *ctx,
7196 const unsigned char *cookie, size_t cookie_len,
7197 const unsigned char *cli_id, size_t cli_id_len )
7198{
7199 ((void) ctx);
7200 ((void) cookie);
7201 ((void) cookie_len);
7202 ((void) cli_id);
7203 ((void) cli_id_len);
7204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007206}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007207#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007208
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007209/* Once ssl->out_hdr as the address of the beginning of the
7210 * next outgoing record is set, deduce the other pointers.
7211 *
7212 * Note: For TLS, we save the implicit record sequence number
7213 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7214 * and the caller has to make sure there's space for this.
7215 */
7216
7217static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7218 mbedtls_ssl_transform *transform )
7219{
7220#if defined(MBEDTLS_SSL_PROTO_DTLS)
7221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7222 {
7223 ssl->out_ctr = ssl->out_hdr + 3;
7224 ssl->out_len = ssl->out_hdr + 11;
7225 ssl->out_iv = ssl->out_hdr + 13;
7226 }
7227 else
7228#endif
7229 {
7230 ssl->out_ctr = ssl->out_hdr - 8;
7231 ssl->out_len = ssl->out_hdr + 3;
7232 ssl->out_iv = ssl->out_hdr + 5;
7233 }
7234
7235 /* Adjust out_msg to make space for explicit IV, if used. */
7236 if( transform != NULL &&
7237 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7238 {
7239 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7240 }
7241 else
7242 ssl->out_msg = ssl->out_iv;
7243}
7244
7245/* Once ssl->in_hdr as the address of the beginning of the
7246 * next incoming record is set, deduce the other pointers.
7247 *
7248 * Note: For TLS, we save the implicit record sequence number
7249 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7250 * and the caller has to make sure there's space for this.
7251 */
7252
7253static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7254 mbedtls_ssl_transform *transform )
7255{
7256#if defined(MBEDTLS_SSL_PROTO_DTLS)
7257 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7258 {
7259 ssl->in_ctr = ssl->in_hdr + 3;
7260 ssl->in_len = ssl->in_hdr + 11;
7261 ssl->in_iv = ssl->in_hdr + 13;
7262 }
7263 else
7264#endif
7265 {
7266 ssl->in_ctr = ssl->in_hdr - 8;
7267 ssl->in_len = ssl->in_hdr + 3;
7268 ssl->in_iv = ssl->in_hdr + 5;
7269 }
7270
7271 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7272 if( transform != NULL &&
7273 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7274 {
7275 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7276 }
7277 else
7278 ssl->in_msg = ssl->in_iv;
7279}
7280
Paul Bakker5121ce52009-01-03 21:22:43 +00007281/*
7282 * Initialize an SSL context
7283 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007284void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7285{
7286 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7287}
7288
7289/*
7290 * Setup an SSL context
7291 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007292
7293static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7294{
7295 /* Set the incoming and outgoing record pointers. */
7296#if defined(MBEDTLS_SSL_PROTO_DTLS)
7297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7298 {
7299 ssl->out_hdr = ssl->out_buf;
7300 ssl->in_hdr = ssl->in_buf;
7301 }
7302 else
7303#endif /* MBEDTLS_SSL_PROTO_DTLS */
7304 {
7305 ssl->out_hdr = ssl->out_buf + 8;
7306 ssl->in_hdr = ssl->in_buf + 8;
7307 }
7308
7309 /* Derive other internal pointers. */
7310 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7311 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7312}
7313
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007314int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007315 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007316{
Paul Bakker48916f92012-09-16 19:57:18 +00007317 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007318
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007319 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007320
7321 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007322 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007323 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007324
7325 /* Set to NULL in case of an error condition */
7326 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007327
Angus Grattond8213d02016-05-25 20:56:48 +10007328 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7329 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007330 {
Angus Grattond8213d02016-05-25 20:56:48 +10007331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007332 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007333 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007334 }
7335
7336 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7337 if( ssl->out_buf == NULL )
7338 {
7339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007340 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007341 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007342 }
7343
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007344 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007345
Paul Bakker48916f92012-09-16 19:57:18 +00007346 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007347 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
7349 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007350
7351error:
7352 mbedtls_free( ssl->in_buf );
7353 mbedtls_free( ssl->out_buf );
7354
7355 ssl->conf = NULL;
7356
7357 ssl->in_buf = NULL;
7358 ssl->out_buf = NULL;
7359
7360 ssl->in_hdr = NULL;
7361 ssl->in_ctr = NULL;
7362 ssl->in_len = NULL;
7363 ssl->in_iv = NULL;
7364 ssl->in_msg = NULL;
7365
7366 ssl->out_hdr = NULL;
7367 ssl->out_ctr = NULL;
7368 ssl->out_len = NULL;
7369 ssl->out_iv = NULL;
7370 ssl->out_msg = NULL;
7371
k-stachowiak9f7798e2018-07-31 16:52:32 +02007372 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007373}
7374
7375/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007376 * Reset an initialized and used SSL context for re-use while retaining
7377 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007378 *
7379 * If partial is non-zero, keep data in the input buffer and client ID.
7380 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007381 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007382static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007383{
Paul Bakker48916f92012-09-16 19:57:18 +00007384 int ret;
7385
Hanno Becker7e772132018-08-10 12:38:21 +01007386#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7387 !defined(MBEDTLS_SSL_SRV_C)
7388 ((void) partial);
7389#endif
7390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007392
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007393 /* Cancel any possibly running timer */
7394 ssl_set_timer( ssl, 0 );
7395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396#if defined(MBEDTLS_SSL_RENEGOTIATION)
7397 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007398 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007399
7400 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7402 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007405
Paul Bakker7eb013f2011-10-06 12:37:39 +00007406 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007407 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007408
7409 ssl->in_msgtype = 0;
7410 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007412 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007413 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007416 ssl_dtls_replay_reset( ssl );
7417#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007418
7419 ssl->in_hslen = 0;
7420 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007421
7422 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007423
7424 ssl->out_msgtype = 0;
7425 ssl->out_msglen = 0;
7426 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7428 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007429 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007430#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007431
Hanno Becker19859472018-08-06 09:40:20 +01007432 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7433
Paul Bakker48916f92012-09-16 19:57:18 +00007434 ssl->transform_in = NULL;
7435 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007436
Hanno Becker78640902018-08-13 16:35:15 +01007437 ssl->session_in = NULL;
7438 ssl->session_out = NULL;
7439
Angus Grattond8213d02016-05-25 20:56:48 +10007440 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007441
7442#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007443 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007444#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7445 {
7446 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007447 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007448 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7451 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7454 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7457 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007458 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007459 }
7460#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007461
Paul Bakker48916f92012-09-16 19:57:18 +00007462 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 mbedtls_ssl_transform_free( ssl->transform );
7465 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007466 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007467 }
Paul Bakker48916f92012-09-16 19:57:18 +00007468
Paul Bakkerc0463502013-02-14 11:19:38 +01007469 if( ssl->session )
7470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 mbedtls_ssl_session_free( ssl->session );
7472 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007473 ssl->session = NULL;
7474 }
7475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007477 ssl->alpn_chosen = NULL;
7478#endif
7479
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007480#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007481#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007482 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007483#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007484 {
7485 mbedtls_free( ssl->cli_id );
7486 ssl->cli_id = NULL;
7487 ssl->cli_id_len = 0;
7488 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007489#endif
7490
Paul Bakker48916f92012-09-16 19:57:18 +00007491 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7492 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007493
7494 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007495}
7496
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007497/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007498 * Reset an initialized and used SSL context for re-use while retaining
7499 * all application-set variables, function pointers and data.
7500 */
7501int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7502{
7503 return( ssl_session_reset_int( ssl, 0 ) );
7504}
7505
7506/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 * SSL set accessors
7508 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007509void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007510{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007511 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007512}
7513
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007514void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007515{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007516 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007517}
7518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007520void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007521{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007522 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007523}
7524#endif
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007527void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007529 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007530}
7531#endif
7532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007534
Hanno Becker1841b0a2018-08-24 11:13:57 +01007535void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7536 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007537{
7538 ssl->disable_datagram_packing = !allow_packing;
7539}
7540
7541void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7542 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007544 conf->hs_timeout_min = min;
7545 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007546}
7547#endif
7548
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007549void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007551 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007552}
7553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007555void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007556 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007557 void *p_vrfy )
7558{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007559 conf->f_vrfy = f_vrfy;
7560 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007563
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007564void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007565 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007566 void *p_rng )
7567{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007568 conf->f_rng = f_rng;
7569 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007570}
7571
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007572void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007573 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007574 void *p_dbg )
7575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007576 conf->f_dbg = f_dbg;
7577 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007578}
7579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007581 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007582 mbedtls_ssl_send_t *f_send,
7583 mbedtls_ssl_recv_t *f_recv,
7584 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007585{
7586 ssl->p_bio = p_bio;
7587 ssl->f_send = f_send;
7588 ssl->f_recv = f_recv;
7589 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007590}
7591
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007592#if defined(MBEDTLS_SSL_PROTO_DTLS)
7593void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7594{
7595 ssl->mtu = mtu;
7596}
7597#endif
7598
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007599void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007600{
7601 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007602}
7603
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007604void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7605 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007606 mbedtls_ssl_set_timer_t *f_set_timer,
7607 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007608{
7609 ssl->p_timer = p_timer;
7610 ssl->f_set_timer = f_set_timer;
7611 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007612
7613 /* Make sure we start with no timer running */
7614 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007615}
7616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007618void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007619 void *p_cache,
7620 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7621 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007622{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007623 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007624 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007625 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#if defined(MBEDTLS_SSL_CLI_C)
7630int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007631{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007632 int ret;
7633
7634 if( ssl == NULL ||
7635 session == NULL ||
7636 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007637 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007640 }
7641
Hanno Becker52055ae2019-02-06 14:30:46 +00007642 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7643 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007644 return( ret );
7645
Paul Bakker0a597072012-09-25 21:55:46 +00007646 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007647
7648 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007651
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007652void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007653 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007655 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7656 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7657 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7658 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007659}
7660
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007661void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007662 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007663 int major, int minor )
7664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007666 return;
7667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007669 return;
7670
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007671 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007672}
7673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007675void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007676 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007677{
7678 conf->cert_profile = profile;
7679}
7680
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007681/* Append a new keycert entry to a (possibly empty) list */
7682static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7683 mbedtls_x509_crt *cert,
7684 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007685{
niisato8ee24222018-06-25 19:05:48 +09007686 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007687
niisato8ee24222018-06-25 19:05:48 +09007688 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7689 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007690 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007691
niisato8ee24222018-06-25 19:05:48 +09007692 new_cert->cert = cert;
7693 new_cert->key = key;
7694 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007695
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007696 /* Update head is the list was null, else add to the end */
7697 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007698 {
niisato8ee24222018-06-25 19:05:48 +09007699 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007700 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007701 else
7702 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007703 mbedtls_ssl_key_cert *cur = *head;
7704 while( cur->next != NULL )
7705 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007706 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007707 }
7708
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007709 return( 0 );
7710}
7711
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007712int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007713 mbedtls_x509_crt *own_cert,
7714 mbedtls_pk_context *pk_key )
7715{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007716 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007717}
7718
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007719void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007720 mbedtls_x509_crt *ca_chain,
7721 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007722{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007723 conf->ca_chain = ca_chain;
7724 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007727
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007728#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7729int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7730 mbedtls_x509_crt *own_cert,
7731 mbedtls_pk_context *pk_key )
7732{
7733 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7734 own_cert, pk_key ) );
7735}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007736
7737void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7738 mbedtls_x509_crt *ca_chain,
7739 mbedtls_x509_crl *ca_crl )
7740{
7741 ssl->handshake->sni_ca_chain = ca_chain;
7742 ssl->handshake->sni_ca_crl = ca_crl;
7743}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007744
7745void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7746 int authmode )
7747{
7748 ssl->handshake->sni_authmode = authmode;
7749}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007750#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7751
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007752#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007753/*
7754 * Set EC J-PAKE password for current handshake
7755 */
7756int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7757 const unsigned char *pw,
7758 size_t pw_len )
7759{
7760 mbedtls_ecjpake_role role;
7761
Janos Follath8eb64132016-06-03 15:40:57 +01007762 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7764
7765 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7766 role = MBEDTLS_ECJPAKE_SERVER;
7767 else
7768 role = MBEDTLS_ECJPAKE_CLIENT;
7769
7770 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7771 role,
7772 MBEDTLS_MD_SHA256,
7773 MBEDTLS_ECP_DP_SECP256R1,
7774 pw, pw_len ) );
7775}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007776#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007779
7780static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7781{
7782 /* Remove reference to existing PSK, if any. */
7783#if defined(MBEDTLS_USE_PSA_CRYPTO)
7784 if( conf->psk_opaque != 0 )
7785 {
7786 /* The maintenance of the PSK key slot is the
7787 * user's responsibility. */
7788 conf->psk_opaque = 0;
7789 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007790 /* This and the following branch should never
7791 * be taken simultaenously as we maintain the
7792 * invariant that raw and opaque PSKs are never
7793 * configured simultaneously. As a safeguard,
7794 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007795#endif /* MBEDTLS_USE_PSA_CRYPTO */
7796 if( conf->psk != NULL )
7797 {
7798 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7799
7800 mbedtls_free( conf->psk );
7801 conf->psk = NULL;
7802 conf->psk_len = 0;
7803 }
7804
7805 /* Remove reference to PSK identity, if any. */
7806 if( conf->psk_identity != NULL )
7807 {
7808 mbedtls_free( conf->psk_identity );
7809 conf->psk_identity = NULL;
7810 conf->psk_identity_len = 0;
7811 }
7812}
7813
Hanno Becker7390c712018-11-15 13:33:04 +00007814/* This function assumes that PSK identity in the SSL config is unset.
7815 * It checks that the provided identity is well-formed and attempts
7816 * to make a copy of it in the SSL config.
7817 * On failure, the PSK identity in the config remains unset. */
7818static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7819 unsigned char const *psk_identity,
7820 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007821{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007822 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007823 if( psk_identity == NULL ||
7824 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007825 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007826 {
7827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7828 }
7829
Hanno Becker7390c712018-11-15 13:33:04 +00007830 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7831 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007832 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007833
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007834 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007835 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007836
7837 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007838}
7839
Hanno Becker7390c712018-11-15 13:33:04 +00007840int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7841 const unsigned char *psk, size_t psk_len,
7842 const unsigned char *psk_identity, size_t psk_identity_len )
7843{
7844 int ret;
7845 /* Remove opaque/raw PSK + PSK Identity */
7846 ssl_conf_remove_psk( conf );
7847
7848 /* Check and set raw PSK */
7849 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7851 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7852 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7853 conf->psk_len = psk_len;
7854 memcpy( conf->psk, psk, conf->psk_len );
7855
7856 /* Check and set PSK Identity */
7857 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7858 if( ret != 0 )
7859 ssl_conf_remove_psk( conf );
7860
7861 return( ret );
7862}
7863
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007864static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7865{
7866#if defined(MBEDTLS_USE_PSA_CRYPTO)
7867 if( ssl->handshake->psk_opaque != 0 )
7868 {
7869 ssl->handshake->psk_opaque = 0;
7870 }
7871 else
7872#endif /* MBEDTLS_USE_PSA_CRYPTO */
7873 if( ssl->handshake->psk != NULL )
7874 {
7875 mbedtls_platform_zeroize( ssl->handshake->psk,
7876 ssl->handshake->psk_len );
7877 mbedtls_free( ssl->handshake->psk );
7878 ssl->handshake->psk_len = 0;
7879 }
7880}
7881
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007882int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7883 const unsigned char *psk, size_t psk_len )
7884{
7885 if( psk == NULL || ssl->handshake == NULL )
7886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7887
7888 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7890
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007891 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007892
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007893 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007894 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007895
7896 ssl->handshake->psk_len = psk_len;
7897 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7898
7899 return( 0 );
7900}
7901
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007902#if defined(MBEDTLS_USE_PSA_CRYPTO)
7903int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007904 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007905 const unsigned char *psk_identity,
7906 size_t psk_identity_len )
7907{
Hanno Becker7390c712018-11-15 13:33:04 +00007908 int ret;
7909 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007910 ssl_conf_remove_psk( conf );
7911
Hanno Becker7390c712018-11-15 13:33:04 +00007912 /* Check and set opaque PSK */
7913 if( psk_slot == 0 )
7914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007915 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007916
7917 /* Check and set PSK Identity */
7918 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7919 psk_identity_len );
7920 if( ret != 0 )
7921 ssl_conf_remove_psk( conf );
7922
7923 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007924}
7925
7926int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007927 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007928{
7929 if( psk_slot == 0 || ssl->handshake == NULL )
7930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7931
7932 ssl_remove_psk( ssl );
7933 ssl->handshake->psk_opaque = psk_slot;
7934 return( 0 );
7935}
7936#endif /* MBEDTLS_USE_PSA_CRYPTO */
7937
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007938void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007940 size_t),
7941 void *p_psk )
7942{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007943 conf->f_psk = f_psk;
7944 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007947
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007948#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007949
7950#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007951int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007952{
7953 int ret;
7954
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007955 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7956 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7957 {
7958 mbedtls_mpi_free( &conf->dhm_P );
7959 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007960 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007962
7963 return( 0 );
7964}
Hanno Becker470a8c42017-10-04 15:28:46 +01007965#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007966
Hanno Beckera90658f2017-10-04 15:29:08 +01007967int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7968 const unsigned char *dhm_P, size_t P_len,
7969 const unsigned char *dhm_G, size_t G_len )
7970{
7971 int ret;
7972
7973 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7974 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7975 {
7976 mbedtls_mpi_free( &conf->dhm_P );
7977 mbedtls_mpi_free( &conf->dhm_G );
7978 return( ret );
7979 }
7980
7981 return( 0 );
7982}
Paul Bakker5121ce52009-01-03 21:22:43 +00007983
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007984int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007985{
7986 int ret;
7987
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007988 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7989 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7990 {
7991 mbedtls_mpi_free( &conf->dhm_P );
7992 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007993 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007994 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007995
7996 return( 0 );
7997}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007998#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007999
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008000#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8001/*
8002 * Set the minimum length for Diffie-Hellman parameters
8003 */
8004void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8005 unsigned int bitlen )
8006{
8007 conf->dhm_min_bitlen = bitlen;
8008}
8009#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8010
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008011#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008012/*
8013 * Set allowed/preferred hashes for handshake signatures
8014 */
8015void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8016 const int *hashes )
8017{
8018 conf->sig_hashes = hashes;
8019}
Hanno Becker947194e2017-04-07 13:25:49 +01008020#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008021
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008022#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008023/*
8024 * Set the allowed elliptic curves
8025 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008026void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008027 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008028{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008029 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008030}
Hanno Becker947194e2017-04-07 13:25:49 +01008031#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008032
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008033#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008035{
Hanno Becker947194e2017-04-07 13:25:49 +01008036 /* Initialize to suppress unnecessary compiler warning */
8037 size_t hostname_len = 0;
8038
8039 /* Check if new hostname is valid before
8040 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008041 if( hostname != NULL )
8042 {
8043 hostname_len = strlen( hostname );
8044
8045 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8046 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8047 }
8048
8049 /* Now it's clear that we will overwrite the old hostname,
8050 * so we can free it safely */
8051
8052 if( ssl->hostname != NULL )
8053 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008054 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008055 mbedtls_free( ssl->hostname );
8056 }
8057
8058 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008059
Paul Bakker5121ce52009-01-03 21:22:43 +00008060 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008061 {
8062 ssl->hostname = NULL;
8063 }
8064 else
8065 {
8066 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008067 if( ssl->hostname == NULL )
8068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008069
Hanno Becker947194e2017-04-07 13:25:49 +01008070 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008071
Hanno Becker947194e2017-04-07 13:25:49 +01008072 ssl->hostname[hostname_len] = '\0';
8073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008074
8075 return( 0 );
8076}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008077#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008078
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008079#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008080void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008082 const unsigned char *, size_t),
8083 void *p_sni )
8084{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008085 conf->f_sni = f_sni;
8086 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008091int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008092{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008093 size_t cur_len, tot_len;
8094 const char **p;
8095
8096 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008097 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8098 * MUST NOT be truncated."
8099 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008100 */
8101 tot_len = 0;
8102 for( p = protos; *p != NULL; p++ )
8103 {
8104 cur_len = strlen( *p );
8105 tot_len += cur_len;
8106
8107 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008109 }
8110
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008111 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008112
8113 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008114}
8115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008117{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008118 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008121
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008122void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008124 conf->max_major_ver = major;
8125 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008126}
8127
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008128void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008130 conf->min_major_ver = major;
8131 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008132}
8133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008135void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008136{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008137 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008138}
8139#endif
8140
Janos Follath088ce432017-04-10 12:42:31 +01008141#if defined(MBEDTLS_SSL_SRV_C)
8142void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8143 char cert_req_ca_list )
8144{
8145 conf->cert_req_ca_list = cert_req_ca_list;
8146}
8147#endif
8148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008150void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008152 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008153}
8154#endif
8155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008157void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008158{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008159 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008160}
8161#endif
8162
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008163#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008164void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008165{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008166 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008167}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008168#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008171int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008174 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008177 }
8178
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008179 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008180
8181 return( 0 );
8182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008186void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008187{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008188 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008193void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008194{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008195 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008196}
8197#endif
8198
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008199void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008200{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008201 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008202}
8203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008205void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008207 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008208}
8209
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008210void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008211{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008212 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008213}
8214
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008215void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008216 const unsigned char period[8] )
8217{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008218 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008223#if defined(MBEDTLS_SSL_CLI_C)
8224void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008225{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008226 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008227}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008228#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008229
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008230#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008231void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8232 mbedtls_ssl_ticket_write_t *f_ticket_write,
8233 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8234 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008235{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008236 conf->f_ticket_write = f_ticket_write;
8237 conf->f_ticket_parse = f_ticket_parse;
8238 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008239}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008242
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008243#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8244void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8245 mbedtls_ssl_export_keys_t *f_export_keys,
8246 void *p_export_keys )
8247{
8248 conf->f_export_keys = f_export_keys;
8249 conf->p_export_keys = p_export_keys;
8250}
8251#endif
8252
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008253#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008254void mbedtls_ssl_conf_async_private_cb(
8255 mbedtls_ssl_config *conf,
8256 mbedtls_ssl_async_sign_t *f_async_sign,
8257 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8258 mbedtls_ssl_async_resume_t *f_async_resume,
8259 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008260 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008261{
8262 conf->f_async_sign_start = f_async_sign;
8263 conf->f_async_decrypt_start = f_async_decrypt;
8264 conf->f_async_resume = f_async_resume;
8265 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008266 conf->p_async_config_data = async_config_data;
8267}
8268
Gilles Peskine8f97af72018-04-26 11:46:10 +02008269void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8270{
8271 return( conf->p_async_config_data );
8272}
8273
Gilles Peskine1febfef2018-04-30 11:54:39 +02008274void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008275{
8276 if( ssl->handshake == NULL )
8277 return( NULL );
8278 else
8279 return( ssl->handshake->user_async_ctx );
8280}
8281
Gilles Peskine1febfef2018-04-30 11:54:39 +02008282void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008283 void *ctx )
8284{
8285 if( ssl->handshake != NULL )
8286 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008287}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008288#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008289
Paul Bakker5121ce52009-01-03 21:22:43 +00008290/*
8291 * SSL get accessors
8292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008294{
8295 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8296}
8297
Hanno Becker8b170a02017-10-10 11:51:19 +01008298int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8299{
8300 /*
8301 * Case A: We're currently holding back
8302 * a message for further processing.
8303 */
8304
8305 if( ssl->keep_current_message == 1 )
8306 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008308 return( 1 );
8309 }
8310
8311 /*
8312 * Case B: Further records are pending in the current datagram.
8313 */
8314
8315#if defined(MBEDTLS_SSL_PROTO_DTLS)
8316 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8317 ssl->in_left > ssl->next_record_offset )
8318 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008320 return( 1 );
8321 }
8322#endif /* MBEDTLS_SSL_PROTO_DTLS */
8323
8324 /*
8325 * Case C: A handshake message is being processed.
8326 */
8327
Hanno Becker8b170a02017-10-10 11:51:19 +01008328 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8329 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008331 return( 1 );
8332 }
8333
8334 /*
8335 * Case D: An application data message is being processed
8336 */
8337 if( ssl->in_offt != NULL )
8338 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008340 return( 1 );
8341 }
8342
8343 /*
8344 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008345 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008346 * we implement support for multiple alerts in single records.
8347 */
8348
8349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8350 return( 0 );
8351}
8352
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008353uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008354{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008355 if( ssl->session != NULL )
8356 return( ssl->session->verify_result );
8357
8358 if( ssl->session_negotiate != NULL )
8359 return( ssl->session_negotiate->verify_result );
8360
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008361 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008362}
8363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008365{
Paul Bakker926c8e42013-03-06 10:23:34 +01008366 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008367 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008370}
8371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008375 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008376 {
8377 switch( ssl->minor_ver )
8378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008380 return( "DTLSv1.0" );
8381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008383 return( "DTLSv1.2" );
8384
8385 default:
8386 return( "unknown (DTLS)" );
8387 }
8388 }
8389#endif
8390
Paul Bakker43ca69c2011-01-15 17:35:19 +00008391 switch( ssl->minor_ver )
8392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008394 return( "SSLv3.0" );
8395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008397 return( "TLSv1.0" );
8398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008400 return( "TLSv1.1" );
8401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008403 return( "TLSv1.2" );
8404
Paul Bakker43ca69c2011-01-15 17:35:19 +00008405 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008406 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008407 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008408}
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008411{
Hanno Becker3136ede2018-08-17 15:28:19 +01008412 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008414 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008415
Hanno Becker78640902018-08-13 16:35:15 +01008416 if( transform == NULL )
8417 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_ZLIB_SUPPORT)
8420 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8421 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008422#endif
8423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008426 case MBEDTLS_MODE_GCM:
8427 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008428 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008430 transform_expansion = transform->minlen;
8431 break;
8432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008434
8435 block_size = mbedtls_cipher_get_block_size(
8436 &transform->cipher_ctx_enc );
8437
Hanno Becker3136ede2018-08-17 15:28:19 +01008438 /* Expansion due to the addition of the MAC. */
8439 transform_expansion += transform->maclen;
8440
8441 /* Expansion due to the addition of CBC padding;
8442 * Theoretically up to 256 bytes, but we never use
8443 * more than the block size of the underlying cipher. */
8444 transform_expansion += block_size;
8445
8446 /* For TLS 1.1 or higher, an explicit IV is added
8447 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008448#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8449 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008450 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008451#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008452
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008453 break;
8454
8455 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008458 }
8459
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008460 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008461}
8462
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008463#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8464size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8465{
8466 size_t max_len;
8467
8468 /*
8469 * Assume mfl_code is correct since it was checked when set
8470 */
Angus Grattond8213d02016-05-25 20:56:48 +10008471 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008472
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008473 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008474 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008475 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008476 {
Angus Grattond8213d02016-05-25 20:56:48 +10008477 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008478 }
8479
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008480 /* During a handshake, use the value being negotiated */
8481 if( ssl->session_negotiate != NULL &&
8482 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8483 {
8484 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8485 }
8486
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008487 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008488}
8489#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8490
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008491#if defined(MBEDTLS_SSL_PROTO_DTLS)
8492static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8493{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008494 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8495 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8496 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8497 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8498 return ( 0 );
8499
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008500 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8501 return( ssl->mtu );
8502
8503 if( ssl->mtu == 0 )
8504 return( ssl->handshake->mtu );
8505
8506 return( ssl->mtu < ssl->handshake->mtu ?
8507 ssl->mtu : ssl->handshake->mtu );
8508}
8509#endif /* MBEDTLS_SSL_PROTO_DTLS */
8510
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008511int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8512{
8513 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8514
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008515#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8516 !defined(MBEDTLS_SSL_PROTO_DTLS)
8517 (void) ssl;
8518#endif
8519
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008520#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8521 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8522
8523 if( max_len > mfl )
8524 max_len = mfl;
8525#endif
8526
8527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008528 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008529 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008530 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008531 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8532 const size_t overhead = (size_t) ret;
8533
8534 if( ret < 0 )
8535 return( ret );
8536
8537 if( mtu <= overhead )
8538 {
8539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8540 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8541 }
8542
8543 if( max_len > mtu - overhead )
8544 max_len = mtu - overhead;
8545 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008546#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008547
Hanno Becker0defedb2018-08-10 12:35:02 +01008548#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8549 !defined(MBEDTLS_SSL_PROTO_DTLS)
8550 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008551#endif
8552
8553 return( (int) max_len );
8554}
8555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#if defined(MBEDTLS_X509_CRT_PARSE_C)
8557const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008558{
8559 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008560 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008561
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008562 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008563}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008567int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8568 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008569{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008570 if( ssl == NULL ||
8571 dst == NULL ||
8572 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008573 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008576 }
8577
Hanno Becker52055ae2019-02-06 14:30:46 +00008578 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008581
Paul Bakker5121ce52009-01-03 21:22:43 +00008582/*
Paul Bakker1961b702013-01-25 14:49:24 +01008583 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008588
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008589 if( ssl == NULL || ssl->conf == NULL )
8590 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008592#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008593 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008595#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008597 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008599#endif
8600
Paul Bakker1961b702013-01-25 14:49:24 +01008601 return( ret );
8602}
8603
8604/*
8605 * Perform the SSL handshake
8606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008608{
8609 int ret = 0;
8610
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008611 if( ssl == NULL || ssl->conf == NULL )
8612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008619
8620 if( ret != 0 )
8621 break;
8622 }
8623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008625
8626 return( ret );
8627}
8628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629#if defined(MBEDTLS_SSL_RENEGOTIATION)
8630#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008631/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008632 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008635{
8636 int ret;
8637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008639
8640 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8642 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008643
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008644 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008645 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008647 return( ret );
8648 }
8649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008651
8652 return( 0 );
8653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008655
8656/*
8657 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658 * - any side: calling mbedtls_ssl_renegotiate(),
8659 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8660 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008661 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008662 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008666{
8667 int ret;
8668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008670
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008671 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8672 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008673
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008674 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8675 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008677 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008679 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008680 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008681 ssl->handshake->out_msg_seq = 1;
8682 else
8683 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008684 }
8685#endif
8686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8688 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008693 return( ret );
8694 }
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008697
8698 return( 0 );
8699}
8700
8701/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008702 * Renegotiate current connection on client,
8703 * or request renegotiation on server
8704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008705int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008708
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008709 if( ssl == NULL || ssl->conf == NULL )
8710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008713 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008714 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008720
8721 /* Did we already try/start sending HelloRequest? */
8722 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008724
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008725 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008726 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008727#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008730 /*
8731 * On client, either start the renegotiation process or,
8732 * if already in progress, continue the handshake
8733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008738
8739 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008742 return( ret );
8743 }
8744 }
8745 else
8746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008750 return( ret );
8751 }
8752 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008754
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008755 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008756}
8757
8758/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008759 * Check record counters and renegotiate if they're above the limit.
8760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008762{
Andres AG2196c7f2016-12-15 17:01:16 +00008763 size_t ep_len = ssl_ep_len( ssl );
8764 int in_ctr_cmp;
8765 int out_ctr_cmp;
8766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8768 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008769 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008770 {
8771 return( 0 );
8772 }
8773
Andres AG2196c7f2016-12-15 17:01:16 +00008774 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8775 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008776 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008777 ssl->conf->renego_period + ep_len, 8 - ep_len );
8778
8779 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008780 {
8781 return( 0 );
8782 }
8783
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008788
8789/*
8790 * Receive application data decrypted from the SSL layer
8791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008793{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008794 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008795 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008796
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008797 if( ssl == NULL || ssl->conf == NULL )
8798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008803 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008805 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008806 return( ret );
8807
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008808 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008810 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008811 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008812 return( ret );
8813 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008814 }
8815#endif
8816
Hanno Becker4a810fb2017-05-24 16:27:30 +01008817 /*
8818 * Check if renegotiation is necessary and/or handshake is
8819 * in process. If yes, perform/continue, and fall through
8820 * if an unexpected packet is received while the client
8821 * is waiting for the ServerHello.
8822 *
8823 * (There is no equivalent to the last condition on
8824 * the server-side as it is not treated as within
8825 * a handshake while waiting for the ClientHello
8826 * after a renegotiation request.)
8827 */
8828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008830 ret = ssl_check_ctr_renegotiate( ssl );
8831 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8832 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008835 return( ret );
8836 }
8837#endif
8838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008842 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8843 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008846 return( ret );
8847 }
8848 }
8849
Hanno Beckere41158b2017-10-23 13:30:32 +01008850 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008851 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008853 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008854 if( ssl->f_get_timer != NULL &&
8855 ssl->f_get_timer( ssl->p_timer ) == -1 )
8856 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008857 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008858 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008859
Hanno Becker327c93b2018-08-15 13:56:18 +01008860 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008861 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008862 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8863 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008864
Hanno Becker4a810fb2017-05-24 16:27:30 +01008865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8866 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008867 }
8868
8869 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008871 {
8872 /*
8873 * OpenSSL sends empty messages to randomize the IV
8874 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008875 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008877 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008878 return( 0 );
8879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008881 return( ret );
8882 }
8883 }
8884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008888
Hanno Becker4a810fb2017-05-24 16:27:30 +01008889 /*
8890 * - For client-side, expect SERVER_HELLO_REQUEST.
8891 * - For server-side, expect CLIENT_HELLO.
8892 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8893 */
8894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008896 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008898 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008901
8902 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008904 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008905 {
8906 continue;
8907 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008909 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008910 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008911#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008912
Hanno Becker4a810fb2017-05-24 16:27:30 +01008913#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008918
8919 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008921 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008922 {
8923 continue;
8924 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008927 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008928#endif /* MBEDTLS_SSL_SRV_C */
8929
Hanno Becker21df7f92017-10-17 11:03:26 +01008930#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008931 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008932 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8933 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8934 ssl->conf->allow_legacy_renegotiation ==
8935 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8936 {
8937 /*
8938 * Accept renegotiation request
8939 */
Paul Bakker48916f92012-09-16 19:57:18 +00008940
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008941 /* DTLS clients need to know renego is server-initiated */
8942#if defined(MBEDTLS_SSL_PROTO_DTLS)
8943 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8944 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8945 {
8946 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8947 }
8948#endif
8949 ret = ssl_start_renegotiation( ssl );
8950 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8951 ret != 0 )
8952 {
8953 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8954 return( ret );
8955 }
8956 }
8957 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008958#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008959 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008960 /*
8961 * Refuse renegotiation
8962 */
8963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966#if defined(MBEDTLS_SSL_PROTO_SSL3)
8967 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008968 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008969 /* SSLv3 does not have a "no_renegotiation" warning, so
8970 we send a fatal alert and abort the connection. */
8971 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8972 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8973 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008974 }
8975 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8977#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8978 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8979 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008981 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8982 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8983 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008984 {
8985 return( ret );
8986 }
Paul Bakker48916f92012-09-16 19:57:18 +00008987 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008988 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8990 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008994 }
Paul Bakker48916f92012-09-16 19:57:18 +00008995 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008996
Hanno Becker90333da2017-10-10 11:27:13 +01008997 /* At this point, we don't know whether the renegotiation has been
8998 * completed or not. The cases to consider are the following:
8999 * 1) The renegotiation is complete. In this case, no new record
9000 * has been read yet.
9001 * 2) The renegotiation is incomplete because the client received
9002 * an application data record while awaiting the ServerHello.
9003 * 3) The renegotiation is incomplete because the client received
9004 * a non-handshake, non-application data message while awaiting
9005 * the ServerHello.
9006 * In each of these case, looping will be the proper action:
9007 * - For 1), the next iteration will read a new record and check
9008 * if it's application data.
9009 * - For 2), the loop condition isn't satisfied as application data
9010 * is present, hence continue is the same as break
9011 * - For 3), the loop condition is satisfied and read_record
9012 * will re-deliver the message that was held back by the client
9013 * when expecting the ServerHello.
9014 */
9015 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009016 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009017#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009019 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009020 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009021 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009022 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009025 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009027 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009028 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009029 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9033 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009036 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009037 }
9038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9042 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009043 }
9044
9045 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009046
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009047 /* We're going to return something now, cancel timer,
9048 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009049 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009050 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009051
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009053 /* If we requested renego but received AppData, resend HelloRequest.
9054 * Do it now, after setting in_offt, to avoid taking this branch
9055 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009057 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009059 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009060 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009063 return( ret );
9064 }
9065 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009067#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009068 }
9069
9070 n = ( len < ssl->in_msglen )
9071 ? len : ssl->in_msglen;
9072
9073 memcpy( buf, ssl->in_offt, n );
9074 ssl->in_msglen -= n;
9075
9076 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009077 {
9078 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009079 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009080 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009082 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009083 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009084 /* more data available */
9085 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009089
Paul Bakker23986e52011-04-24 08:57:21 +00009090 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009091}
9092
9093/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009094 * Send application data to be encrypted by the SSL layer, taking care of max
9095 * fragment length and buffer size.
9096 *
9097 * According to RFC 5246 Section 6.2.1:
9098 *
9099 * Zero-length fragments of Application data MAY be sent as they are
9100 * potentially useful as a traffic analysis countermeasure.
9101 *
9102 * Therefore, it is possible that the input message length is 0 and the
9103 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009104 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009105static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009106 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009107{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009108 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9109 const size_t max_len = (size_t) ret;
9110
9111 if( ret < 0 )
9112 {
9113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9114 return( ret );
9115 }
9116
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009117 if( len > max_len )
9118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009120 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009123 "maximum fragment length: %d > %d",
9124 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009126 }
9127 else
9128#endif
9129 len = max_len;
9130 }
Paul Bakker887bd502011-06-08 13:10:54 +00009131
Paul Bakker5121ce52009-01-03 21:22:43 +00009132 if( ssl->out_left != 0 )
9133 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009134 /*
9135 * The user has previously tried to send the data and
9136 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9137 * written. In this case, we expect the high-level write function
9138 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009143 return( ret );
9144 }
9145 }
Paul Bakker887bd502011-06-08 13:10:54 +00009146 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009147 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009148 /*
9149 * The user is trying to send a message the first time, so we need to
9150 * copy the data into the internal buffers and setup the data structure
9151 * to keep track of partial writes
9152 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009153 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009155 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009156
Hanno Becker67bc7c32018-08-06 11:33:50 +01009157 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009160 return( ret );
9161 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009162 }
9163
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009164 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009165}
9166
9167/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009168 * Write application data, doing 1/n-1 splitting if necessary.
9169 *
9170 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009171 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009172 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009175static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009176 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009177{
9178 int ret;
9179
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009180 if( ssl->conf->cbc_record_splitting ==
9181 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009182 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9184 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9185 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009186 {
9187 return( ssl_write_real( ssl, buf, len ) );
9188 }
9189
9190 if( ssl->split_done == 0 )
9191 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009192 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009193 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009194 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009195 }
9196
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009197 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9198 return( ret );
9199 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009200
9201 return( ret + 1 );
9202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009204
9205/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009206 * Write application data (public-facing wrapper)
9207 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009208int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009209{
9210 int ret;
9211
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009213
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009214 if( ssl == NULL || ssl->conf == NULL )
9215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9216
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009217#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009218 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9219 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009220 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009221 return( ret );
9222 }
9223#endif
9224
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009225 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009226 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009227 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009228 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009230 return( ret );
9231 }
9232 }
9233
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009234#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009235 ret = ssl_write_split( ssl, buf, len );
9236#else
9237 ret = ssl_write_real( ssl, buf, len );
9238#endif
9239
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009241
9242 return( ret );
9243}
9244
9245/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009246 * Notify the peer that the connection is being closed
9247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009249{
9250 int ret;
9251
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009252 if( ssl == NULL || ssl->conf == NULL )
9253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009256
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009257 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009258 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9263 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9264 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009267 return( ret );
9268 }
9269 }
9270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009272
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009273 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009274}
9275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009277{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009278 if( transform == NULL )
9279 return;
9280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009282 deflateEnd( &transform->ctx_deflate );
9283 inflateEnd( &transform->ctx_inflate );
9284#endif
9285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9287 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 mbedtls_md_free( &transform->md_ctx_enc );
9290 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009291
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009292 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009293}
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295#if defined(MBEDTLS_X509_CRT_PARSE_C)
9296static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009299
9300 while( cur != NULL )
9301 {
9302 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009304 cur = next;
9305 }
9306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009308
Hanno Becker0271f962018-08-16 13:23:47 +01009309#if defined(MBEDTLS_SSL_PROTO_DTLS)
9310
9311static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9312{
9313 unsigned offset;
9314 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9315
9316 if( hs == NULL )
9317 return;
9318
Hanno Becker283f5ef2018-08-24 09:34:47 +01009319 ssl_free_buffered_record( ssl );
9320
Hanno Becker0271f962018-08-16 13:23:47 +01009321 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009322 ssl_buffering_free_slot( ssl, offset );
9323}
9324
9325static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9326 uint8_t slot )
9327{
9328 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9329 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009330
9331 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9332 return;
9333
Hanno Beckere605b192018-08-21 15:59:07 +01009334 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009335 {
Hanno Beckere605b192018-08-21 15:59:07 +01009336 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009337 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009338 mbedtls_free( hs_buf->data );
9339 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009340 }
9341}
9342
9343#endif /* MBEDTLS_SSL_PROTO_DTLS */
9344
Gilles Peskine9b562d52018-04-25 20:32:43 +02009345void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009346{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009347 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9348
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009349 if( handshake == NULL )
9350 return;
9351
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009352#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9353 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9354 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009355 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009356 handshake->async_in_progress = 0;
9357 }
9358#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9359
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009360#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9361 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9362 mbedtls_md5_free( &handshake->fin_md5 );
9363 mbedtls_sha1_free( &handshake->fin_sha1 );
9364#endif
9365#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9366#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009367#if defined(MBEDTLS_USE_PSA_CRYPTO)
9368 psa_hash_abort( &handshake->fin_sha256_psa );
9369#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009370 mbedtls_sha256_free( &handshake->fin_sha256 );
9371#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009372#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009373#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009374#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009375 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009376#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009377 mbedtls_sha512_free( &handshake->fin_sha512 );
9378#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009379#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009380#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382#if defined(MBEDTLS_DHM_C)
9383 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385#if defined(MBEDTLS_ECDH_C)
9386 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009387#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009388#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009389 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009390#if defined(MBEDTLS_SSL_CLI_C)
9391 mbedtls_free( handshake->ecjpake_cache );
9392 handshake->ecjpake_cache = NULL;
9393 handshake->ecjpake_cache_len = 0;
9394#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009395#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009396
Janos Follath4ae5c292016-02-10 11:27:43 +00009397#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9398 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009399 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009401#endif
9402
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009403#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9404 if( handshake->psk != NULL )
9405 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009406 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009407 mbedtls_free( handshake->psk );
9408 }
9409#endif
9410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9412 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009413 /*
9414 * Free only the linked list wrapper, not the keys themselves
9415 * since the belong to the SNI callback
9416 */
9417 if( handshake->sni_key_cert != NULL )
9418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009420
9421 while( cur != NULL )
9422 {
9423 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009425 cur = next;
9426 }
9427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009429
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009430#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009431 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009432#endif
9433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434#if defined(MBEDTLS_SSL_PROTO_DTLS)
9435 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009436 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009437 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009438#endif
9439
Hanno Becker4a63ed42019-01-08 11:39:35 +00009440#if defined(MBEDTLS_ECDH_C) && \
9441 defined(MBEDTLS_USE_PSA_CRYPTO)
9442 psa_destroy_key( handshake->ecdh_psa_privkey );
9443#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9444
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009445 mbedtls_platform_zeroize( handshake,
9446 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009447}
9448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009450{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009451 if( session == NULL )
9452 return;
9453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009455 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009456#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009457
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009458#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009460#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009461
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009462 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009463}
9464
Paul Bakker5121ce52009-01-03 21:22:43 +00009465/*
9466 * Free an SSL context
9467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009469{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009470 if( ssl == NULL )
9471 return;
9472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009474
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009475 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009476 {
Angus Grattond8213d02016-05-25 20:56:48 +10009477 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009479 }
9480
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009481 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009482 {
Angus Grattond8213d02016-05-25 20:56:48 +10009483 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009485 }
9486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009488 if( ssl->compress_buf != NULL )
9489 {
Angus Grattond8213d02016-05-25 20:56:48 +10009490 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009492 }
9493#endif
9494
Paul Bakker48916f92012-09-16 19:57:18 +00009495 if( ssl->transform )
9496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 mbedtls_ssl_transform_free( ssl->transform );
9498 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009499 }
9500
9501 if( ssl->handshake )
9502 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009503 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9505 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 mbedtls_free( ssl->handshake );
9508 mbedtls_free( ssl->transform_negotiate );
9509 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009510 }
9511
Paul Bakkerc0463502013-02-14 11:19:38 +01009512 if( ssl->session )
9513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514 mbedtls_ssl_session_free( ssl->session );
9515 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009516 }
9517
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009518#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009519 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009520 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009521 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009523 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009524#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9527 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9530 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009531 }
9532#endif
9533
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009534#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009536#endif
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009539
Paul Bakker86f04f42013-02-14 11:20:09 +01009540 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009541 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009542}
9543
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009544/*
9545 * Initialze mbedtls_ssl_config
9546 */
9547void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9548{
9549 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9550}
9551
Simon Butcherc97b6972015-12-27 23:48:17 +00009552#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009553static int ssl_preset_default_hashes[] = {
9554#if defined(MBEDTLS_SHA512_C)
9555 MBEDTLS_MD_SHA512,
9556 MBEDTLS_MD_SHA384,
9557#endif
9558#if defined(MBEDTLS_SHA256_C)
9559 MBEDTLS_MD_SHA256,
9560 MBEDTLS_MD_SHA224,
9561#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009562#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009563 MBEDTLS_MD_SHA1,
9564#endif
9565 MBEDTLS_MD_NONE
9566};
Simon Butcherc97b6972015-12-27 23:48:17 +00009567#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009568
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009569static int ssl_preset_suiteb_ciphersuites[] = {
9570 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9571 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9572 0
9573};
9574
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009575#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009576static int ssl_preset_suiteb_hashes[] = {
9577 MBEDTLS_MD_SHA256,
9578 MBEDTLS_MD_SHA384,
9579 MBEDTLS_MD_NONE
9580};
9581#endif
9582
9583#if defined(MBEDTLS_ECP_C)
9584static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9585 MBEDTLS_ECP_DP_SECP256R1,
9586 MBEDTLS_ECP_DP_SECP384R1,
9587 MBEDTLS_ECP_DP_NONE
9588};
9589#endif
9590
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009591/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009592 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009593 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009594int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009595 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009596{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009597#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009598 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009599#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009600
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009601 /* Use the functions here so that they are covered in tests,
9602 * but otherwise access member directly for efficiency */
9603 mbedtls_ssl_conf_endpoint( conf, endpoint );
9604 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009605
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009606 /*
9607 * Things that are common to all presets
9608 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009609#if defined(MBEDTLS_SSL_CLI_C)
9610 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9611 {
9612 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9613#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9614 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9615#endif
9616 }
9617#endif
9618
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009619#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009620 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009621#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009622
9623#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9624 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9625#endif
9626
9627#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9628 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9629#endif
9630
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009631#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9632 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9633#endif
9634
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009635#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009636 conf->f_cookie_write = ssl_cookie_write_dummy;
9637 conf->f_cookie_check = ssl_cookie_check_dummy;
9638#endif
9639
9640#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9641 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9642#endif
9643
Janos Follath088ce432017-04-10 12:42:31 +01009644#if defined(MBEDTLS_SSL_SRV_C)
9645 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9646#endif
9647
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009648#if defined(MBEDTLS_SSL_PROTO_DTLS)
9649 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9650 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9651#endif
9652
9653#if defined(MBEDTLS_SSL_RENEGOTIATION)
9654 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009655 memset( conf->renego_period, 0x00, 2 );
9656 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009657#endif
9658
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009659#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9660 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9661 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009662 const unsigned char dhm_p[] =
9663 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9664 const unsigned char dhm_g[] =
9665 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9666
Hanno Beckera90658f2017-10-04 15:29:08 +01009667 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9668 dhm_p, sizeof( dhm_p ),
9669 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009670 {
9671 return( ret );
9672 }
9673 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009674#endif
9675
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009676 /*
9677 * Preset-specific defaults
9678 */
9679 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009680 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009681 /*
9682 * NSA Suite B
9683 */
9684 case MBEDTLS_SSL_PRESET_SUITEB:
9685 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9686 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9687 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9688 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9689
9690 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9691 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9692 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9693 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9694 ssl_preset_suiteb_ciphersuites;
9695
9696#if defined(MBEDTLS_X509_CRT_PARSE_C)
9697 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009698#endif
9699
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009700#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009701 conf->sig_hashes = ssl_preset_suiteb_hashes;
9702#endif
9703
9704#if defined(MBEDTLS_ECP_C)
9705 conf->curve_list = ssl_preset_suiteb_curves;
9706#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009707 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009708
9709 /*
9710 * Default
9711 */
9712 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009713 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9714 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9715 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9716 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9717 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9718 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9719 MBEDTLS_SSL_MIN_MINOR_VERSION :
9720 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009721 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9722 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9723
9724#if defined(MBEDTLS_SSL_PROTO_DTLS)
9725 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9726 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9727#endif
9728
9729 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9730 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9731 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9732 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9733 mbedtls_ssl_list_ciphersuites();
9734
9735#if defined(MBEDTLS_X509_CRT_PARSE_C)
9736 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9737#endif
9738
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009739#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009740 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009741#endif
9742
9743#if defined(MBEDTLS_ECP_C)
9744 conf->curve_list = mbedtls_ecp_grp_id_list();
9745#endif
9746
9747#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9748 conf->dhm_min_bitlen = 1024;
9749#endif
9750 }
9751
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009752 return( 0 );
9753}
9754
9755/*
9756 * Free mbedtls_ssl_config
9757 */
9758void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9759{
9760#if defined(MBEDTLS_DHM_C)
9761 mbedtls_mpi_free( &conf->dhm_P );
9762 mbedtls_mpi_free( &conf->dhm_G );
9763#endif
9764
9765#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9766 if( conf->psk != NULL )
9767 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009768 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009769 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009770 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009771 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009772 }
9773
9774 if( conf->psk_identity != NULL )
9775 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009776 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009777 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009778 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009779 conf->psk_identity_len = 0;
9780 }
9781#endif
9782
9783#if defined(MBEDTLS_X509_CRT_PARSE_C)
9784 ssl_key_cert_free( conf->key_cert );
9785#endif
9786
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009787 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009788}
9789
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009790#if defined(MBEDTLS_PK_C) && \
9791 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009792/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797#if defined(MBEDTLS_RSA_C)
9798 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9799 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801#if defined(MBEDTLS_ECDSA_C)
9802 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9803 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009804#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009806}
9807
Hanno Becker7e5437a2017-04-28 17:15:26 +01009808unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9809{
9810 switch( type ) {
9811 case MBEDTLS_PK_RSA:
9812 return( MBEDTLS_SSL_SIG_RSA );
9813 case MBEDTLS_PK_ECDSA:
9814 case MBEDTLS_PK_ECKEY:
9815 return( MBEDTLS_SSL_SIG_ECDSA );
9816 default:
9817 return( MBEDTLS_SSL_SIG_ANON );
9818 }
9819}
9820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009822{
9823 switch( sig )
9824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#if defined(MBEDTLS_RSA_C)
9826 case MBEDTLS_SSL_SIG_RSA:
9827 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009828#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829#if defined(MBEDTLS_ECDSA_C)
9830 case MBEDTLS_SSL_SIG_ECDSA:
9831 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009832#endif
9833 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009835 }
9836}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009837#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009838
Hanno Becker7e5437a2017-04-28 17:15:26 +01009839#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9840 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9841
9842/* Find an entry in a signature-hash set matching a given hash algorithm. */
9843mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9844 mbedtls_pk_type_t sig_alg )
9845{
9846 switch( sig_alg )
9847 {
9848 case MBEDTLS_PK_RSA:
9849 return( set->rsa );
9850 case MBEDTLS_PK_ECDSA:
9851 return( set->ecdsa );
9852 default:
9853 return( MBEDTLS_MD_NONE );
9854 }
9855}
9856
9857/* Add a signature-hash-pair to a signature-hash set */
9858void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9859 mbedtls_pk_type_t sig_alg,
9860 mbedtls_md_type_t md_alg )
9861{
9862 switch( sig_alg )
9863 {
9864 case MBEDTLS_PK_RSA:
9865 if( set->rsa == MBEDTLS_MD_NONE )
9866 set->rsa = md_alg;
9867 break;
9868
9869 case MBEDTLS_PK_ECDSA:
9870 if( set->ecdsa == MBEDTLS_MD_NONE )
9871 set->ecdsa = md_alg;
9872 break;
9873
9874 default:
9875 break;
9876 }
9877}
9878
9879/* Allow exactly one hash algorithm for each signature. */
9880void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9881 mbedtls_md_type_t md_alg )
9882{
9883 set->rsa = md_alg;
9884 set->ecdsa = md_alg;
9885}
9886
9887#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9888 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9889
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009890/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009891 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009894{
9895 switch( hash )
9896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897#if defined(MBEDTLS_MD5_C)
9898 case MBEDTLS_SSL_HASH_MD5:
9899 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901#if defined(MBEDTLS_SHA1_C)
9902 case MBEDTLS_SSL_HASH_SHA1:
9903 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#if defined(MBEDTLS_SHA256_C)
9906 case MBEDTLS_SSL_HASH_SHA224:
9907 return( MBEDTLS_MD_SHA224 );
9908 case MBEDTLS_SSL_HASH_SHA256:
9909 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#if defined(MBEDTLS_SHA512_C)
9912 case MBEDTLS_SSL_HASH_SHA384:
9913 return( MBEDTLS_MD_SHA384 );
9914 case MBEDTLS_SSL_HASH_SHA512:
9915 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009916#endif
9917 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009919 }
9920}
9921
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009922/*
9923 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9924 */
9925unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9926{
9927 switch( md )
9928 {
9929#if defined(MBEDTLS_MD5_C)
9930 case MBEDTLS_MD_MD5:
9931 return( MBEDTLS_SSL_HASH_MD5 );
9932#endif
9933#if defined(MBEDTLS_SHA1_C)
9934 case MBEDTLS_MD_SHA1:
9935 return( MBEDTLS_SSL_HASH_SHA1 );
9936#endif
9937#if defined(MBEDTLS_SHA256_C)
9938 case MBEDTLS_MD_SHA224:
9939 return( MBEDTLS_SSL_HASH_SHA224 );
9940 case MBEDTLS_MD_SHA256:
9941 return( MBEDTLS_SSL_HASH_SHA256 );
9942#endif
9943#if defined(MBEDTLS_SHA512_C)
9944 case MBEDTLS_MD_SHA384:
9945 return( MBEDTLS_SSL_HASH_SHA384 );
9946 case MBEDTLS_MD_SHA512:
9947 return( MBEDTLS_SSL_HASH_SHA512 );
9948#endif
9949 default:
9950 return( MBEDTLS_SSL_HASH_NONE );
9951 }
9952}
9953
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009954#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009955/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009956 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009957 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009958 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009959int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009962
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009963 if( ssl->conf->curve_list == NULL )
9964 return( -1 );
9965
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009966 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009967 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009968 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009969
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009970 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009971}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009972#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009973
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009974#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009975/*
9976 * Check if a hash proposed by the peer is in our list.
9977 * Return 0 if we're willing to use it, -1 otherwise.
9978 */
9979int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9980 mbedtls_md_type_t md )
9981{
9982 const int *cur;
9983
9984 if( ssl->conf->sig_hashes == NULL )
9985 return( -1 );
9986
9987 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9988 if( *cur == (int) md )
9989 return( 0 );
9990
9991 return( -1 );
9992}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009993#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995#if defined(MBEDTLS_X509_CRT_PARSE_C)
9996int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9997 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009998 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009999 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010000{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010001 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010003 int usage = 0;
10004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010006 const char *ext_oid;
10007 size_t ext_len;
10008#endif
10009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10011 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010012 ((void) cert);
10013 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010014 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010015#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10018 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010019 {
10020 /* Server part of the key exchange */
10021 switch( ciphersuite->key_exchange )
10022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 case MBEDTLS_KEY_EXCHANGE_RSA:
10024 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010025 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010026 break;
10027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10029 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10030 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10031 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010032 break;
10033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10035 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010036 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010037 break;
10038
10039 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 case MBEDTLS_KEY_EXCHANGE_NONE:
10041 case MBEDTLS_KEY_EXCHANGE_PSK:
10042 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10043 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010044 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010045 usage = 0;
10046 }
10047 }
10048 else
10049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10051 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010052 }
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010055 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010056 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010057 ret = -1;
10058 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010059#else
10060 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10064 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10067 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010068 }
10069 else
10070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10072 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010073 }
10074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010076 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010077 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010078 ret = -1;
10079 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010081
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010082 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010085
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010086/*
10087 * Convert version numbers to/from wire format
10088 * and, for DTLS, to/from TLS equivalent.
10089 *
10090 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010091 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010092 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10093 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010096 unsigned char ver[2] )
10097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098#if defined(MBEDTLS_SSL_PROTO_DTLS)
10099 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010102 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10103
10104 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10105 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10106 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010107 else
10108#else
10109 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010110#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010111 {
10112 ver[0] = (unsigned char) major;
10113 ver[1] = (unsigned char) minor;
10114 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010115}
10116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010118 const unsigned char ver[2] )
10119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120#if defined(MBEDTLS_SSL_PROTO_DTLS)
10121 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010122 {
10123 *major = 255 - ver[0] + 2;
10124 *minor = 255 - ver[1] + 1;
10125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010127 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10128 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010129 else
10130#else
10131 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010132#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010133 {
10134 *major = ver[0];
10135 *minor = ver[1];
10136 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010137}
10138
Simon Butcher99000142016-10-13 17:21:01 +010010139int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10140{
10141#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10142 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10143 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10144
10145 switch( md )
10146 {
10147#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10148#if defined(MBEDTLS_MD5_C)
10149 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010150 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010151#endif
10152#if defined(MBEDTLS_SHA1_C)
10153 case MBEDTLS_SSL_HASH_SHA1:
10154 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10155 break;
10156#endif
10157#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10158#if defined(MBEDTLS_SHA512_C)
10159 case MBEDTLS_SSL_HASH_SHA384:
10160 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10161 break;
10162#endif
10163#if defined(MBEDTLS_SHA256_C)
10164 case MBEDTLS_SSL_HASH_SHA256:
10165 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10166 break;
10167#endif
10168 default:
10169 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10170 }
10171
10172 return 0;
10173#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10174 (void) ssl;
10175 (void) md;
10176
10177 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10179}
10180
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010181#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10182 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10183int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10184 unsigned char *output,
10185 unsigned char *data, size_t data_len )
10186{
10187 int ret = 0;
10188 mbedtls_md5_context mbedtls_md5;
10189 mbedtls_sha1_context mbedtls_sha1;
10190
10191 mbedtls_md5_init( &mbedtls_md5 );
10192 mbedtls_sha1_init( &mbedtls_sha1 );
10193
10194 /*
10195 * digitally-signed struct {
10196 * opaque md5_hash[16];
10197 * opaque sha_hash[20];
10198 * };
10199 *
10200 * md5_hash
10201 * MD5(ClientHello.random + ServerHello.random
10202 * + ServerParams);
10203 * sha_hash
10204 * SHA(ClientHello.random + ServerHello.random
10205 * + ServerParams);
10206 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010207 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010208 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010210 goto exit;
10211 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010212 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010213 ssl->handshake->randbytes, 64 ) ) != 0 )
10214 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010216 goto exit;
10217 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010218 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010219 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010221 goto exit;
10222 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010223 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010224 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010226 goto exit;
10227 }
10228
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010229 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010230 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010232 goto exit;
10233 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010234 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010235 ssl->handshake->randbytes, 64 ) ) != 0 )
10236 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010238 goto exit;
10239 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010240 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010241 data_len ) ) != 0 )
10242 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010244 goto exit;
10245 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010246 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010247 output + 16 ) ) != 0 )
10248 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010249 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010250 goto exit;
10251 }
10252
10253exit:
10254 mbedtls_md5_free( &mbedtls_md5 );
10255 mbedtls_sha1_free( &mbedtls_sha1 );
10256
10257 if( ret != 0 )
10258 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10259 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10260
10261 return( ret );
10262
10263}
10264#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10265 MBEDTLS_SSL_PROTO_TLS1_1 */
10266
10267#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10268 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010269
10270#if defined(MBEDTLS_USE_PSA_CRYPTO)
10271int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10272 unsigned char *hash, size_t *hashlen,
10273 unsigned char *data, size_t data_len,
10274 mbedtls_md_type_t md_alg )
10275{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010276 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010277 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010278 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10279
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010281
10282 if( ( status = psa_hash_setup( &hash_operation,
10283 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010284 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010285 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010286 goto exit;
10287 }
10288
Andrzej Kurek814feff2019-01-14 04:35:19 -050010289 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10290 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010291 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010292 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010293 goto exit;
10294 }
10295
Andrzej Kurek814feff2019-01-14 04:35:19 -050010296 if( ( status = psa_hash_update( &hash_operation,
10297 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010298 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010299 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010300 goto exit;
10301 }
10302
Andrzej Kurek814feff2019-01-14 04:35:19 -050010303 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10304 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010305 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010306 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010307 goto exit;
10308 }
10309
10310exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010311 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010312 {
10313 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10314 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010315 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010316 {
10317 case PSA_ERROR_NOT_SUPPORTED:
10318 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010319 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010320 case PSA_ERROR_BUFFER_TOO_SMALL:
10321 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10322 case PSA_ERROR_INSUFFICIENT_MEMORY:
10323 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10324 default:
10325 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10326 }
10327 }
10328 return( 0 );
10329}
10330
10331#else
10332
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010333int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010334 unsigned char *hash, size_t *hashlen,
10335 unsigned char *data, size_t data_len,
10336 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010337{
10338 int ret = 0;
10339 mbedtls_md_context_t ctx;
10340 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010341 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010342
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010344
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010345 mbedtls_md_init( &ctx );
10346
10347 /*
10348 * digitally-signed struct {
10349 * opaque client_random[32];
10350 * opaque server_random[32];
10351 * ServerDHParams params;
10352 * };
10353 */
10354 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10355 {
10356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10357 goto exit;
10358 }
10359 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10360 {
10361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10362 goto exit;
10363 }
10364 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10365 {
10366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10367 goto exit;
10368 }
10369 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10370 {
10371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10372 goto exit;
10373 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010374 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010375 {
10376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10377 goto exit;
10378 }
10379
10380exit:
10381 mbedtls_md_free( &ctx );
10382
10383 if( ret != 0 )
10384 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10385 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10386
10387 return( ret );
10388}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010389#endif /* MBEDTLS_USE_PSA_CRYPTO */
10390
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010391#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10392 MBEDTLS_SSL_PROTO_TLS1_2 */
10393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394#endif /* MBEDTLS_SSL_TLS_C */