blob: df106a530fd588e1bdc7fe9745bac7dc451bbc85 [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)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000289
290#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291 if( src->peer_cert != NULL )
292 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200293 int ret;
294
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200295 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200296 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200297 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200302 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 dst->peer_cert = NULL;
306 return( ret );
307 }
308 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000309#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000310 if( src->peer_cert_digest != NULL )
311 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000312 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000313 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000314 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;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000320 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000321 }
322#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200325
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200326#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200327 if( src->ticket != NULL )
328 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200329 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200330 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200331 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200332
333 memcpy( dst->ticket, src->ticket, src->ticket_len );
334 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200335#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200336
337 return( 0 );
338}
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
341int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200342 const unsigned char *key_enc, const unsigned char *key_dec,
343 size_t keylen,
344 const unsigned char *iv_enc, const unsigned char *iv_dec,
345 size_t ivlen,
346 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200347 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
349int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
353#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Key material generation
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200359static int ssl3_prf( const unsigned char *secret, size_t slen,
360 const char *label,
361 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000362 unsigned char *dstbuf, size_t dlen )
363{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100364 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000365 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200366 mbedtls_md5_context md5;
367 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000368 unsigned char padding[16];
369 unsigned char sha1sum[20];
370 ((void)label);
371
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200372 mbedtls_md5_init( &md5 );
373 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
Paul Bakker5f70b252012-09-13 14:23:06 +0000375 /*
376 * SSLv3:
377 * block =
378 * MD5( secret + SHA1( 'A' + secret + random ) ) +
379 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
380 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
381 * ...
382 */
383 for( i = 0; i < dlen / 16; i++ )
384 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100398 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100399 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100401 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000406 }
407
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100408exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200409 mbedtls_md5_free( &md5 );
410 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500412 mbedtls_platform_zeroize( padding, sizeof( padding ) );
413 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000414
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100415 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200420static int tls1_prf( const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000423 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 size_t nb, hs;
426 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300428 unsigned char *tmp;
429 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 const mbedtls_md_info_t *md_info;
432 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100433 int ret;
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Ron Eldor3b350852019-05-07 18:31:49 +0300437 tmp_len = 20 + strlen( label ) + rlen;
438 tmp = mbedtls_calloc( 1, tmp_len );
439 if( tmp == NULL )
440 {
441 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
442 goto exit;
443 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445 hs = ( slen + 1 ) / 2;
446 S1 = secret;
447 S2 = secret + slen - hs;
448
449 nb = strlen( label );
450 memcpy( tmp + 20, label, nb );
451 memcpy( tmp + 20 + nb, random, rlen );
452 nb += rlen;
453
454 /*
455 * First compute P_md5(secret,label+random)[0..dlen]
456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300458 {
459 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
460 goto exit;
461 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300464 {
465 goto exit;
466 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
469 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
470 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 for( i = 0; i < dlen; i += 16 )
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_hmac_reset ( &md_ctx );
475 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
476 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_hmac_reset ( &md_ctx );
479 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
480 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
483
484 for( j = 0; j < k; j++ )
485 dstbuf[i + j] = h_i[j];
486 }
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 /*
491 * XOR out with P_sha1(secret,label+random)[0..dlen]
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300494 {
495 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
496 goto exit;
497 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300500 {
501 goto exit;
502 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
505 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
506 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508 for( i = 0; i < dlen; i += 20 )
509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_md_hmac_reset ( &md_ctx );
511 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
512 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_md_hmac_reset ( &md_ctx );
515 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
516 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
519
520 for( j = 0; j < k; j++ )
521 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
522 }
523
Ron Eldor3b350852019-05-07 18:31:49 +0300524exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100526
Ron Eldor3b350852019-05-07 18:31:49 +0300527 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500528 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Ron Eldor3b350852019-05-07 18:31:49 +0300530 mbedtls_free( tmp );
531 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500536#if defined(MBEDTLS_USE_PSA_CRYPTO)
537static int tls_prf_generic( mbedtls_md_type_t md_type,
538 const unsigned char *secret, size_t slen,
539 const char *label,
540 const unsigned char *random, size_t rlen,
541 unsigned char *dstbuf, size_t dlen )
542{
543 psa_status_t status;
544 psa_algorithm_t alg;
545 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500546 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500547 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
548
Andrzej Kurek2f760752019-01-28 08:08:15 -0500549 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500550 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500551
Andrzej Kurekc929a822019-01-14 03:51:11 -0500552 if( md_type == MBEDTLS_MD_SHA384 )
553 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
554 else
555 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
556
Andrzej Kurek2f760752019-01-28 08:08:15 -0500557 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500558 psa_key_policy_set_usage( &policy,
559 PSA_KEY_USAGE_DERIVE,
560 alg );
561 status = psa_set_key_policy( master_slot, &policy );
562 if( status != PSA_SUCCESS )
563 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
564
565 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
566 if( status != PSA_SUCCESS )
567 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
568
569 status = psa_key_derivation( &generator,
570 master_slot, alg,
571 random, rlen,
572 (unsigned char const *) label,
573 (size_t) strlen( label ),
574 dlen );
575 if( status != PSA_SUCCESS )
576 {
577 psa_generator_abort( &generator );
578 psa_destroy_key( master_slot );
579 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
580 }
581
582 status = psa_generator_read( &generator, dstbuf, dlen );
583 if( status != PSA_SUCCESS )
584 {
585 psa_generator_abort( &generator );
586 psa_destroy_key( master_slot );
587 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
588 }
589
590 status = psa_generator_abort( &generator );
591 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500592 {
593 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500594 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500595 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500596
597 status = psa_destroy_key( master_slot );
598 if( status != PSA_SUCCESS )
599 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
600
Andrzej Kurek33171262019-01-15 03:25:18 -0500601 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500602}
603
604#else /* MBEDTLS_USE_PSA_CRYPTO */
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 const unsigned char *secret, size_t slen,
608 const char *label,
609 const unsigned char *random, size_t rlen,
610 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611{
612 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300614 unsigned char *tmp;
615 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
Ron Eldor3b350852019-05-07 18:31:49 +0300628 tmp_len = md_len + strlen( label ) + rlen;
629 tmp = mbedtls_calloc( 1, tmp_len );
630 if( tmp == NULL )
631 {
632 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
633 goto exit;
634 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635
636 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 memcpy( tmp + md_len, label, nb );
638 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639 nb += rlen;
640
641 /*
642 * Compute P_<hash>(secret, label + random)[0..dlen]
643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300645 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
648 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
649 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100650
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100651 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_md_hmac_reset ( &md_ctx );
654 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
655 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_md_hmac_reset ( &md_ctx );
658 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
659 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000660
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100661 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000662
663 for( j = 0; j < k; j++ )
664 dstbuf[i + j] = h_i[j];
665 }
666
Ron Eldor3b350852019-05-07 18:31:49 +0300667exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669
Ron Eldor3b350852019-05-07 18:31:49 +0300670 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500671 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000672
Ron Eldor3b350852019-05-07 18:31:49 +0300673 mbedtls_free( tmp );
674
675 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000676}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500677#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100679static int tls_prf_sha256( const unsigned char *secret, size_t slen,
680 const char *label,
681 const unsigned char *random, size_t rlen,
682 unsigned char *dstbuf, size_t dlen )
683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100685 label, random, rlen, dstbuf, dlen ) );
686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200690static int tls_prf_sha384( const unsigned char *secret, size_t slen,
691 const char *label,
692 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000693 unsigned char *dstbuf, size_t dlen )
694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100696 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#endif /* MBEDTLS_SHA512_C */
699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
704 defined(MBEDTLS_SSL_PROTO_TLS1_1)
705static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200706#endif
Paul Bakker380da532012-04-18 16:10:25 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#if defined(MBEDTLS_SSL_PROTO_SSL3)
709static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
710static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200711#endif
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
714static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
715static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#endif
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
719#if defined(MBEDTLS_SHA256_C)
720static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
721static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
722static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200723#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#if defined(MBEDTLS_SHA512_C)
726static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
727static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
728static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000731
Hanno Becker7d0a5692018-10-23 15:26:22 +0100732#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
733 defined(MBEDTLS_USE_PSA_CRYPTO)
734static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
735{
736 if( ssl->conf->f_psk != NULL )
737 {
738 /* If we've used a callback to select the PSK,
739 * the static configuration is irrelevant. */
740 if( ssl->handshake->psk_opaque != 0 )
741 return( 1 );
742
743 return( 0 );
744 }
745
746 if( ssl->conf->psk_opaque != 0 )
747 return( 1 );
748
749 return( 0 );
750}
751#endif /* MBEDTLS_USE_PSA_CRYPTO &&
752 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
753
Ron Eldor51d3ab52019-05-12 14:54:30 +0300754int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
755 const unsigned char *secret, size_t slen,
756 const char *label,
757 const unsigned char *random, size_t rlen,
758 unsigned char *dstbuf, size_t dlen )
759{
760 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
761
762 switch( prf )
763 {
764#if defined(MBEDTLS_SSL_PROTO_SSL3)
765 case MBEDTLS_SSL_TLS_PRF_SSL3:
766 tls_prf = ssl3_prf;
767 break;
768#endif
769#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
770 case MBEDTLS_SSL_TLS_PRF_TLS1:
771 tls_prf = tls1_prf;
772 break;
773#endif
774#if defined(MBEDTLS_SHA512_C)
775 case MBEDTLS_SSL_TLS_PRF_SHA384:
776 tls_prf = tls_prf_sha384;
777 break;
778#endif
779#if defined(MBEDTLS_SHA256_C)
780 case MBEDTLS_SSL_TLS_PRF_SHA256:
781 tls_prf = tls_prf_sha256;
782 break;
783#endif
784 default:
785 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
786 }
787
788 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
789}
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000792{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200793 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000794#if defined(MBEDTLS_USE_PSA_CRYPTO)
795 int psa_fallthrough;
796#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000798 unsigned char keyblk[256];
799 unsigned char *key1;
800 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100801 unsigned char *mac_enc;
802 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000803 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200804 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000805 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000806 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 const mbedtls_cipher_info_t *cipher_info;
808 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100809
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000810 /* cf. RFC 5246, Section 8.1:
811 * "The master secret is always exactly 48 bytes in length." */
812 size_t const master_secret_len = 48;
813
Ron Eldor51d3ab52019-05-12 14:54:30 +0300814#if defined(MBEDTLS_SSL_EXPORT_KEYS)
815 mbedtls_tls_prf_types tls_prf_type = MBEDTLS_SSL_TLS_PRF_NONE;
816#endif
817
Hanno Becker35b23c72018-10-23 12:10:41 +0100818#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
819 unsigned char session_hash[48];
820#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_ssl_session *session = ssl->session_negotiate;
823 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
824 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000828#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
829 transform->encrypt_then_mac = session->encrypt_then_mac;
830#endif
831 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000832
833 ciphersuite_info = handshake->ciphersuite_info;
834 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100835 if( cipher_info == NULL )
836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000838 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100840 }
841
Hanno Beckere694c3e2017-12-27 21:34:08 +0000842 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100843 if( md_info == NULL )
844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000846 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100848 }
849
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000851 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_SSL_PROTO_SSL3)
854 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000855 {
Paul Bakker48916f92012-09-16 19:57:18 +0000856 handshake->tls_prf = ssl3_prf;
857 handshake->calc_verify = ssl_calc_verify_ssl;
858 handshake->calc_finished = ssl_calc_finished_ssl;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300859#if defined(MBEDTLS_SSL_EXPORT_KEYS)
860 tls_prf_type = MBEDTLS_SSL_TLS_PRF_SSL3;
861#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +0000862 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200863 else
864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
866 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000867 {
Paul Bakker48916f92012-09-16 19:57:18 +0000868 handshake->tls_prf = tls1_prf;
869 handshake->calc_verify = ssl_calc_verify_tls;
870 handshake->calc_finished = ssl_calc_finished_tls;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300871#if defined(MBEDTLS_SSL_EXPORT_KEYS)
872 tls_prf_type = MBEDTLS_SSL_TLS_PRF_TLS1;
873#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +0000874 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200875 else
876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
878#if defined(MBEDTLS_SHA512_C)
879 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000880 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000881 {
Paul Bakker48916f92012-09-16 19:57:18 +0000882 handshake->tls_prf = tls_prf_sha384;
883 handshake->calc_verify = ssl_calc_verify_tls_sha384;
884 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300885#if defined(MBEDTLS_SSL_EXPORT_KEYS)
886 tls_prf_type = MBEDTLS_SSL_TLS_PRF_SHA384;
887#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +0000888 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000889 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#if defined(MBEDTLS_SHA256_C)
892 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000893 {
Paul Bakker48916f92012-09-16 19:57:18 +0000894 handshake->tls_prf = tls_prf_sha256;
895 handshake->calc_verify = ssl_calc_verify_tls_sha256;
896 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300897#if defined(MBEDTLS_SSL_EXPORT_KEYS)
898 tls_prf_type = MBEDTLS_SSL_TLS_PRF_SHA256;
899#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +0000900 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200901 else
902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200907 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000908
909 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 * SSLv3:
911 * master =
912 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
913 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
914 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200915 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 * master = PRF( premaster, "master secret", randbytes )[0..47]
918 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100919 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100921 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
922 }
923 else
924 {
925 /* The label for the KDF used for key expansion.
926 * This is either "master secret" or "extended master secret"
927 * depending on whether the Extended Master Secret extension
928 * is used. */
929 char const *lbl = "master secret";
930
931 /* The salt for the KDF used for key expansion.
932 * - If the Extended Master Secret extension is not used,
933 * this is ClientHello.Random + ServerHello.Random
934 * (see Sect. 8.1 in RFC 5246).
935 * - If the Extended Master Secret extension is used,
936 * this is the transcript of the handshake so far.
937 * (see Sect. 4 in RFC 7627). */
938 unsigned char const *salt = handshake->randbytes;
939 size_t salt_len = 64;
940
941#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200945
Hanno Becker35b23c72018-10-23 12:10:41 +0100946 lbl = "extended master secret";
947 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200948 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
950 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000953 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
954 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100955 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000956 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200957 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100958#endif /* MBEDTLS_SHA512_C */
959 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200960 }
961 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100963 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200964
Hanno Becker35b23c72018-10-23 12:10:41 +0100965 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200966 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100967#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
968
Hanno Becker7d0a5692018-10-23 15:26:22 +0100969#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
970 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
971 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
972 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
973 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100974 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100975 /* Perform PSK-to-MS expansion in a single step. */
976 psa_status_t status;
977 psa_algorithm_t alg;
978 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500979 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100980
981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
982
983 psk = ssl->conf->psk_opaque;
984 if( ssl->handshake->psk_opaque != 0 )
985 psk = ssl->handshake->psk_opaque;
986
Hanno Becker22bf1452019-04-05 11:21:08 +0100987 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +0100988 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
989 else
990 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
991
992 status = psa_key_derivation( &generator, psk, alg,
993 salt, salt_len,
994 (unsigned char const *) lbl,
995 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000996 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100997 if( status != PSA_SUCCESS )
998 {
999 psa_generator_abort( &generator );
1000 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1001 }
1002
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001003 status = psa_generator_read( &generator, session->master,
1004 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001005 if( status != PSA_SUCCESS )
1006 {
1007 psa_generator_abort( &generator );
1008 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1009 }
1010
1011 status = psa_generator_abort( &generator );
1012 if( status != PSA_SUCCESS )
1013 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001014 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001015 else
1016#endif
1017 {
1018 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1019 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001020 session->master,
1021 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001022 if( ret != 0 )
1023 {
1024 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1025 return( ret );
1026 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001027
Hanno Becker7d0a5692018-10-23 15:26:22 +01001028 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1029 handshake->premaster,
1030 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001031
Hanno Becker7d0a5692018-10-23 15:26:22 +01001032 mbedtls_platform_zeroize( handshake->premaster,
1033 sizeof(handshake->premaster) );
1034 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
1037 /*
1038 * Swap the client and server random values.
1039 */
Paul Bakker48916f92012-09-16 19:57:18 +00001040 memcpy( tmp, handshake->randbytes, 64 );
1041 memcpy( handshake->randbytes, tmp + 32, 32 );
1042 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001043 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
1045 /*
1046 * SSLv3:
1047 * key block =
1048 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1049 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1050 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1051 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1052 * ...
1053 *
1054 * TLSv1:
1055 * key block = PRF( master, "key expansion", randbytes )
1056 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001057 ret = handshake->tls_prf( session->master, 48, "key expansion",
1058 handshake->randbytes, 64, keyblk, 256 );
1059 if( ret != 0 )
1060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001062 return( ret );
1063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1066 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1067 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1068 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1069 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 /*
1072 * Determine the appropriate key, IV and MAC length.
1073 */
Paul Bakker68884e32013-01-07 18:20:04 +01001074
Hanno Becker88aaf652017-12-27 08:17:40 +00001075 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001076
Hanno Becker8031d062018-01-03 15:32:31 +00001077#if defined(MBEDTLS_GCM_C) || \
1078 defined(MBEDTLS_CCM_C) || \
1079 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001081 cipher_info->mode == MBEDTLS_MODE_CCM ||
1082 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001084 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001085
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001086 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001087 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001088 transform->taglen =
1089 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001090
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001091 /* All modes haves 96-bit IVs;
1092 * GCM and CCM has 4 implicit and 8 explicit bytes
1093 * ChachaPoly has all 12 bytes implicit
1094 */
Paul Bakker68884e32013-01-07 18:20:04 +01001095 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001096 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1097 transform->fixed_ivlen = 12;
1098 else
1099 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001100
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001101 /* Minimum length of encrypted record */
1102 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001103 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001104 }
1105 else
Hanno Becker8031d062018-01-03 15:32:31 +00001106#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1107#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1108 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1109 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001110 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001111 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1113 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001116 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001119 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001120 mac_key_len = mbedtls_md_get_size( md_info );
1121 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001124 /*
1125 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1126 * (rfc 6066 page 13 or rfc 2104 section 4),
1127 * so we only need to adjust the length here.
1128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001132
1133#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1134 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001135 * HMAC implementation which also truncates the key
1136 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001137 mac_key_len = transform->maclen;
1138#endif
1139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001141
1142 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001143 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001145 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001147 transform->minlen = transform->maclen;
1148 else
Paul Bakker68884e32013-01-07 18:20:04 +01001149 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001150 /*
1151 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001152 * 1. if EtM is in use: one block plus MAC
1153 * otherwise: * first multiple of blocklen greater than maclen
1154 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1157 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001158 {
1159 transform->minlen = transform->maclen
1160 + cipher_info->block_size;
1161 }
1162 else
1163#endif
1164 {
1165 transform->minlen = transform->maclen
1166 + cipher_info->block_size
1167 - transform->maclen % cipher_info->block_size;
1168 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1171 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1172 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001173 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001174 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1177 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1178 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001179 {
1180 transform->minlen += transform->ivlen;
1181 }
1182 else
1183#endif
1184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001186 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1187 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 }
Hanno Becker8031d062018-01-03 15:32:31 +00001191 else
1192#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1193 {
1194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1196 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Hanno Becker88aaf652017-12-27 08:17:40 +00001198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1199 (unsigned) keylen,
1200 (unsigned) transform->minlen,
1201 (unsigned) transform->ivlen,
1202 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
1204 /*
1205 * Finally setup the cipher contexts, IVs and MAC secrets.
1206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001208 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001210 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001211 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
Paul Bakker68884e32013-01-07 18:20:04 +01001213 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001214 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 /*
1217 * This is not used in TLS v1.1.
1218 */
Paul Bakker48916f92012-09-16 19:57:18 +00001219 iv_copy_len = ( transform->fixed_ivlen ) ?
1220 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001221 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1222 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001223 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 }
1225 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#endif /* MBEDTLS_SSL_CLI_C */
1227#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001228 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001230 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001231 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Hanno Becker81c7b182017-11-09 18:39:33 +00001233 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001234 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001235
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236 /*
1237 * This is not used in TLS v1.1.
1238 */
Paul Bakker48916f92012-09-16 19:57:18 +00001239 iv_copy_len = ( transform->fixed_ivlen ) ?
1240 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001241 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1242 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001243 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001249 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1250 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001251 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Hanno Beckerd56ed242018-01-03 15:32:51 +00001253#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_PROTO_SSL3)
1255 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001256 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001257 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001260 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1261 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001262 }
1263
Hanno Becker81c7b182017-11-09 18:39:33 +00001264 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1265 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001266 }
1267 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1269#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1270 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1271 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001272 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001273 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1274 For AEAD-based ciphersuites, there is nothing to do here. */
1275 if( mac_key_len != 0 )
1276 {
1277 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1278 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1279 }
Paul Bakker68884e32013-01-07 18:20:04 +01001280 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001281 else
1282#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001285 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1286 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001287 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001288#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1291 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001292 {
1293 int ret = 0;
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001296
Hanno Becker88aaf652017-12-27 08:17:40 +00001297 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001298 transform->iv_enc, transform->iv_dec,
1299 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001300 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001301 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001304 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1305 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001306 }
1307 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001308#else
1309 ((void) mac_dec);
1310 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001312
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001313#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1314 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001315 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001316 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1317 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001318 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001319 iv_copy_len );
1320 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001321
1322 if( ssl->conf->f_export_keys_ext != NULL )
1323 {
1324 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1325 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001326 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001327 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001328 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001329 handshake->randbytes,
1330 tls_prf_type);
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001331 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001332#endif
1333
Hanno Beckerf704bef2018-11-16 15:21:18 +00001334#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001335
1336 /* Only use PSA-based ciphers for TLS-1.2.
1337 * That's relevant at least for TLS-1.0, where
1338 * we assume that mbedtls_cipher_crypt() updates
1339 * the structure field for the IV, which the PSA-based
1340 * implementation currently doesn't. */
1341#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1342 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001343 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001344 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001345 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001346 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1347 {
1348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001349 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001350 }
1351
1352 if( ret == 0 )
1353 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001355 psa_fallthrough = 0;
1356 }
1357 else
1358 {
1359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1360 psa_fallthrough = 1;
1361 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001362 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001363 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001364 psa_fallthrough = 1;
1365#else
1366 psa_fallthrough = 1;
1367#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001368
Hanno Beckercb1cc802018-11-17 22:27:38 +00001369 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001370#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001371 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001372 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001375 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001376 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001377
Hanno Beckerf704bef2018-11-16 15:21:18 +00001378#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001379 /* Only use PSA-based ciphers for TLS-1.2.
1380 * That's relevant at least for TLS-1.0, where
1381 * we assume that mbedtls_cipher_crypt() updates
1382 * the structure field for the IV, which the PSA-based
1383 * implementation currently doesn't. */
1384#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1385 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001386 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001387 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001388 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001389 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1390 {
1391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001392 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001393 }
1394
1395 if( ret == 0 )
1396 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001398 psa_fallthrough = 0;
1399 }
1400 else
1401 {
1402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1403 psa_fallthrough = 1;
1404 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001405 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001406 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001407 psa_fallthrough = 1;
1408#else
1409 psa_fallthrough = 1;
1410#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001411
Hanno Beckercb1cc802018-11-17 22:27:38 +00001412 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001413#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001414 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001415 cipher_info ) ) != 0 )
1416 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001418 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001419 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001422 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001426 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001427 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001430 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001434 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001435 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_CIPHER_MODE_CBC)
1438 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1441 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001444 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001445 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1448 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001451 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001458 // Initialize compression
1459 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001461 {
Paul Bakker16770332013-10-11 09:59:44 +02001462 if( ssl->compress_buf == NULL )
1463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001465 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001466 if( ssl->compress_buf == NULL )
1467 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001469 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001470 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1471 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001472 }
1473 }
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001476
Paul Bakker48916f92012-09-16 19:57:18 +00001477 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1478 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001479
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001480 if( deflateInit( &transform->ctx_deflate,
1481 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001482 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001485 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1486 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001487 }
1488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001492end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001493 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1494 mbedtls_platform_zeroize( handshake->randbytes,
1495 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001496 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001497}
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#if defined(MBEDTLS_SSL_PROTO_SSL3)
1500void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001501{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001502 mbedtls_md5_context md5;
1503 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 unsigned char pad_1[48];
1505 unsigned char pad_2[48];
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001509 mbedtls_md5_init( &md5 );
1510 mbedtls_sha1_init( &sha1 );
1511
1512 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1513 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
Paul Bakker380da532012-04-18 16:10:25 +00001515 memset( pad_1, 0x36, 48 );
1516 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001518 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1519 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1520 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001522 mbedtls_md5_starts_ret( &md5 );
1523 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1524 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1525 mbedtls_md5_update_ret( &md5, hash, 16 );
1526 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001527
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001528 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1529 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1530 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001531
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001532 mbedtls_sha1_starts_ret( &sha1 );
1533 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1534 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1535 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1536 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001540
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001541 mbedtls_md5_free( &md5 );
1542 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001543
Paul Bakker380da532012-04-18 16:10:25 +00001544 return;
1545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1549void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001550{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001551 mbedtls_md5_context md5;
1552 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001555
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001556 mbedtls_md5_init( &md5 );
1557 mbedtls_sha1_init( &sha1 );
1558
1559 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1560 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001561
Andrzej Kurekeb342242019-01-29 09:14:33 -05001562 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001563 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001567
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001568 mbedtls_md5_free( &md5 );
1569 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001570
Paul Bakker380da532012-04-18 16:10:25 +00001571 return;
1572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1576#if defined(MBEDTLS_SHA256_C)
1577void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001578{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001579#if defined(MBEDTLS_USE_PSA_CRYPTO)
1580 size_t hash_size;
1581 psa_status_t status;
1582 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1583
1584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1585 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1586 if( status != PSA_SUCCESS )
1587 {
1588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1589 return;
1590 }
1591
1592 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1593 if( status != PSA_SUCCESS )
1594 {
1595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1596 return;
1597 }
1598 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1600#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001601 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001602
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001603 mbedtls_sha256_init( &sha256 );
1604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001606
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001607 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001608 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001612
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001613 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001614#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001615 return;
1616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#if defined(MBEDTLS_SHA512_C)
1620void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001621{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001622#if defined(MBEDTLS_USE_PSA_CRYPTO)
1623 size_t hash_size;
1624 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001625 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001626
1627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001628 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001629 if( status != PSA_SUCCESS )
1630 {
1631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1632 return;
1633 }
1634
Andrzej Kurek972fba52019-01-30 03:29:12 -05001635 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001636 if( status != PSA_SUCCESS )
1637 {
1638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1639 return;
1640 }
1641 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1643#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001644 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001645
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001646 mbedtls_sha512_init( &sha512 );
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001649
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001650 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001651 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001656 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001657#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001658 return;
1659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_SHA512_C */
1661#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1664int 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 +02001665{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001666 unsigned char *p = ssl->handshake->premaster;
1667 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001668 const unsigned char *psk = ssl->conf->psk;
1669 size_t psk_len = ssl->conf->psk_len;
1670
1671 /* If the psk callback was called, use its result */
1672 if( ssl->handshake->psk != NULL )
1673 {
1674 psk = ssl->handshake->psk;
1675 psk_len = ssl->handshake->psk_len;
1676 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001677
1678 /*
1679 * PMS = struct {
1680 * opaque other_secret<0..2^16-1>;
1681 * opaque psk<0..2^16-1>;
1682 * };
1683 * with "other_secret" depending on the particular key exchange
1684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1686 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001687 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001688 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001690
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001691 *(p++) = (unsigned char)( psk_len >> 8 );
1692 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001693
1694 if( end < p || (size_t)( end - p ) < psk_len )
1695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1696
1697 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001698 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001699 }
1700 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1702#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1703 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001704 {
1705 /*
1706 * other_secret already set by the ClientKeyExchange message,
1707 * and is 48 bytes long
1708 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001709 if( end - p < 2 )
1710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1711
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001712 *p++ = 0;
1713 *p++ = 48;
1714 p += 48;
1715 }
1716 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1718#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1719 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001720 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001721 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001722 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001723
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001724 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001726 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001727 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001730 return( ret );
1731 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001732 *(p++) = (unsigned char)( len >> 8 );
1733 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001734 p += len;
1735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001737 }
1738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1740#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1741 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001742 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001743 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001744 size_t zlen;
1745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001747 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001748 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001751 return( ret );
1752 }
1753
1754 *(p++) = (unsigned char)( zlen >> 8 );
1755 *(p++) = (unsigned char)( zlen );
1756 p += zlen;
1757
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001758 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1759 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001760 }
1761 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001766 }
1767
1768 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001769 if( end - p < 2 )
1770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001771
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001772 *(p++) = (unsigned char)( psk_len >> 8 );
1773 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001774
1775 if( end < p || (size_t)( end - p ) < psk_len )
1776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1777
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001778 memcpy( p, psk, psk_len );
1779 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001780
1781 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1782
1783 return( 0 );
1784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001788/*
1789 * SSLv3.0 MAC functions
1790 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001791#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001792static void ssl_mac( mbedtls_md_context_t *md_ctx,
1793 const unsigned char *secret,
1794 const unsigned char *buf, size_t len,
1795 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001796 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001797{
1798 unsigned char header[11];
1799 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001800 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1802 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001803
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001804 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001806 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001807 else
Paul Bakker68884e32013-01-07 18:20:04 +01001808 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
1810 memcpy( header, ctr, 8 );
1811 header[ 8] = (unsigned char) type;
1812 header[ 9] = (unsigned char)( len >> 8 );
1813 header[10] = (unsigned char)( len );
1814
Paul Bakker68884e32013-01-07 18:20:04 +01001815 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 mbedtls_md_starts( md_ctx );
1817 mbedtls_md_update( md_ctx, secret, md_size );
1818 mbedtls_md_update( md_ctx, padding, padlen );
1819 mbedtls_md_update( md_ctx, header, 11 );
1820 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001821 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Paul Bakker68884e32013-01-07 18:20:04 +01001823 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 mbedtls_md_starts( md_ctx );
1825 mbedtls_md_update( md_ctx, secret, md_size );
1826 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001827 mbedtls_md_update( md_ctx, out, md_size );
1828 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001831
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001832/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001833 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001834#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001835 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1836 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1837 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1838/* This function makes sure every byte in the memory region is accessed
1839 * (in ascending addresses order) */
1840static void ssl_read_memory( unsigned char *p, size_t len )
1841{
1842 unsigned char acc = 0;
1843 volatile unsigned char force;
1844
1845 for( ; len != 0; p++, len-- )
1846 acc ^= *p;
1847
1848 force = acc;
1849 (void) force;
1850}
1851#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1852
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001853/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001855 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001856
1857static void ssl_extract_add_data_from_record( unsigned char* add_data,
1858 mbedtls_record *rec )
1859{
1860 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1861 add_data[8] = rec->type;
1862 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1863 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1864 add_data[12] = rec->data_len & 0xFF;
1865}
1866
Hanno Beckera18d1322018-01-03 14:27:32 +00001867int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1868 mbedtls_ssl_transform *transform,
1869 mbedtls_record *rec,
1870 int (*f_rng)(void *, unsigned char *, size_t),
1871 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001874 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001875 unsigned char * data;
1876 unsigned char add_data[13];
1877 size_t post_avail;
1878
1879 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001880#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001881 ((void) ssl);
1882#endif
1883
1884 /* The PRNG is used for dynamic IV generation that's used
1885 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1886#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1887 ( defined(MBEDTLS_AES_C) || \
1888 defined(MBEDTLS_ARIA_C) || \
1889 defined(MBEDTLS_CAMELLIA_C) ) && \
1890 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1891 ((void) f_rng);
1892 ((void) p_rng);
1893#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001897 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001898 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1901 }
1902 if( rec == NULL ||
1903 rec->buf == NULL ||
1904 rec->buf_len < rec->data_offset ||
1905 rec->buf_len - rec->data_offset < rec->data_len )
1906 {
1907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001909 }
1910
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001911 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1912 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001914 data, rec->data_len );
1915
1916 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1917
1918 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1919 {
1920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1921 (unsigned) rec->data_len,
1922 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1923 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1924 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001927 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 */
Hanno Becker52344c22018-01-03 15:24:20 +00001929#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 if( mode == MBEDTLS_MODE_STREAM ||
1931 ( mode == MBEDTLS_MODE_CBC
1932#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001933 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001934#endif
1935 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001937 if( post_avail < transform->maclen )
1938 {
1939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1940 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1941 }
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001944 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001945 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001946 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001947 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1948 data, rec->data_len, rec->ctr, rec->type, mac );
1949 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001950 }
1951 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1954 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001955 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001956 {
Hanno Becker992b6872017-11-09 18:57:39 +00001957 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1958
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001959 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001960
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001961 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1962 sizeof( add_data ) );
1963 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1964 data, rec->data_len );
1965 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1966 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1967
1968 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001969 }
1970 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001971#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001975 }
1976
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001977 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1978 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001979
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001980 rec->data_len += transform->maclen;
1981 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001982 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001983 }
Hanno Becker52344c22018-01-03 15:24:20 +00001984#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001986 /*
1987 * Encrypt
1988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1990 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001992 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001993 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001995 "including %d bytes of padding",
1996 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001998 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1999 transform->iv_enc, transform->ivlen,
2000 data, rec->data_len,
2001 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002004 return( ret );
2005 }
2006
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002007 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 }
Paul Bakker68884e32013-01-07 18:20:04 +01002013 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002015
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002016#if defined(MBEDTLS_GCM_C) || \
2017 defined(MBEDTLS_CCM_C) || \
2018 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 mode == MBEDTLS_MODE_CCM ||
2021 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002022 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002023 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002024 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002025 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002026
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002027 /* Check that there's space for both the authentication tag
2028 * and the explicit IV before and after the record content. */
2029 if( post_avail < transform->taglen ||
2030 rec->data_offset < explicit_iv_len )
2031 {
2032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2033 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2034 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002035
Paul Bakker68884e32013-01-07 18:20:04 +01002036 /*
2037 * Generate IV
2038 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002039 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2040 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002041 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002042 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002043 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2044 explicit_iv_len );
2045 /* Prefix record content with explicit IV. */
2046 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002047 }
2048 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2049 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002050 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002051 unsigned char i;
2052
2053 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2054
2055 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002056 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002057 }
2058 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002059 {
2060 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002063 }
2064
Hanno Becker1f10d762019-04-26 13:34:37 +01002065 ssl_extract_add_data_from_record( add_data, rec );
2066
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2068 iv, transform->ivlen );
2069 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002070 data - explicit_iv_len, explicit_iv_len );
2071 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2072 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002074 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002075 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002076
Paul Bakker68884e32013-01-07 18:20:04 +01002077 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002078 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002079 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002080
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002081 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002082 iv, transform->ivlen,
2083 add_data, 13, /* add data */
2084 data, rec->data_len, /* source */
2085 data, &rec->data_len, /* destination */
2086 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002089 return( ret );
2090 }
2091
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002092 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2093 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002094
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002095 rec->data_len += transform->taglen + explicit_iv_len;
2096 rec->data_offset -= explicit_iv_len;
2097 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002098 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2102#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002103 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002106 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002107 size_t padlen, i;
2108 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002109
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002110 /* Currently we're always using minimal padding
2111 * (up to 255 bytes would be allowed). */
2112 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2113 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 padlen = 0;
2115
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002116 /* Check there's enough space in the buffer for the padding. */
2117 if( post_avail < padlen + 1 )
2118 {
2119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2120 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2121 }
2122
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002124 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002126 rec->data_len += padlen + 1;
2127 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002130 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002131 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2132 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002133 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002135 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 if( f_rng == NULL )
2137 {
2138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2139 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2140 }
2141
2142 if( rec->data_offset < transform->ivlen )
2143 {
2144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2145 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2146 }
2147
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002148 /*
2149 * Generate IV
2150 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002151 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002152 if( ret != 0 )
2153 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002154
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002155 memcpy( data - transform->ivlen, transform->iv_enc,
2156 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002157
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002162 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002163 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002164 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002166 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2167 transform->iv_enc,
2168 transform->ivlen,
2169 data, rec->data_len,
2170 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002173 return( ret );
2174 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002176 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002180 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002183 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002184 {
2185 /*
2186 * Save IV in SSL3 and TLS1
2187 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002188 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2189 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002191 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002192#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002193 {
2194 data -= transform->ivlen;
2195 rec->data_offset -= transform->ivlen;
2196 rec->data_len += transform->ivlen;
2197 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002200 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002201 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002202 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2203
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002204 /*
2205 * MAC(MAC_write_key, seq_num +
2206 * TLSCipherText.type +
2207 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002208 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002209 * IV + // except for TLS 1.0
2210 * ENC(content + padding + padding_length));
2211 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002212
2213 if( post_avail < transform->maclen)
2214 {
2215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2216 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2217 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002218
Hanno Becker1f10d762019-04-26 13:34:37 +01002219 ssl_extract_add_data_from_record( add_data, rec );
2220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002222 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2223 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002224
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002225 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2226 sizeof( add_data ) );
2227 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2228 data, rec->data_len );
2229 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2230 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002231
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002233
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 rec->data_len += transform->maclen;
2235 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002236 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002237 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002240 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002242 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002246 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002248 /* Make extra sure authentication was performed, exactly once */
2249 if( auth_done != 1 )
2250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2252 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002253 }
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
2257 return( 0 );
2258}
2259
Hanno Beckera18d1322018-01-03 14:27:32 +00002260int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2261 mbedtls_ssl_transform *transform,
2262 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002264 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002266 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002267#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002268 size_t padlen = 0, correct = 1;
2269#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002270 unsigned char* data;
2271 unsigned char add_data[13];
2272
Hanno Beckera18d1322018-01-03 14:27:32 +00002273#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002274 ((void) ssl);
2275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002278 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002279 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2282 }
2283 if( rec == NULL ||
2284 rec->buf == NULL ||
2285 rec->buf_len < rec->data_offset ||
2286 rec->buf_len - rec->data_offset < rec->data_len )
2287 {
2288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002290 }
2291
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002292 data = rec->buf + rec->data_offset;
2293 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2296 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002297 {
2298 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002299 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2300 transform->iv_dec,
2301 transform->ivlen,
2302 data, rec->data_len,
2303 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002306 return( ret );
2307 }
2308
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002309 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2312 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 }
Paul Bakker68884e32013-01-07 18:20:04 +01002315 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002317#if defined(MBEDTLS_GCM_C) || \
2318 defined(MBEDTLS_CCM_C) || \
2319 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002321 mode == MBEDTLS_MODE_CCM ||
2322 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002323 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002324 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002326
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002327 /*
2328 * Compute and update sizes
2329 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002330 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002333 "+ taglen (%d)", rec->data_len,
2334 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002336 }
Paul Bakker68884e32013-01-07 18:20:04 +01002337
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 /*
2339 * Prepare IV
2340 */
2341 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2342 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002343 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002344 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002345 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002346
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 }
2348 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2349 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002350 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 unsigned char i;
2352
2353 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2354
2355 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002356 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 }
2358 else
2359 {
2360 /* Reminder if we ever add an AEAD mode with a different size */
2361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2363 }
2364
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002365 data += explicit_iv_len;
2366 rec->data_offset += explicit_iv_len;
2367 rec->data_len -= explicit_iv_len + transform->taglen;
2368
2369 ssl_extract_add_data_from_record( add_data, rec );
2370 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2371 add_data, 13 );
2372
2373 memcpy( transform->iv_dec + transform->fixed_ivlen,
2374 data - explicit_iv_len, explicit_iv_len );
2375
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002376 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002377 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002378 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002379
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002380
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002381 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002382 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002383 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002384 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2385 iv, transform->ivlen,
2386 add_data, 13,
2387 data, rec->data_len,
2388 data, &olen,
2389 data + rec->data_len,
2390 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2395 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002396
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002397 return( ret );
2398 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002399 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002400
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002401 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2404 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002405 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2409#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002410 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002413 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002414
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 /*
Paul Bakker45829992013-01-03 14:52:21 +01002416 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002419 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2420 {
2421 /* The ciphertext is prefixed with the CBC IV. */
2422 minlen += transform->ivlen;
2423 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002424#endif
Paul Bakker45829992013-01-03 14:52:21 +01002425
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002426 /* Size considerations:
2427 *
2428 * - The CBC cipher text must not be empty and hence
2429 * at least of size transform->ivlen.
2430 *
2431 * Together with the potential IV-prefix, this explains
2432 * the first of the two checks below.
2433 *
2434 * - The record must contain a MAC, either in plain or
2435 * encrypted, depending on whether Encrypt-then-MAC
2436 * is used or not.
2437 * - If it is, the message contains the IV-prefix,
2438 * the CBC ciphertext, and the MAC.
2439 * - If it is not, the padded plaintext, and hence
2440 * the CBC ciphertext, has at least length maclen + 1
2441 * because there is at least the padding length byte.
2442 *
2443 * As the CBC ciphertext is not empty, both cases give the
2444 * lower bound minlen + maclen + 1 on the record size, which
2445 * we test for in the second check below.
2446 */
2447 if( rec->data_len < minlen + transform->ivlen ||
2448 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002451 "+ 1 ) ( + expl IV )", rec->data_len,
2452 transform->ivlen,
2453 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002455 }
2456
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002457 /*
2458 * Authenticate before decrypt if enabled
2459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002461 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002462 {
Hanno Becker992b6872017-11-09 18:57:39 +00002463 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002466
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002467 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2468 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002469
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002470 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002471
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002472 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2473 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2474 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2475 data, rec->data_len );
2476 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2477 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002478
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002479 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2480 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002481 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002482 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002483
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002484 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2485 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002489 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002490 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493
2494 /*
2495 * Check length sanity
2496 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002497 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002500 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 }
2503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002505 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002506 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002507 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002508 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002509 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002510 /* This is safe because data_len >= minlen + maclen + 1 initially,
2511 * and at this point we have at most subtracted maclen (note that
2512 * minlen == transform->ivlen here). */
2513 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002514
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002515 data += transform->ivlen;
2516 rec->data_offset += transform->ivlen;
2517 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002520
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002521 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2522 transform->iv_dec, transform->ivlen,
2523 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002526 return( ret );
2527 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002528
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002529 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002533 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002536 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002537 {
2538 /*
2539 * Save IV in SSL3 and TLS1
2540 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002541 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2542 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 /* Safe since data_len >= minlen + maclen + 1, so after having
2547 * subtracted at most minlen and maclen up to this point,
2548 * data_len > 0. */
2549 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002550
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002551 if( auth_done == 1 )
2552 {
2553 correct *= ( rec->data_len >= padlen + 1 );
2554 padlen *= ( rec->data_len >= padlen + 1 );
2555 }
2556 else
Paul Bakker45829992013-01-03 14:52:21 +01002557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002559 if( rec->data_len < transform->maclen + padlen + 1 )
2560 {
2561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2562 rec->data_len,
2563 transform->maclen,
2564 padlen + 1 ) );
2565 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002566#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002567
2568 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2569 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002572 padlen++;
2573
2574 /* Regardless of the validity of the padding,
2575 * we have data_len >= padlen here. */
2576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002578 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002580 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582#if defined(MBEDTLS_SSL_DEBUG_ALL)
2583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002584 "should be no more than %d",
2585 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002586#endif
Paul Bakker45829992013-01-03 14:52:21 +01002587 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 }
2589 }
2590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2592#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2593 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002594 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002596 /* The padding check involves a series of up to 256
2597 * consecutive memory reads at the end of the record
2598 * plaintext buffer. In order to hide the length and
2599 * validity of the padding, always perform exactly
2600 * `min(256,plaintext_len)` reads (but take into account
2601 * only the last `padlen` bytes for the padding check). */
2602 size_t pad_count = 0;
2603 size_t real_count = 0;
2604 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002605
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002606 /* Index of first padding byte; it has been ensured above
2607 * that the subtraction is safe. */
2608 size_t const padding_idx = rec->data_len - padlen;
2609 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2610 size_t const start_idx = rec->data_len - num_checks;
2611 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002612
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002613 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002614 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002615 real_count |= ( idx >= padding_idx );
2616 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002617 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002621 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002623#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002624 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002626 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2628 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2631 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002632 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002633
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002634 /* If the padding was found to be invalid, padlen == 0
2635 * and the subtraction is safe. If the padding was found valid,
2636 * padlen hasn't been changed and the previous assertion
2637 * data_len >= padlen still holds. */
2638 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002640 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002642 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2645 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002646 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002648#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002650 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002651#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
2653 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002654 * Authenticate if not done yet.
2655 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 */
Hanno Becker52344c22018-01-03 15:24:20 +00002657#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002658 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002659 {
Hanno Becker992b6872017-11-09 18:57:39 +00002660 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002661
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002662 /* If the initial value of padlen was such that
2663 * data_len < maclen + padlen + 1, then padlen
2664 * got reset to 1, and the initial check
2665 * data_len >= minlen + maclen + 1
2666 * guarantees that at this point we still
2667 * have at least data_len >= maclen.
2668 *
2669 * If the initial value of padlen was such that
2670 * data_len >= maclen + padlen + 1, then we have
2671 * subtracted either padlen + 1 (if the padding was correct)
2672 * or 0 (if the padding was incorrect) since then,
2673 * hence data_len >= maclen in any case.
2674 */
2675 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002677 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002681 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002682 ssl_mac( &transform->md_ctx_dec,
2683 transform->mac_dec,
2684 data, rec->data_len,
2685 rec->ctr, rec->type,
2686 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002687 }
2688 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2690#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2691 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002692 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002693 {
2694 /*
2695 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002696 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 *
2698 * Known timing attacks:
2699 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2700 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002701 * To compensate for different timings for the MAC calculation
2702 * depending on how much padding was removed (which is determined
2703 * by padlen), process extra_run more blocks through the hash
2704 * function.
2705 *
2706 * The formula in the paper is
2707 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2708 * where L1 is the size of the header plus the decrypted message
2709 * plus CBC padding and L2 is the size of the header plus the
2710 * decrypted message. This is for an underlying hash function
2711 * with 64-byte blocks.
2712 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2713 * correctly. We round down instead of up, so -56 is the correct
2714 * value for our calculations instead of -55.
2715 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002716 * Repeat the formula rather than defining a block_size variable.
2717 * This avoids requiring division by a variable at runtime
2718 * (which would be marginally less efficient and would require
2719 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002720 */
2721 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002722 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002723
2724 /*
2725 * The next two sizes are the minimum and maximum values of
2726 * in_msglen over all padlen values.
2727 *
2728 * They're independent of padlen, since we previously did
2729 * in_msglen -= padlen.
2730 *
2731 * Note that max_len + maclen is never more than the buffer
2732 * length, as we previously did in_msglen -= maclen too.
2733 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002735 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2736
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002737 memset( tmp, 0, sizeof( tmp ) );
2738
2739 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002740 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002741#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2742 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002743 case MBEDTLS_MD_MD5:
2744 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002745 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002746 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002747 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2748 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002749 break;
2750#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002751#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002752 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002753 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002754 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2755 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 break;
2757#endif
2758 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002760 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2761 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002762
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002763 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002764
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002765 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2766 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2767 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002768 /* Make sure we access everything even when padlen > 0. This
2769 * makes the synchronisation requirements for just-in-time
2770 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 ssl_read_memory( data + rec->data_len, padlen );
2772 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002773
2774 /* Call mbedtls_md_process at least once due to cache attacks
2775 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002776 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002777 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002778
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002779 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002780
2781 /* Make sure we access all the memory that could contain the MAC,
2782 * before we check it in the next code block. This makes the
2783 * synchronisation requirements for just-in-time Prime+Probe
2784 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 ssl_read_memory( data + min_len,
2786 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002787 }
2788 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2790 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002796#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002797 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2798 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002799#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002801 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2802 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804#if defined(MBEDTLS_SSL_DEBUG_ALL)
2805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002806#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002807 correct = 0;
2808 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002809 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002810 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002811
2812 /*
2813 * Finally check the correct flag
2814 */
2815 if( correct == 0 )
2816 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002817#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002818
2819 /* Make extra sure authentication was performed, exactly once */
2820 if( auth_done != 1 )
2821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2823 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
2828 return( 0 );
2829}
2830
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002831#undef MAC_NONE
2832#undef MAC_PLAINTEXT
2833#undef MAC_CIPHERTEXT
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002836/*
2837 * Compression/decompression functions
2838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002840{
2841 int ret;
2842 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002843 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002844 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002845 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002848
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002849 if( len_pre == 0 )
2850 return( 0 );
2851
Paul Bakker2770fbd2012-07-03 13:30:23 +00002852 memcpy( msg_pre, ssl->out_msg, len_pre );
2853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002855 ssl->out_msglen ) );
2856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002858 ssl->out_msg, ssl->out_msglen );
2859
Paul Bakker48916f92012-09-16 19:57:18 +00002860 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2861 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2862 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002863 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002864
Paul Bakker48916f92012-09-16 19:57:18 +00002865 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002866 if( ret != Z_OK )
2867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2869 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870 }
2871
Angus Grattond8213d02016-05-25 20:56:48 +10002872 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002873 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002876 ssl->out_msglen ) );
2877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002879 ssl->out_msg, ssl->out_msglen );
2880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002882
2883 return( 0 );
2884}
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887{
2888 int ret;
2889 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002890 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002891 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002892 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002896 if( len_pre == 0 )
2897 return( 0 );
2898
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 memcpy( msg_pre, ssl->in_msg, len_pre );
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 ssl->in_msglen ) );
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 ssl->in_msg, ssl->in_msglen );
2906
Paul Bakker48916f92012-09-16 19:57:18 +00002907 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2908 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2909 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002910 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002911 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002912
Paul Bakker48916f92012-09-16 19:57:18 +00002913 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914 if( ret != Z_OK )
2915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2917 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002918 }
2919
Angus Grattond8213d02016-05-25 20:56:48 +10002920 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002921 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924 ssl->in_msglen ) );
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927 ssl->in_msg, ssl->in_msglen );
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002930
2931 return( 0 );
2932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2936static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#if defined(MBEDTLS_SSL_PROTO_DTLS)
2939static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002940{
2941 /* If renegotiation is not enforced, retransmit until we would reach max
2942 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002943 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002944 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002945 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002946 unsigned char doublings = 1;
2947
2948 while( ratio != 0 )
2949 {
2950 ++doublings;
2951 ratio >>= 1;
2952 }
2953
2954 if( ++ssl->renego_records_seen > doublings )
2955 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002957 return( 0 );
2958 }
2959 }
2960
2961 return( ssl_write_hello_request( ssl ) );
2962}
2963#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002965
Paul Bakker5121ce52009-01-03 21:22:43 +00002966/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002967 * Fill the input message buffer by appending data to it.
2968 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002969 *
2970 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2971 * available (from this read and/or a previous one). Otherwise, an error code
2972 * is returned (possibly EOF or WANT_READ).
2973 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002974 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2975 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2976 * since we always read a whole datagram at once.
2977 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002978 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002979 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002982{
Paul Bakker23986e52011-04-24 08:57:21 +00002983 int ret;
2984 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002988 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002991 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002993 }
2994
Angus Grattond8213d02016-05-25 20:56:48 +10002995 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002999 }
3000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003002 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003004 uint32_t timeout;
3005
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003006 /* Just to be sure */
3007 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3008 {
3009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3010 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3012 }
3013
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003014 /*
3015 * The point is, we need to always read a full datagram at once, so we
3016 * sometimes read more then requested, and handle the additional data.
3017 * It could be the rest of the current record (while fetching the
3018 * header) and/or some other records in the same datagram.
3019 */
3020
3021 /*
3022 * Move to the next record in the already read datagram if applicable
3023 */
3024 if( ssl->next_record_offset != 0 )
3025 {
3026 if( ssl->in_left < ssl->next_record_offset )
3027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003030 }
3031
3032 ssl->in_left -= ssl->next_record_offset;
3033
3034 if( ssl->in_left != 0 )
3035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003037 ssl->next_record_offset ) );
3038 memmove( ssl->in_hdr,
3039 ssl->in_hdr + ssl->next_record_offset,
3040 ssl->in_left );
3041 }
3042
3043 ssl->next_record_offset = 0;
3044 }
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003048
3049 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003050 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003051 */
3052 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003055 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003056 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003057
3058 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003059 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003060 * are not at the beginning of a new record, the caller did something
3061 * wrong.
3062 */
3063 if( ssl->in_left != 0 )
3064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003067 }
3068
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003069 /*
3070 * Don't even try to read if time's out already.
3071 * This avoids by-passing the timer when repeatedly receiving messages
3072 * that will end up being dropped.
3073 */
3074 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003075 {
3076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003077 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003078 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003079 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003080 {
Angus Grattond8213d02016-05-25 20:56:48 +10003081 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003084 timeout = ssl->handshake->retransmit_timeout;
3085 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003086 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003089
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003090 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003091 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3092 timeout );
3093 else
3094 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003097
3098 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003100 }
3101
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003102 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003105 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003108 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003109 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003112 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003113 }
3114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003118 return( ret );
3119 }
3120
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003121 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003122 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003124 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003126 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003127 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003130 return( ret );
3131 }
3132
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003133 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003136 }
3137
Paul Bakker5121ce52009-01-03 21:22:43 +00003138 if( ret < 0 )
3139 return( ret );
3140
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003141 ssl->in_left = ret;
3142 }
3143 else
3144#endif
3145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003147 ssl->in_left, nb_want ) );
3148
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003149 while( ssl->in_left < nb_want )
3150 {
3151 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003152
3153 if( ssl_check_timer( ssl ) != 0 )
3154 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3155 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003156 {
3157 if( ssl->f_recv_timeout != NULL )
3158 {
3159 ret = ssl->f_recv_timeout( ssl->p_bio,
3160 ssl->in_hdr + ssl->in_left, len,
3161 ssl->conf->read_timeout );
3162 }
3163 else
3164 {
3165 ret = ssl->f_recv( ssl->p_bio,
3166 ssl->in_hdr + ssl->in_left, len );
3167 }
3168 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003171 ssl->in_left, nb_want ) );
3172 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003173
3174 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003176
3177 if( ret < 0 )
3178 return( ret );
3179
mohammad160352aecb92018-03-28 23:41:40 -07003180 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003181 {
Darryl Green11999bb2018-03-13 15:22:58 +00003182 MBEDTLS_SSL_DEBUG_MSG( 1,
3183 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003184 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3186 }
3187
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003188 ssl->in_left += ret;
3189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 }
3191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193
3194 return( 0 );
3195}
3196
3197/*
3198 * Flush any data not yet written
3199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003201{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003202 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003203 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003207 if( ssl->f_send == NULL )
3208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003210 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003212 }
3213
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003214 /* Avoid incrementing counter if data is flushed */
3215 if( ssl->out_left == 0 )
3216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003218 return( 0 );
3219 }
3220
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 while( ssl->out_left > 0 )
3222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3224 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Hanno Becker2b1e3542018-08-06 11:19:13 +01003226 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003227 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
3231 if( ret <= 0 )
3232 return( ret );
3233
mohammad160352aecb92018-03-28 23:41:40 -07003234 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003235 {
Darryl Green11999bb2018-03-13 15:22:58 +00003236 MBEDTLS_SSL_DEBUG_MSG( 1,
3237 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003238 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3240 }
3241
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 ssl->out_left -= ret;
3243 }
3244
Hanno Becker2b1e3542018-08-06 11:19:13 +01003245#if defined(MBEDTLS_SSL_PROTO_DTLS)
3246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003247 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003248 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003249 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003250 else
3251#endif
3252 {
3253 ssl->out_hdr = ssl->out_buf + 8;
3254 }
3255 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
3259 return( 0 );
3260}
3261
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003262/*
3263 * Functions to handle the DTLS retransmission state machine
3264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003266/*
3267 * Append current handshake message to current outgoing flight
3268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3273 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3274 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003275
3276 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003277 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003278 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003281 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003282 }
3283
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003284 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003285 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003288 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003289 }
3290
3291 /* Copy current handshake message with headers */
3292 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3293 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003294 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003295 msg->next = NULL;
3296
3297 /* Append to the current flight */
3298 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003299 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003300 else
3301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003303 while( cur->next != NULL )
3304 cur = cur->next;
3305 cur->next = msg;
3306 }
3307
Hanno Becker3b235902018-08-06 09:54:53 +01003308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309 return( 0 );
3310}
3311
3312/*
3313 * Free the current flight of handshake messages
3314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 mbedtls_ssl_flight_item *cur = flight;
3318 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319
3320 while( cur != NULL )
3321 {
3322 next = cur->next;
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 mbedtls_free( cur->p );
3325 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326
3327 cur = next;
3328 }
3329}
3330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3332static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003333#endif
3334
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003335/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003336 * Swap transform_out and out_ctr with the alternative ones
3337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003341 unsigned char tmp_out_ctr[8];
3342
3343 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003346 return;
3347 }
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003350
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003351 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003352 tmp_transform = ssl->transform_out;
3353 ssl->transform_out = ssl->handshake->alt_transform_out;
3354 ssl->handshake->alt_transform_out = tmp_transform;
3355
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003356 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003357 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3358 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003359 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003360
3361 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003362 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3365 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3370 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003371 }
3372 }
3373#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003374}
3375
3376/*
3377 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003378 */
3379int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3380{
3381 int ret = 0;
3382
3383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3384
3385 ret = mbedtls_ssl_flight_transmit( ssl );
3386
3387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3388
3389 return( ret );
3390}
3391
3392/*
3393 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003394 *
3395 * Need to remember the current message in case flush_output returns
3396 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003397 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003398 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003399int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003400{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003401 int ret;
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003405 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407
3408 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003409 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003410 ssl_swap_epochs( ssl );
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003413 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003414
3415 while( ssl->handshake->cur_msg != NULL )
3416 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003417 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003418 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003419
Hanno Beckere1dcb032018-08-17 16:47:58 +01003420 int const is_finished =
3421 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3422 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3423
Hanno Becker04da1892018-08-14 13:22:10 +01003424 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3425 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3426
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003427 /* Swap epochs before sending Finished: we can't do it after
3428 * sending ChangeCipherSpec, in case write returns WANT_READ.
3429 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003430 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003431 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003433 ssl_swap_epochs( ssl );
3434 }
3435
Hanno Becker67bc7c32018-08-06 11:33:50 +01003436 ret = ssl_get_remaining_payload_in_datagram( ssl );
3437 if( ret < 0 )
3438 return( ret );
3439 max_frag_len = (size_t) ret;
3440
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003441 /* CCS is copied as is, while HS messages may need fragmentation */
3442 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3443 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003444 if( max_frag_len == 0 )
3445 {
3446 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3447 return( ret );
3448
3449 continue;
3450 }
3451
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003452 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003453 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003454 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003456 /* Update position inside current message */
3457 ssl->handshake->cur_msg_p += cur->len;
3458 }
3459 else
3460 {
3461 const unsigned char * const p = ssl->handshake->cur_msg_p;
3462 const size_t hs_len = cur->len - 12;
3463 const size_t frag_off = p - ( cur->p + 12 );
3464 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003466
Hanno Beckere1dcb032018-08-17 16:47:58 +01003467 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003468 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003469 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003470 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003471
Hanno Becker67bc7c32018-08-06 11:33:50 +01003472 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3473 return( ret );
3474
3475 continue;
3476 }
3477 max_hs_frag_len = max_frag_len - 12;
3478
3479 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3480 max_hs_frag_len : rem_len;
3481
3482 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003483 {
3484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003485 (unsigned) cur_hs_frag_len,
3486 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003487 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* Messages are stored with handshake headers as if not fragmented,
3490 * copy beginning of headers then fill fragmentation fields.
3491 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3492 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003493
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003494 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3495 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3496 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3497
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3499 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3500 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003501
3502 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3503
Hanno Becker3f7b9732018-08-28 09:53:25 +01003504 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3506 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003507 ssl->out_msgtype = cur->type;
3508
3509 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003510 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003511 }
3512
3513 /* If done with the current message move to the next one if any */
3514 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3515 {
3516 if( cur->next != NULL )
3517 {
3518 ssl->handshake->cur_msg = cur->next;
3519 ssl->handshake->cur_msg_p = cur->next->p + 12;
3520 }
3521 else
3522 {
3523 ssl->handshake->cur_msg = NULL;
3524 ssl->handshake->cur_msg_p = NULL;
3525 }
3526 }
3527
3528 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003529 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003532 return( ret );
3533 }
3534 }
3535
Hanno Becker67bc7c32018-08-06 11:33:50 +01003536 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3537 return( ret );
3538
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003539 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3541 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003542 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003545 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3546 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003547
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003549
3550 return( 0 );
3551}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003552
3553/*
3554 * To be called when the last message of an incoming flight is received.
3555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003557{
3558 /* We won't need to resend that one any more */
3559 ssl_flight_free( ssl->handshake->flight );
3560 ssl->handshake->flight = NULL;
3561 ssl->handshake->cur_msg = NULL;
3562
3563 /* The next incoming flight will start with this msg_seq */
3564 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3565
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003566 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003567 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003568
Hanno Becker0271f962018-08-16 13:23:47 +01003569 /* Clear future message buffering structure. */
3570 ssl_buffering_free( ssl );
3571
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003572 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003573 ssl_set_timer( ssl, 0 );
3574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3576 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003579 }
3580 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003582}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003583
3584/*
3585 * To be called when the last message of an outgoing flight is send.
3586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003588{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003589 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003590 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3593 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003596 }
3597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003601
Paul Bakker5121ce52009-01-03 21:22:43 +00003602/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003603 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605
3606/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003607 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003608 *
3609 * - fill in handshake headers
3610 * - update handshake checksum
3611 * - DTLS: save message for resending
3612 * - then pass to the record layer
3613 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003614 * DTLS: except for HelloRequest, messages are only queued, and will only be
3615 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003616 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003617 * Inputs:
3618 * - ssl->out_msglen: 4 + actual handshake message len
3619 * (4 is the size of handshake headers for TLS)
3620 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3621 * - ssl->out_msg + 4: the handshake message body
3622 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003623 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003624 * - ssl->out_msglen: the length of the record contents
3625 * (including handshake headers but excluding record headers)
3626 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003627 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003628int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003629{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003630 int ret;
3631 const size_t hs_len = ssl->out_msglen - 4;
3632 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3635
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003636 /*
3637 * Sanity checks
3638 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003639 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003640 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3641 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003642 /* In SSLv3, the client might send a NoCertificate alert. */
3643#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3644 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3645 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3646 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3647#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3648 {
3649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3651 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003652 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003654 /* Whenever we send anything different from a
3655 * HelloRequest we should be in a handshake - double check. */
3656 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3657 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003658 ssl->handshake == NULL )
3659 {
3660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003665 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003666 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003668 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003671 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003672#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003673
Hanno Beckerb50a2532018-08-06 11:52:54 +01003674 /* Double-check that we did not exceed the bounds
3675 * of the outgoing record buffer.
3676 * This should never fail as the various message
3677 * writing functions must obey the bounds of the
3678 * outgoing record buffer, but better be safe.
3679 *
3680 * Note: We deliberately do not check for the MTU or MFL here.
3681 */
3682 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3683 {
3684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3685 "size %u, maximum %u",
3686 (unsigned) ssl->out_msglen,
3687 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3689 }
3690
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003691 /*
3692 * Fill handshake headers
3693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003695 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003696 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3697 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3698 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003699
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003700 /*
3701 * DTLS has additional fields in the Handshake layer,
3702 * between the length field and the actual payload:
3703 * uint16 message_seq;
3704 * uint24 fragment_offset;
3705 * uint24 fragment_length;
3706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003708 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003709 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003710 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003711 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003712 {
3713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3714 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003715 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003716 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003717 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3718 }
3719
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003720 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003721 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003722
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003723 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003724 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003725 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003726 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3727 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3728 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003729 }
3730 else
3731 {
3732 ssl->out_msg[4] = 0;
3733 ssl->out_msg[5] = 0;
3734 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003735
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003736 /* Handshake hashes are computed without fragmentation,
3737 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003738 memset( ssl->out_msg + 6, 0x00, 3 );
3739 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003742
Hanno Becker0207e532018-08-28 10:28:28 +01003743 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3745 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003748 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003751 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3752 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003753 {
3754 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003757 return( ret );
3758 }
3759 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003760 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003761#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003762 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003763 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003764 {
3765 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3766 return( ret );
3767 }
3768 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003769
3770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3771
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003772 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003773}
3774
3775/*
3776 * Record layer functions
3777 */
3778
3779/*
3780 * Write current record.
3781 *
3782 * Uses:
3783 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3784 * - ssl->out_msglen: length of the record content (excl headers)
3785 * - ssl->out_msg: record content
3786 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003787int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003788{
3789 int ret, done = 0;
3790 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003791 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003792
3793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003796 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003798 {
3799 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003802 return( ret );
3803 }
3804
3805 len = ssl->out_msglen;
3806 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3810 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 ret = mbedtls_ssl_hw_record_write( ssl );
3815 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3818 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003819 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003820
3821 if( ret == 0 )
3822 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003823 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003825 if( !done )
3826 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003827 unsigned i;
3828 size_t protected_record_size;
3829
Paul Bakker05ef8352012-05-08 09:17:57 +00003830 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003832 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003833
Hanno Becker19859472018-08-06 09:40:20 +01003834 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003835 ssl->out_len[0] = (unsigned char)( len >> 8 );
3836 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003837
Paul Bakker48916f92012-09-16 19:57:18 +00003838 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003839 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003840 mbedtls_record rec;
3841
3842 rec.buf = ssl->out_iv;
3843 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3844 ( ssl->out_iv - ssl->out_buf );
3845 rec.data_len = ssl->out_msglen;
3846 rec.data_offset = ssl->out_msg - rec.buf;
3847
3848 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3849 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3850 ssl->conf->transport, rec.ver );
3851 rec.type = ssl->out_msgtype;
3852
Hanno Beckera18d1322018-01-03 14:27:32 +00003853 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003854 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003857 return( ret );
3858 }
3859
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003860 if( rec.data_offset != 0 )
3861 {
3862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3864 }
3865
Hanno Becker78f839d2019-03-14 12:56:23 +00003866 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003867 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3868 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003869 }
3870
Hanno Becker2b1e3542018-08-06 11:19:13 +01003871 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3872
3873#if defined(MBEDTLS_SSL_PROTO_DTLS)
3874 /* In case of DTLS, double-check that we don't exceed
3875 * the remaining space in the datagram. */
3876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3877 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003878 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003879 if( ret < 0 )
3880 return( ret );
3881
3882 if( protected_record_size > (size_t) ret )
3883 {
3884 /* Should never happen */
3885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3886 }
3887 }
3888#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003891 "version = [%d:%d], msglen = %d",
3892 ssl->out_hdr[0], ssl->out_hdr[1],
3893 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003896 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003897
3898 ssl->out_left += protected_record_size;
3899 ssl->out_hdr += protected_record_size;
3900 ssl_update_out_pointers( ssl, ssl->transform_out );
3901
Hanno Becker04484622018-08-06 09:49:38 +01003902 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3903 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3904 break;
3905
3906 /* The loop goes to its end iff the counter is wrapping */
3907 if( i == ssl_ep_len( ssl ) )
3908 {
3909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3910 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003912 }
3913
Hanno Becker67bc7c32018-08-06 11:33:50 +01003914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3916 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003917 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003918 size_t remaining;
3919 ret = ssl_get_remaining_payload_in_datagram( ssl );
3920 if( ret < 0 )
3921 {
3922 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3923 ret );
3924 return( ret );
3925 }
3926
3927 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003928 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003929 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003930 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003931 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003932 else
3933 {
Hanno Becker513815a2018-08-20 11:56:09 +01003934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003935 }
3936 }
3937#endif /* MBEDTLS_SSL_PROTO_DTLS */
3938
3939 if( ( flush == SSL_FORCE_FLUSH ) &&
3940 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003943 return( ret );
3944 }
3945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003947
3948 return( 0 );
3949}
3950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003952
3953static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3954{
3955 if( ssl->in_msglen < ssl->in_hslen ||
3956 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3957 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3958 {
3959 return( 1 );
3960 }
3961 return( 0 );
3962}
Hanno Becker44650b72018-08-16 12:51:11 +01003963
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003964static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003965{
3966 return( ( ssl->in_msg[9] << 16 ) |
3967 ( ssl->in_msg[10] << 8 ) |
3968 ssl->in_msg[11] );
3969}
3970
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003971static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003972{
3973 return( ( ssl->in_msg[6] << 16 ) |
3974 ( ssl->in_msg[7] << 8 ) |
3975 ssl->in_msg[8] );
3976}
3977
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003978static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003979{
3980 uint32_t msg_len, frag_off, frag_len;
3981
3982 msg_len = ssl_get_hs_total_len( ssl );
3983 frag_off = ssl_get_hs_frag_off( ssl );
3984 frag_len = ssl_get_hs_frag_len( ssl );
3985
3986 if( frag_off > msg_len )
3987 return( -1 );
3988
3989 if( frag_len > msg_len - frag_off )
3990 return( -1 );
3991
3992 if( frag_len + 12 > ssl->in_msglen )
3993 return( -1 );
3994
3995 return( 0 );
3996}
3997
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003998/*
3999 * Mark bits in bitmask (used for DTLS HS reassembly)
4000 */
4001static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4002{
4003 unsigned int start_bits, end_bits;
4004
4005 start_bits = 8 - ( offset % 8 );
4006 if( start_bits != 8 )
4007 {
4008 size_t first_byte_idx = offset / 8;
4009
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004010 /* Special case */
4011 if( len <= start_bits )
4012 {
4013 for( ; len != 0; len-- )
4014 mask[first_byte_idx] |= 1 << ( start_bits - len );
4015
4016 /* Avoid potential issues with offset or len becoming invalid */
4017 return;
4018 }
4019
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004020 offset += start_bits; /* Now offset % 8 == 0 */
4021 len -= start_bits;
4022
4023 for( ; start_bits != 0; start_bits-- )
4024 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4025 }
4026
4027 end_bits = len % 8;
4028 if( end_bits != 0 )
4029 {
4030 size_t last_byte_idx = ( offset + len ) / 8;
4031
4032 len -= end_bits; /* Now len % 8 == 0 */
4033
4034 for( ; end_bits != 0; end_bits-- )
4035 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4036 }
4037
4038 memset( mask + offset / 8, 0xFF, len / 8 );
4039}
4040
4041/*
4042 * Check that bitmask is full
4043 */
4044static int ssl_bitmask_check( unsigned char *mask, size_t len )
4045{
4046 size_t i;
4047
4048 for( i = 0; i < len / 8; i++ )
4049 if( mask[i] != 0xFF )
4050 return( -1 );
4051
4052 for( i = 0; i < len % 8; i++ )
4053 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4054 return( -1 );
4055
4056 return( 0 );
4057}
4058
Hanno Becker56e205e2018-08-16 09:06:12 +01004059/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004060static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004061 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004062{
Hanno Becker56e205e2018-08-16 09:06:12 +01004063 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004064
Hanno Becker56e205e2018-08-16 09:06:12 +01004065 alloc_len = 12; /* Handshake header */
4066 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004067
Hanno Beckerd07df862018-08-16 09:14:58 +01004068 if( add_bitmap )
4069 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004070
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004071 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004072}
Hanno Becker56e205e2018-08-16 09:06:12 +01004073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004075
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004076static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004077{
4078 return( ( ssl->in_msg[1] << 16 ) |
4079 ( ssl->in_msg[2] << 8 ) |
4080 ssl->in_msg[3] );
4081}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004082
Simon Butcher99000142016-10-13 17:21:01 +01004083int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004088 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004090 }
4091
Hanno Becker12555c62018-08-16 12:47:53 +01004092 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004095 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004096 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004100 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004101 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004102 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004103
Hanno Becker44650b72018-08-16 12:51:11 +01004104 if( ssl_check_hs_header( ssl ) != 0 )
4105 {
4106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4107 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4108 }
4109
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004110 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004111 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4112 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4113 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4114 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004115 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004116 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4117 {
4118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4119 recv_msg_seq,
4120 ssl->handshake->in_msg_seq ) );
4121 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4122 }
4123
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004124 /* Retransmit only on last message from previous flight, to avoid
4125 * too many retransmissions.
4126 * Besides, No sane server ever retransmits HelloVerifyRequest */
4127 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004131 "message_seq = %d, start_of_flight = %d",
4132 recv_msg_seq,
4133 ssl->handshake->in_flight_start_seq ) );
4134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004138 return( ret );
4139 }
4140 }
4141 else
4142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004144 "message_seq = %d, expected = %d",
4145 recv_msg_seq,
4146 ssl->handshake->in_msg_seq ) );
4147 }
4148
Hanno Becker90333da2017-10-10 11:27:13 +01004149 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004150 }
4151 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004152
Hanno Becker6d97ef52018-08-16 13:09:04 +01004153 /* Message reassembly is handled alongside buffering of future
4154 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004155 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004156 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004157 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004160 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004161 }
4162 }
4163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004165 /* With TLS we don't handle fragmentation (for now) */
4166 if( ssl->in_msglen < ssl->in_hslen )
4167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4169 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004170 }
4171
Simon Butcher99000142016-10-13 17:21:01 +01004172 return( 0 );
4173}
4174
4175void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4176{
Hanno Becker0271f962018-08-16 13:23:47 +01004177 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004178
Hanno Becker0271f962018-08-16 13:23:47 +01004179 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004180 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004181 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004182 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004183
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004184 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004187 ssl->handshake != NULL )
4188 {
Hanno Becker0271f962018-08-16 13:23:47 +01004189 unsigned offset;
4190 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004191
Hanno Becker0271f962018-08-16 13:23:47 +01004192 /* Increment handshake sequence number */
4193 hs->in_msg_seq++;
4194
4195 /*
4196 * Clear up handshake buffering and reassembly structure.
4197 */
4198
4199 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004200 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004201
4202 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004203 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4204 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004205 offset++, hs_buf++ )
4206 {
4207 *hs_buf = *(hs_buf + 1);
4208 }
4209
4210 /* Create a fresh last entry */
4211 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004212 }
4213#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004214}
4215
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004216/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004217 * DTLS anti-replay: RFC 6347 4.1.2.6
4218 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004219 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4220 * Bit n is set iff record number in_window_top - n has been seen.
4221 *
4222 * Usually, in_window_top is the last record number seen and the lsb of
4223 * in_window is set. The only exception is the initial state (record number 0
4224 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4227static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004228{
4229 ssl->in_window_top = 0;
4230 ssl->in_window = 0;
4231}
4232
4233static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4234{
4235 return( ( (uint64_t) buf[0] << 40 ) |
4236 ( (uint64_t) buf[1] << 32 ) |
4237 ( (uint64_t) buf[2] << 24 ) |
4238 ( (uint64_t) buf[3] << 16 ) |
4239 ( (uint64_t) buf[4] << 8 ) |
4240 ( (uint64_t) buf[5] ) );
4241}
4242
4243/*
4244 * Return 0 if sequence number is acceptable, -1 otherwise
4245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004247{
4248 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4249 uint64_t bit;
4250
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004251 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004252 return( 0 );
4253
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004254 if( rec_seqnum > ssl->in_window_top )
4255 return( 0 );
4256
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004257 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004258
4259 if( bit >= 64 )
4260 return( -1 );
4261
4262 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4263 return( -1 );
4264
4265 return( 0 );
4266}
4267
4268/*
4269 * Update replay window on new validated record
4270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004272{
4273 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4274
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004275 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004276 return;
4277
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004278 if( rec_seqnum > ssl->in_window_top )
4279 {
4280 /* Update window_top and the contents of the window */
4281 uint64_t shift = rec_seqnum - ssl->in_window_top;
4282
4283 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004284 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004285 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004286 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004287 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004288 ssl->in_window |= 1;
4289 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004290
4291 ssl->in_window_top = rec_seqnum;
4292 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 else
4294 {
4295 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004296 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004297
4298 if( bit < 64 ) /* Always true, but be extra sure */
4299 ssl->in_window |= (uint64_t) 1 << bit;
4300 }
4301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004303
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004304#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004305/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004306static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4307
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004308/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004309 * Without any SSL context, check if a datagram looks like a ClientHello with
4310 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004311 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004312 *
4313 * - if cookie is valid, return 0
4314 * - if ClientHello looks superficially valid but cookie is not,
4315 * fill obuf and set olen, then
4316 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4317 * - otherwise return a specific error code
4318 */
4319static int ssl_check_dtls_clihlo_cookie(
4320 mbedtls_ssl_cookie_write_t *f_cookie_write,
4321 mbedtls_ssl_cookie_check_t *f_cookie_check,
4322 void *p_cookie,
4323 const unsigned char *cli_id, size_t cli_id_len,
4324 const unsigned char *in, size_t in_len,
4325 unsigned char *obuf, size_t buf_len, size_t *olen )
4326{
4327 size_t sid_len, cookie_len;
4328 unsigned char *p;
4329
4330 if( f_cookie_write == NULL || f_cookie_check == NULL )
4331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4332
4333 /*
4334 * Structure of ClientHello with record and handshake headers,
4335 * and expected values. We don't need to check a lot, more checks will be
4336 * done when actually parsing the ClientHello - skipping those checks
4337 * avoids code duplication and does not make cookie forging any easier.
4338 *
4339 * 0-0 ContentType type; copied, must be handshake
4340 * 1-2 ProtocolVersion version; copied
4341 * 3-4 uint16 epoch; copied, must be 0
4342 * 5-10 uint48 sequence_number; copied
4343 * 11-12 uint16 length; (ignored)
4344 *
4345 * 13-13 HandshakeType msg_type; (ignored)
4346 * 14-16 uint24 length; (ignored)
4347 * 17-18 uint16 message_seq; copied
4348 * 19-21 uint24 fragment_offset; copied, must be 0
4349 * 22-24 uint24 fragment_length; (ignored)
4350 *
4351 * 25-26 ProtocolVersion client_version; (ignored)
4352 * 27-58 Random random; (ignored)
4353 * 59-xx SessionID session_id; 1 byte len + sid_len content
4354 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4355 * ...
4356 *
4357 * Minimum length is 61 bytes.
4358 */
4359 if( in_len < 61 ||
4360 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4361 in[3] != 0 || in[4] != 0 ||
4362 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4363 {
4364 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4365 }
4366
4367 sid_len = in[59];
4368 if( sid_len > in_len - 61 )
4369 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4370
4371 cookie_len = in[60 + sid_len];
4372 if( cookie_len > in_len - 60 )
4373 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4374
4375 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4376 cli_id, cli_id_len ) == 0 )
4377 {
4378 /* Valid cookie */
4379 return( 0 );
4380 }
4381
4382 /*
4383 * If we get here, we've got an invalid cookie, let's prepare HVR.
4384 *
4385 * 0-0 ContentType type; copied
4386 * 1-2 ProtocolVersion version; copied
4387 * 3-4 uint16 epoch; copied
4388 * 5-10 uint48 sequence_number; copied
4389 * 11-12 uint16 length; olen - 13
4390 *
4391 * 13-13 HandshakeType msg_type; hello_verify_request
4392 * 14-16 uint24 length; olen - 25
4393 * 17-18 uint16 message_seq; copied
4394 * 19-21 uint24 fragment_offset; copied
4395 * 22-24 uint24 fragment_length; olen - 25
4396 *
4397 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4398 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4399 *
4400 * Minimum length is 28.
4401 */
4402 if( buf_len < 28 )
4403 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4404
4405 /* Copy most fields and adapt others */
4406 memcpy( obuf, in, 25 );
4407 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4408 obuf[25] = 0xfe;
4409 obuf[26] = 0xff;
4410
4411 /* Generate and write actual cookie */
4412 p = obuf + 28;
4413 if( f_cookie_write( p_cookie,
4414 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4415 {
4416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4417 }
4418
4419 *olen = p - obuf;
4420
4421 /* Go back and fill length fields */
4422 obuf[27] = (unsigned char)( *olen - 28 );
4423
4424 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4425 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4426 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4427
4428 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4429 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4430
4431 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4432}
4433
4434/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004435 * Handle possible client reconnect with the same UDP quadruplet
4436 * (RFC 6347 Section 4.2.8).
4437 *
4438 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4439 * that looks like a ClientHello.
4440 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004441 * - if the input looks like a ClientHello without cookies,
4442 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004443 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004444 * - if the input looks like a ClientHello with a valid cookie,
4445 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004446 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004447 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004448 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004449 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004450 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4451 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004452 */
4453static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4454{
4455 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004456 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004457
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004458 ret = ssl_check_dtls_clihlo_cookie(
4459 ssl->conf->f_cookie_write,
4460 ssl->conf->f_cookie_check,
4461 ssl->conf->p_cookie,
4462 ssl->cli_id, ssl->cli_id_len,
4463 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004464 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004465
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004466 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4467
4468 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004469 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004470 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004471 * If the error is permanent we'll catch it later,
4472 * if it's not, then hopefully it'll work next time. */
4473 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4474
4475 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004476 }
4477
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004478 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004479 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004480 /* Got a valid cookie, partially reset context */
4481 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4482 {
4483 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4484 return( ret );
4485 }
4486
4487 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004488 }
4489
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004490 return( ret );
4491}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004492#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004493
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004494/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004495 * ContentType type;
4496 * ProtocolVersion version;
4497 * uint16 epoch; // DTLS only
4498 * uint48 sequence_number; // DTLS only
4499 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004500 *
4501 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004502 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004503 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4504 *
4505 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004506 * 1. proceed with the record if this function returns 0
4507 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4508 * 3. return CLIENT_RECONNECT if this function return that value
4509 * 4. drop the whole datagram if this function returns anything else.
4510 * Point 2 is needed when the peer is resending, and we have already received
4511 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004514{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004515 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 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 +02004518
Paul Bakker5121ce52009-01-03 21:22:43 +00004519 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004520 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004521 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004524 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004525 ssl->in_msgtype,
4526 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004527
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004528 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4530 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4531 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4532 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004535
4536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004537 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4538 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004539 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4540#endif /* MBEDTLS_SSL_PROTO_DTLS */
4541 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4542 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004545 }
4546
4547 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004548 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4551 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 }
4553
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004554 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4557 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004558 }
4559
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004560 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004561 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004562 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4565 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004566 }
4567
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004569 * DTLS-related tests.
4570 * Check epoch before checking length constraint because
4571 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4572 * message gets duplicated before the corresponding Finished message,
4573 * the second ChangeCipherSpec should be discarded because it belongs
4574 * to an old epoch, but not because its length is shorter than
4575 * the minimum record length for packets using the new record transform.
4576 * Note that these two kinds of failures are handled differently,
4577 * as an unexpected record is silently skipped but an invalid
4578 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004579 */
4580#if defined(MBEDTLS_SSL_PROTO_DTLS)
4581 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4582 {
4583 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4584
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004585 /* Check epoch (and sequence number) with DTLS */
4586 if( rec_epoch != ssl->in_epoch )
4587 {
4588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4589 "expected %d, received %d",
4590 ssl->in_epoch, rec_epoch ) );
4591
4592#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4593 /*
4594 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4595 * access the first byte of record content (handshake type), as we
4596 * have an active transform (possibly iv_len != 0), so use the
4597 * fact that the record header len is 13 instead.
4598 */
4599 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4600 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4601 rec_epoch == 0 &&
4602 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4603 ssl->in_left > 13 &&
4604 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4605 {
4606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4607 "from the same port" ) );
4608 return( ssl_handle_possible_reconnect( ssl ) );
4609 }
4610 else
4611#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004612 {
4613 /* Consider buffering the record. */
4614 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4615 {
4616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4617 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4618 }
4619
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004620 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004621 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004622 }
4623
4624#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4625 /* Replay detection only works for the current epoch */
4626 if( rec_epoch == ssl->in_epoch &&
4627 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4628 {
4629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4630 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4631 }
4632#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004633
Hanno Becker52c6dc62017-05-26 16:07:36 +01004634 /* Drop unexpected ApplicationData records,
4635 * except at the beginning of renegotiations */
4636 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4637 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4638#if defined(MBEDTLS_SSL_RENEGOTIATION)
4639 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4640 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4641#endif
4642 )
4643 {
4644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4645 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4646 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004647 }
4648#endif /* MBEDTLS_SSL_PROTO_DTLS */
4649
Hanno Becker52c6dc62017-05-26 16:07:36 +01004650
4651 /* Check length against bounds of the current transform and version */
4652 if( ssl->transform_in == NULL )
4653 {
4654 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004655 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004656 {
4657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4658 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4659 }
4660 }
4661 else
4662 {
4663 if( ssl->in_msglen < ssl->transform_in->minlen )
4664 {
4665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4666 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4667 }
4668
4669#if defined(MBEDTLS_SSL_PROTO_SSL3)
4670 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004671 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004672 {
4673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4674 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4675 }
4676#endif
4677#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4678 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4679 /*
4680 * TLS encrypted messages can have up to 256 bytes of padding
4681 */
4682 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4683 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004684 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004685 {
4686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4687 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4688 }
4689#endif
4690 }
4691
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004692 return( 0 );
4693}
Paul Bakker5121ce52009-01-03 21:22:43 +00004694
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004695/*
4696 * If applicable, decrypt (and decompress) record content
4697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004699{
4700 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4703 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4706 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710 ret = mbedtls_ssl_hw_record_read( ssl );
4711 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4714 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004715 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004716
4717 if( ret == 0 )
4718 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004721 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004723 mbedtls_record rec;
4724
4725 rec.buf = ssl->in_iv;
4726 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4727 - ( ssl->in_iv - ssl->in_buf );
4728 rec.data_len = ssl->in_msglen;
4729 rec.data_offset = 0;
4730
4731 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4732 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4733 ssl->conf->transport, rec.ver );
4734 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004735 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4736 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004739 return( ret );
4740 }
4741
Hanno Becker29800d22018-08-07 14:30:18 +01004742 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4743 {
4744 /* Should never happen */
4745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4746 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004747
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004748 ssl->in_msglen = rec.data_len;
4749 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4750 ssl->in_len[1] = (unsigned char)( rec.data_len );
4751
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004752 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4753 ssl->in_msg, ssl->in_msglen );
4754
Angus Grattond8213d02016-05-25 20:56:48 +10004755 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4758 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004759 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004760 else if( ssl->in_msglen == 0 )
4761 {
4762#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4763 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4764 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4765 {
4766 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4768 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4769 }
4770#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4771
4772 ssl->nb_zero++;
4773
4774 /*
4775 * Three or more empty messages may be a DoS attack
4776 * (excessive CPU consumption).
4777 */
4778 if( ssl->nb_zero > 3 )
4779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4781 "messages, possible DoS attack" ) );
4782 /* Q: Is that the right error code? */
4783 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4784 }
4785 }
4786 else
4787 ssl->nb_zero = 0;
4788
4789#if defined(MBEDTLS_SSL_PROTO_DTLS)
4790 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4791 {
4792 ; /* in_ctr read from peer, not maintained internally */
4793 }
4794 else
4795#endif
4796 {
4797 unsigned i;
4798 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4799 if( ++ssl->in_ctr[i - 1] != 0 )
4800 break;
4801
4802 /* The loop goes to its end iff the counter is wrapping */
4803 if( i == ssl_ep_len( ssl ) )
4804 {
4805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4806 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4807 }
4808 }
4809
Paul Bakker5121ce52009-01-03 21:22:43 +00004810 }
4811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004813 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004815 {
4816 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004819 return( ret );
4820 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004825 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004828 }
4829#endif
4830
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004831 return( 0 );
4832}
4833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004835
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004836/*
4837 * Read a record.
4838 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004839 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4840 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4841 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004842 */
Hanno Becker1097b342018-08-15 14:09:41 +01004843
4844/* Helper functions for mbedtls_ssl_read_record(). */
4845static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004846static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4847static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004848
Hanno Becker327c93b2018-08-15 13:56:18 +01004849int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004850 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004851{
4852 int ret;
4853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004855
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004856 if( ssl->keep_current_message == 0 )
4857 {
4858 do {
Simon Butcher99000142016-10-13 17:21:01 +01004859
Hanno Becker26994592018-08-15 14:14:59 +01004860 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004861 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004862 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004863
Hanno Beckere74d5562018-08-15 14:26:08 +01004864 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004865 {
Hanno Becker40f50842018-08-15 14:48:01 +01004866#if defined(MBEDTLS_SSL_PROTO_DTLS)
4867 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004868
Hanno Becker40f50842018-08-15 14:48:01 +01004869 /* We only check for buffered messages if the
4870 * current datagram is fully consumed. */
4871 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004872 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004873 {
Hanno Becker40f50842018-08-15 14:48:01 +01004874 if( ssl_load_buffered_message( ssl ) == 0 )
4875 have_buffered = 1;
4876 }
4877
4878 if( have_buffered == 0 )
4879#endif /* MBEDTLS_SSL_PROTO_DTLS */
4880 {
4881 ret = ssl_get_next_record( ssl );
4882 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4883 continue;
4884
4885 if( ret != 0 )
4886 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004887 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004888 return( ret );
4889 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004890 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004891 }
4892
4893 ret = mbedtls_ssl_handle_message_type( ssl );
4894
Hanno Becker40f50842018-08-15 14:48:01 +01004895#if defined(MBEDTLS_SSL_PROTO_DTLS)
4896 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4897 {
4898 /* Buffer future message */
4899 ret = ssl_buffer_message( ssl );
4900 if( ret != 0 )
4901 return( ret );
4902
4903 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4904 }
4905#endif /* MBEDTLS_SSL_PROTO_DTLS */
4906
Hanno Becker90333da2017-10-10 11:27:13 +01004907 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4908 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004909
4910 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004911 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004912 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004913 return( ret );
4914 }
4915
Hanno Becker327c93b2018-08-15 13:56:18 +01004916 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004917 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004918 {
4919 mbedtls_ssl_update_handshake_status( ssl );
4920 }
Simon Butcher99000142016-10-13 17:21:01 +01004921 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004922 else
Simon Butcher99000142016-10-13 17:21:01 +01004923 {
Hanno Becker02f59072018-08-15 14:00:24 +01004924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004925 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004926 }
4927
4928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4929
4930 return( 0 );
4931}
4932
Hanno Becker40f50842018-08-15 14:48:01 +01004933#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004934static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004935{
Hanno Becker40f50842018-08-15 14:48:01 +01004936 if( ssl->in_left > ssl->next_record_offset )
4937 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004938
Hanno Becker40f50842018-08-15 14:48:01 +01004939 return( 0 );
4940}
4941
4942static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4943{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004944 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004945 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004946 int ret = 0;
4947
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004948 if( hs == NULL )
4949 return( -1 );
4950
Hanno Beckere00ae372018-08-20 09:39:42 +01004951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4952
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004953 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4954 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4955 {
4956 /* Check if we have seen a ChangeCipherSpec before.
4957 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004958 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004959 {
4960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4961 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004962 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004963 }
4964
Hanno Becker39b8bc92018-08-28 17:17:13 +01004965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004966 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4967 ssl->in_msglen = 1;
4968 ssl->in_msg[0] = 1;
4969
4970 /* As long as they are equal, the exact value doesn't matter. */
4971 ssl->in_left = 0;
4972 ssl->next_record_offset = 0;
4973
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004974 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004975 goto exit;
4976 }
Hanno Becker37f95322018-08-16 13:55:32 +01004977
Hanno Beckerb8f50142018-08-28 10:01:34 +01004978#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004979 /* Debug only */
4980 {
4981 unsigned offset;
4982 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4983 {
4984 hs_buf = &hs->buffering.hs[offset];
4985 if( hs_buf->is_valid == 1 )
4986 {
4987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4988 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004989 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004990 }
4991 }
4992 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004993#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004994
4995 /* Check if we have buffered and/or fully reassembled the
4996 * next handshake message. */
4997 hs_buf = &hs->buffering.hs[0];
4998 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4999 {
5000 /* Synthesize a record containing the buffered HS message. */
5001 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5002 ( hs_buf->data[2] << 8 ) |
5003 hs_buf->data[3];
5004
5005 /* Double-check that we haven't accidentally buffered
5006 * a message that doesn't fit into the input buffer. */
5007 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5008 {
5009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5011 }
5012
5013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5014 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5015 hs_buf->data, msg_len + 12 );
5016
5017 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5018 ssl->in_hslen = msg_len + 12;
5019 ssl->in_msglen = msg_len + 12;
5020 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5021
5022 ret = 0;
5023 goto exit;
5024 }
5025 else
5026 {
5027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5028 hs->in_msg_seq ) );
5029 }
5030
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005031 ret = -1;
5032
5033exit:
5034
5035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5036 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005037}
5038
Hanno Beckera02b0b42018-08-21 17:20:27 +01005039static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5040 size_t desired )
5041{
5042 int offset;
5043 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5045 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005046
Hanno Becker01315ea2018-08-21 17:22:17 +01005047 /* Get rid of future records epoch first, if such exist. */
5048 ssl_free_buffered_record( ssl );
5049
5050 /* Check if we have enough space available now. */
5051 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5052 hs->buffering.total_bytes_buffered ) )
5053 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005055 return( 0 );
5056 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005057
Hanno Becker4f432ad2018-08-28 10:02:32 +01005058 /* We don't have enough space to buffer the next expected handshake
5059 * message. Remove buffers used for future messages to gain space,
5060 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005061 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5062 offset >= 0; offset-- )
5063 {
5064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5065 offset ) );
5066
Hanno Beckerb309b922018-08-23 13:18:05 +01005067 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005068
5069 /* Check if we have enough space available now. */
5070 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5071 hs->buffering.total_bytes_buffered ) )
5072 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005074 return( 0 );
5075 }
5076 }
5077
5078 return( -1 );
5079}
5080
Hanno Becker40f50842018-08-15 14:48:01 +01005081static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5082{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005083 int ret = 0;
5084 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5085
5086 if( hs == NULL )
5087 return( 0 );
5088
5089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5090
5091 switch( ssl->in_msgtype )
5092 {
5093 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005095
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005096 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005097 break;
5098
5099 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005100 {
5101 unsigned recv_msg_seq_offset;
5102 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5103 mbedtls_ssl_hs_buffer *hs_buf;
5104 size_t msg_len = ssl->in_hslen - 12;
5105
5106 /* We should never receive an old handshake
5107 * message - double-check nonetheless. */
5108 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5109 {
5110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5112 }
5113
5114 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5115 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5116 {
5117 /* Silently ignore -- message too far in the future */
5118 MBEDTLS_SSL_DEBUG_MSG( 2,
5119 ( "Ignore future HS message with sequence number %u, "
5120 "buffering window %u - %u",
5121 recv_msg_seq, ssl->handshake->in_msg_seq,
5122 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5123
5124 goto exit;
5125 }
5126
5127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5128 recv_msg_seq, recv_msg_seq_offset ) );
5129
5130 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5131
5132 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005133 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005134 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005135 size_t reassembly_buf_sz;
5136
Hanno Becker37f95322018-08-16 13:55:32 +01005137 hs_buf->is_fragmented =
5138 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5139
5140 /* We copy the message back into the input buffer
5141 * after reassembly, so check that it's not too large.
5142 * This is an implementation-specific limitation
5143 * and not one from the standard, hence it is not
5144 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005145 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005146 {
5147 /* Ignore message */
5148 goto exit;
5149 }
5150
Hanno Beckere0b150f2018-08-21 15:51:03 +01005151 /* Check if we have enough space to buffer the message. */
5152 if( hs->buffering.total_bytes_buffered >
5153 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5154 {
5155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5157 }
5158
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005159 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5160 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005161
5162 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5163 hs->buffering.total_bytes_buffered ) )
5164 {
5165 if( recv_msg_seq_offset > 0 )
5166 {
5167 /* If we can't buffer a future message because
5168 * of space limitations -- ignore. */
5169 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",
5170 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5171 (unsigned) hs->buffering.total_bytes_buffered ) );
5172 goto exit;
5173 }
Hanno Beckere1801392018-08-21 16:51:05 +01005174 else
5175 {
5176 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",
5177 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5178 (unsigned) hs->buffering.total_bytes_buffered ) );
5179 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005180
Hanno Beckera02b0b42018-08-21 17:20:27 +01005181 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005182 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005183 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",
5184 (unsigned) msg_len,
5185 (unsigned) reassembly_buf_sz,
5186 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005187 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005188 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5189 goto exit;
5190 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005191 }
5192
5193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5194 msg_len ) );
5195
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005196 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5197 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005198 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005199 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005200 goto exit;
5201 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005202 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005203
5204 /* Prepare final header: copy msg_type, length and message_seq,
5205 * then add standardised fragment_offset and fragment_length */
5206 memcpy( hs_buf->data, ssl->in_msg, 6 );
5207 memset( hs_buf->data + 6, 0, 3 );
5208 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5209
5210 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005211
5212 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005213 }
5214 else
5215 {
5216 /* Make sure msg_type and length are consistent */
5217 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5218 {
5219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5220 /* Ignore */
5221 goto exit;
5222 }
5223 }
5224
Hanno Becker4422bbb2018-08-20 09:40:19 +01005225 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005226 {
5227 size_t frag_len, frag_off;
5228 unsigned char * const msg = hs_buf->data + 12;
5229
5230 /*
5231 * Check and copy current fragment
5232 */
5233
5234 /* Validation of header fields already done in
5235 * mbedtls_ssl_prepare_handshake_record(). */
5236 frag_off = ssl_get_hs_frag_off( ssl );
5237 frag_len = ssl_get_hs_frag_len( ssl );
5238
5239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5240 frag_off, frag_len ) );
5241 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5242
5243 if( hs_buf->is_fragmented )
5244 {
5245 unsigned char * const bitmask = msg + msg_len;
5246 ssl_bitmask_set( bitmask, frag_off, frag_len );
5247 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5248 msg_len ) == 0 );
5249 }
5250 else
5251 {
5252 hs_buf->is_complete = 1;
5253 }
5254
5255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5256 hs_buf->is_complete ? "" : "not yet " ) );
5257 }
5258
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005259 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005260 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005261
5262 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005263 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005264 break;
5265 }
5266
5267exit:
5268
5269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5270 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005271}
5272#endif /* MBEDTLS_SSL_PROTO_DTLS */
5273
Hanno Becker1097b342018-08-15 14:09:41 +01005274static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005275{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005276 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005277 * Consume last content-layer message and potentially
5278 * update in_msglen which keeps track of the contents'
5279 * consumption state.
5280 *
5281 * (1) Handshake messages:
5282 * Remove last handshake message, move content
5283 * and adapt in_msglen.
5284 *
5285 * (2) Alert messages:
5286 * Consume whole record content, in_msglen = 0.
5287 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005288 * (3) Change cipher spec:
5289 * Consume whole record content, in_msglen = 0.
5290 *
5291 * (4) Application data:
5292 * Don't do anything - the record layer provides
5293 * the application data as a stream transport
5294 * and consumes through mbedtls_ssl_read only.
5295 *
5296 */
5297
5298 /* Case (1): Handshake messages */
5299 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005300 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005301 /* Hard assertion to be sure that no application data
5302 * is in flight, as corrupting ssl->in_msglen during
5303 * ssl->in_offt != NULL is fatal. */
5304 if( ssl->in_offt != NULL )
5305 {
5306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5308 }
5309
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005310 /*
5311 * Get next Handshake message in the current record
5312 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005313
Hanno Becker4a810fb2017-05-24 16:27:30 +01005314 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005315 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005316 * current handshake content: If DTLS handshake
5317 * fragmentation is used, that's the fragment
5318 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005319 * size here is faulty and should be changed at
5320 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005321 * (2) While it doesn't seem to cause problems, one
5322 * has to be very careful not to assume that in_hslen
5323 * is always <= in_msglen in a sensible communication.
5324 * Again, it's wrong for DTLS handshake fragmentation.
5325 * The following check is therefore mandatory, and
5326 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005327 * Additionally, ssl->in_hslen might be arbitrarily out of
5328 * bounds after handling a DTLS message with an unexpected
5329 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005330 */
5331 if( ssl->in_hslen < ssl->in_msglen )
5332 {
5333 ssl->in_msglen -= ssl->in_hslen;
5334 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5335 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005336
Hanno Becker4a810fb2017-05-24 16:27:30 +01005337 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5338 ssl->in_msg, ssl->in_msglen );
5339 }
5340 else
5341 {
5342 ssl->in_msglen = 0;
5343 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005344
Hanno Becker4a810fb2017-05-24 16:27:30 +01005345 ssl->in_hslen = 0;
5346 }
5347 /* Case (4): Application data */
5348 else if( ssl->in_offt != NULL )
5349 {
5350 return( 0 );
5351 }
5352 /* Everything else (CCS & Alerts) */
5353 else
5354 {
5355 ssl->in_msglen = 0;
5356 }
5357
Hanno Becker1097b342018-08-15 14:09:41 +01005358 return( 0 );
5359}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005360
Hanno Beckere74d5562018-08-15 14:26:08 +01005361static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5362{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005363 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005364 return( 1 );
5365
5366 return( 0 );
5367}
5368
Hanno Becker5f066e72018-08-16 14:56:31 +01005369#if defined(MBEDTLS_SSL_PROTO_DTLS)
5370
5371static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5372{
5373 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5374 if( hs == NULL )
5375 return;
5376
Hanno Becker01315ea2018-08-21 17:22:17 +01005377 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005378 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005379 hs->buffering.total_bytes_buffered -=
5380 hs->buffering.future_record.len;
5381
5382 mbedtls_free( hs->buffering.future_record.data );
5383 hs->buffering.future_record.data = NULL;
5384 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005385}
5386
5387static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5388{
5389 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5390 unsigned char * rec;
5391 size_t rec_len;
5392 unsigned rec_epoch;
5393
5394 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5395 return( 0 );
5396
5397 if( hs == NULL )
5398 return( 0 );
5399
Hanno Becker5f066e72018-08-16 14:56:31 +01005400 rec = hs->buffering.future_record.data;
5401 rec_len = hs->buffering.future_record.len;
5402 rec_epoch = hs->buffering.future_record.epoch;
5403
5404 if( rec == NULL )
5405 return( 0 );
5406
Hanno Becker4cb782d2018-08-20 11:19:05 +01005407 /* Only consider loading future records if the
5408 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005409 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005410 return( 0 );
5411
Hanno Becker5f066e72018-08-16 14:56:31 +01005412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5413
5414 if( rec_epoch != ssl->in_epoch )
5415 {
5416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5417 goto exit;
5418 }
5419
5420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5421
5422 /* Double-check that the record is not too large */
5423 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5424 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5425 {
5426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5428 }
5429
5430 memcpy( ssl->in_hdr, rec, rec_len );
5431 ssl->in_left = rec_len;
5432 ssl->next_record_offset = 0;
5433
5434 ssl_free_buffered_record( ssl );
5435
5436exit:
5437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5438 return( 0 );
5439}
5440
5441static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5442{
5443 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5444 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005445 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005446
5447 /* Don't buffer future records outside handshakes. */
5448 if( hs == NULL )
5449 return( 0 );
5450
5451 /* Only buffer handshake records (we are only interested
5452 * in Finished messages). */
5453 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5454 return( 0 );
5455
5456 /* Don't buffer more than one future epoch record. */
5457 if( hs->buffering.future_record.data != NULL )
5458 return( 0 );
5459
Hanno Becker01315ea2018-08-21 17:22:17 +01005460 /* Don't buffer record if there's not enough buffering space remaining. */
5461 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5462 hs->buffering.total_bytes_buffered ) )
5463 {
5464 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",
5465 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5466 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005467 return( 0 );
5468 }
5469
Hanno Becker5f066e72018-08-16 14:56:31 +01005470 /* Buffer record */
5471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5472 ssl->in_epoch + 1 ) );
5473 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5474 rec_hdr_len + ssl->in_msglen );
5475
5476 /* ssl_parse_record_header() only considers records
5477 * of the next epoch as candidates for buffering. */
5478 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005479 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005480
5481 hs->buffering.future_record.data =
5482 mbedtls_calloc( 1, hs->buffering.future_record.len );
5483 if( hs->buffering.future_record.data == NULL )
5484 {
5485 /* If we run out of RAM trying to buffer a
5486 * record from the next epoch, just ignore. */
5487 return( 0 );
5488 }
5489
Hanno Becker01315ea2018-08-21 17:22:17 +01005490 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005491
Hanno Becker01315ea2018-08-21 17:22:17 +01005492 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005493 return( 0 );
5494}
5495
5496#endif /* MBEDTLS_SSL_PROTO_DTLS */
5497
Hanno Beckere74d5562018-08-15 14:26:08 +01005498static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005499{
5500 int ret;
5501
Hanno Becker5f066e72018-08-16 14:56:31 +01005502#if defined(MBEDTLS_SSL_PROTO_DTLS)
5503 /* We might have buffered a future record; if so,
5504 * and if the epoch matches now, load it.
5505 * On success, this call will set ssl->in_left to
5506 * the length of the buffered record, so that
5507 * the calls to ssl_fetch_input() below will
5508 * essentially be no-ops. */
5509 ret = ssl_load_buffered_record( ssl );
5510 if( ret != 0 )
5511 return( ret );
5512#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005517 return( ret );
5518 }
5519
5520 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005523 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5524 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005525 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005526 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5527 {
5528 ret = ssl_buffer_future_record( ssl );
5529 if( ret != 0 )
5530 return( ret );
5531
5532 /* Fall through to handling of unexpected records */
5533 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5534 }
5535
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005536 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5537 {
5538 /* Skip unexpected record (but not whole datagram) */
5539 ssl->next_record_offset = ssl->in_msglen
5540 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005541
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5543 "(header)" ) );
5544 }
5545 else
5546 {
5547 /* Skip invalid record and the rest of the datagram */
5548 ssl->next_record_offset = 0;
5549 ssl->in_left = 0;
5550
5551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5552 "(header)" ) );
5553 }
5554
5555 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005556 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005557 }
5558#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005559 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005560 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005561
5562 /*
5563 * Read and optionally decrypt the message contents
5564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5566 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005569 return( ret );
5570 }
5571
5572 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005577 if( ssl->next_record_offset < ssl->in_left )
5578 {
5579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5580 }
5581 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005582 else
5583#endif
5584 ssl->in_left = 0;
5585
5586 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005589 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005590 {
5591 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5593 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005594 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005595 /* Except when waiting for Finished as a bad mac here
5596 * probably means something went wrong in the handshake
5597 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5598 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5599 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5600 {
5601#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5602 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5603 {
5604 mbedtls_ssl_send_alert_message( ssl,
5605 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5606 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5607 }
5608#endif
5609 return( ret );
5610 }
5611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005613 if( ssl->conf->badmac_limit != 0 &&
5614 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5617 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005618 }
5619#endif
5620
Hanno Becker4a810fb2017-05-24 16:27:30 +01005621 /* As above, invalid records cause
5622 * dismissal of the whole datagram. */
5623
5624 ssl->next_record_offset = 0;
5625 ssl->in_left = 0;
5626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005628 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005629 }
5630
5631 return( ret );
5632 }
5633 else
5634#endif
5635 {
5636 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5638 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 mbedtls_ssl_send_alert_message( ssl,
5641 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5642 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005643 }
5644#endif
5645 return( ret );
5646 }
5647 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005648
Simon Butcher99000142016-10-13 17:21:01 +01005649 return( 0 );
5650}
5651
5652int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5653{
5654 int ret;
5655
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005656 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005657 * Handle particular types of records
5658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005659 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005660 {
Simon Butcher99000142016-10-13 17:21:01 +01005661 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5662 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005663 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005665 }
5666
Hanno Beckere678eaa2018-08-21 14:57:46 +01005667 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005668 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005669 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005670 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5672 ssl->in_msglen ) );
5673 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005674 }
5675
Hanno Beckere678eaa2018-08-21 14:57:46 +01005676 if( ssl->in_msg[0] != 1 )
5677 {
5678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5679 ssl->in_msg[0] ) );
5680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5681 }
5682
5683#if defined(MBEDTLS_SSL_PROTO_DTLS)
5684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5685 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5686 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5687 {
5688 if( ssl->handshake == NULL )
5689 {
5690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5691 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5692 }
5693
5694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5695 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5696 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005697#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005698 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005702 if( ssl->in_msglen != 2 )
5703 {
5704 /* Note: Standard allows for more than one 2 byte alert
5705 to be packed in a single message, but Mbed TLS doesn't
5706 currently support this. */
5707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5708 ssl->in_msglen ) );
5709 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5710 }
5711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 ssl->in_msg[0], ssl->in_msg[1] ) );
5714
5715 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005716 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005721 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723 }
5724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5726 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5729 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005730 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005731
5732#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5733 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5734 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5735 {
Hanno Becker90333da2017-10-10 11:27:13 +01005736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005737 /* Will be handled when trying to parse ServerHello */
5738 return( 0 );
5739 }
5740#endif
5741
5742#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5743 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5744 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5745 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5746 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5747 {
5748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5749 /* Will be handled in mbedtls_ssl_parse_certificate() */
5750 return( 0 );
5751 }
5752#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5753
5754 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005755 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005756 }
5757
Hanno Beckerc76c6192017-06-06 10:03:17 +01005758#if defined(MBEDTLS_SSL_PROTO_DTLS)
5759 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5760 ssl->handshake != NULL &&
5761 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5762 {
5763 ssl_handshake_wrapup_free_hs_transform( ssl );
5764 }
5765#endif
5766
Paul Bakker5121ce52009-01-03 21:22:43 +00005767 return( 0 );
5768}
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005771{
5772 int ret;
5773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5775 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5776 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005777 {
5778 return( ret );
5779 }
5780
5781 return( 0 );
5782}
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005785 unsigned char level,
5786 unsigned char message )
5787{
5788 int ret;
5789
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005790 if( ssl == NULL || ssl->conf == NULL )
5791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005797 ssl->out_msglen = 2;
5798 ssl->out_msg[0] = level;
5799 ssl->out_msg[1] = message;
5800
Hanno Becker67bc7c32018-08-06 11:33:50 +01005801 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005804 return( ret );
5805 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005807
5808 return( 0 );
5809}
5810
Hanno Beckerb9d44792019-02-08 07:19:04 +00005811#if defined(MBEDTLS_X509_CRT_PARSE_C)
5812static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5813{
5814#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5815 if( session->peer_cert != NULL )
5816 {
5817 mbedtls_x509_crt_free( session->peer_cert );
5818 mbedtls_free( session->peer_cert );
5819 session->peer_cert = NULL;
5820 }
5821#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5822 if( session->peer_cert_digest != NULL )
5823 {
5824 /* Zeroization is not necessary. */
5825 mbedtls_free( session->peer_cert_digest );
5826 session->peer_cert_digest = NULL;
5827 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5828 session->peer_cert_digest_len = 0;
5829 }
5830#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5831}
5832#endif /* MBEDTLS_X509_CRT_PARSE_C */
5833
Paul Bakker5121ce52009-01-03 21:22:43 +00005834/*
5835 * Handshake functions
5836 */
Hanno Becker21489932019-02-05 13:20:55 +00005837#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005838/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005839int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005840{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005841 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5842 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005845
Hanno Becker7177a882019-02-05 13:36:46 +00005846 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005849 ssl->state++;
5850 return( 0 );
5851 }
5852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005855}
5856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005858{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005859 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5860 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005863
Hanno Becker7177a882019-02-05 13:36:46 +00005864 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005867 ssl->state++;
5868 return( 0 );
5869 }
5870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005873}
Gilles Peskinef9828522017-05-03 12:28:43 +02005874
Hanno Becker21489932019-02-05 13:20:55 +00005875#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005876/* Some certificate support -> implement write and parse */
5877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005879{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005881 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005883 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5884 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005887
Hanno Becker7177a882019-02-05 13:36:46 +00005888 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005891 ssl->state++;
5892 return( 0 );
5893 }
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005896 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 {
5898 if( ssl->client_auth == 0 )
5899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005901 ssl->state++;
5902 return( 0 );
5903 }
5904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005906 /*
5907 * If using SSLv3 and got no cert, send an Alert message
5908 * (otherwise an empty Certificate message will be sent).
5909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5911 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005912 {
5913 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5915 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5916 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005919 goto write_msg;
5920 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923#endif /* MBEDTLS_SSL_CLI_C */
5924#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005925 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5930 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005931 }
5932 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005933#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005936
5937 /*
5938 * 0 . 0 handshake type
5939 * 1 . 3 handshake length
5940 * 4 . 6 length of all certs
5941 * 7 . 9 length of cert. 1
5942 * 10 . n-1 peer certificate
5943 * n . n+2 length of cert. 2
5944 * n+3 . ... upper level cert, etc.
5945 */
5946 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005948
Paul Bakker29087132010-03-21 21:03:34 +00005949 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 {
5951 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005952 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005955 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005957 }
5958
5959 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5960 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5961 ssl->out_msg[i + 2] = (unsigned char)( n );
5962
5963 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5964 i += n; crt = crt->next;
5965 }
5966
5967 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5968 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5969 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5970
5971 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5973 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005974
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005975#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005976write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005977#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005978
5979 ssl->state++;
5980
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005981 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005982 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005984 return( ret );
5985 }
5986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005988
Paul Bakkered27a042013-04-18 22:46:23 +02005989 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005990}
5991
Hanno Becker84879e32019-01-31 07:44:03 +00005992#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005993
5994#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005995static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5996 unsigned char *crt_buf,
5997 size_t crt_buf_len )
5998{
5999 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6000
6001 if( peer_crt == NULL )
6002 return( -1 );
6003
6004 if( peer_crt->raw.len != crt_buf_len )
6005 return( -1 );
6006
Hanno Becker46f34d02019-02-08 14:00:04 +00006007 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006008}
Hanno Becker177475a2019-02-05 17:02:46 +00006009#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6010static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6011 unsigned char *crt_buf,
6012 size_t crt_buf_len )
6013{
6014 int ret;
6015 unsigned char const * const peer_cert_digest =
6016 ssl->session->peer_cert_digest;
6017 mbedtls_md_type_t const peer_cert_digest_type =
6018 ssl->session->peer_cert_digest_type;
6019 mbedtls_md_info_t const * const digest_info =
6020 mbedtls_md_info_from_type( peer_cert_digest_type );
6021 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6022 size_t digest_len;
6023
6024 if( peer_cert_digest == NULL || digest_info == NULL )
6025 return( -1 );
6026
6027 digest_len = mbedtls_md_get_size( digest_info );
6028 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6029 return( -1 );
6030
6031 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6032 if( ret != 0 )
6033 return( -1 );
6034
6035 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6036}
6037#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006038#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006039
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006040/*
6041 * Once the certificate message is read, parse it into a cert chain and
6042 * perform basic checks, but leave actual verification to the caller
6043 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006044static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6045 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006047 int ret;
6048#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6049 int crt_cnt=0;
6050#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006051 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006052 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006057 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6058 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 }
6061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6063 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6067 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 }
6070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006072
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006076 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006077
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006078 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006082 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6083 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 }
6086
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006087 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6088 i += 3;
6089
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006090 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006091 while( i < ssl->in_hslen )
6092 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006093 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006094 if ( i + 3 > ssl->in_hslen ) {
6095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006096 mbedtls_ssl_send_alert_message( ssl,
6097 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6098 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006099 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6100 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006101 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6102 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006103 if( ssl->in_msg[i] != 0 )
6104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006106 mbedtls_ssl_send_alert_message( ssl,
6107 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6108 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006110 }
6111
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006112 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006113 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6114 | (unsigned int) ssl->in_msg[i + 2];
6115 i += 3;
6116
6117 if( n < 128 || i + n > ssl->in_hslen )
6118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006120 mbedtls_ssl_send_alert_message( ssl,
6121 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6122 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 }
6125
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006126 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006127#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6128 if( crt_cnt++ == 0 &&
6129 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6130 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006131 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006132 /* During client-side renegotiation, check that the server's
6133 * end-CRTs hasn't changed compared to the initial handshake,
6134 * mitigating the triple handshake attack. On success, reuse
6135 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6137 if( ssl_check_peer_crt_unchanged( ssl,
6138 &ssl->in_msg[i],
6139 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006140 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6142 mbedtls_ssl_send_alert_message( ssl,
6143 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6144 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6145 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006146 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006147
6148 /* Now we can safely free the original chain. */
6149 ssl_clear_peer_cert( ssl->session );
6150 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006151#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6152
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006153 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006154#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006155 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006156#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006157 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006158 * it in-place from the input buffer instead of making a copy. */
6159 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6160#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006161 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006163 case 0: /*ok*/
6164 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6165 /* Ignore certificate with an unknown algorithm: maybe a
6166 prior certificate was already trusted. */
6167 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006168
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006169 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6170 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6171 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006172
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006173 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6174 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6175 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006176
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006177 default:
6178 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6179 crt_parse_der_failed:
6180 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6181 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6182 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006183 }
6184
6185 i += n;
6186 }
6187
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006188 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006189 return( 0 );
6190}
6191
Hanno Becker4a55f632019-02-05 12:49:06 +00006192#if defined(MBEDTLS_SSL_SRV_C)
6193static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6194{
6195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6196 return( -1 );
6197
6198#if defined(MBEDTLS_SSL_PROTO_SSL3)
6199 /*
6200 * Check if the client sent an empty certificate
6201 */
6202 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6203 {
6204 if( ssl->in_msglen == 2 &&
6205 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6206 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6207 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6208 {
6209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6210 return( 0 );
6211 }
6212
6213 return( -1 );
6214 }
6215#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6216
6217#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6218 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6219 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6220 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6221 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6222 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6223 {
6224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6225 return( 0 );
6226 }
6227
6228 return( -1 );
6229#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6230 MBEDTLS_SSL_PROTO_TLS1_2 */
6231}
6232#endif /* MBEDTLS_SSL_SRV_C */
6233
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006234/* Check if a certificate message is expected.
6235 * Return either
6236 * - SSL_CERTIFICATE_EXPECTED, or
6237 * - SSL_CERTIFICATE_SKIP
6238 * indicating whether a Certificate message is expected or not.
6239 */
6240#define SSL_CERTIFICATE_EXPECTED 0
6241#define SSL_CERTIFICATE_SKIP 1
6242static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6243 int authmode )
6244{
6245 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006246 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006247
6248 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6249 return( SSL_CERTIFICATE_SKIP );
6250
6251#if defined(MBEDTLS_SSL_SRV_C)
6252 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6253 {
6254 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6255 return( SSL_CERTIFICATE_SKIP );
6256
6257 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6258 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006259 ssl->session_negotiate->verify_result =
6260 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6261 return( SSL_CERTIFICATE_SKIP );
6262 }
6263 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006264#else
6265 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006266#endif /* MBEDTLS_SSL_SRV_C */
6267
6268 return( SSL_CERTIFICATE_EXPECTED );
6269}
6270
Hanno Becker68636192019-02-05 14:36:34 +00006271static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6272 int authmode,
6273 mbedtls_x509_crt *chain,
6274 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006275{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006276 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006277 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006278 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006279 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006280
Hanno Becker8927c832019-04-03 12:52:50 +01006281 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6282 void *p_vrfy;
6283
Hanno Becker68636192019-02-05 14:36:34 +00006284 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6285 return( 0 );
6286
Hanno Becker8927c832019-04-03 12:52:50 +01006287 if( ssl->f_vrfy != NULL )
6288 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006290 f_vrfy = ssl->f_vrfy;
6291 p_vrfy = ssl->p_vrfy;
6292 }
6293 else
6294 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006296 f_vrfy = ssl->conf->f_vrfy;
6297 p_vrfy = ssl->conf->p_vrfy;
6298 }
6299
Hanno Becker68636192019-02-05 14:36:34 +00006300 /*
6301 * Main check: verify certificate
6302 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006303#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6304 if( ssl->conf->f_ca_cb != NULL )
6305 {
6306 ((void) rs_ctx);
6307 have_ca_chain = 1;
6308
6309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006310 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006311 chain,
6312 ssl->conf->f_ca_cb,
6313 ssl->conf->p_ca_cb,
6314 ssl->conf->cert_profile,
6315 ssl->hostname,
6316 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006317 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006318 }
6319 else
6320#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6321 {
6322 mbedtls_x509_crt *ca_chain;
6323 mbedtls_x509_crl *ca_crl;
6324
6325#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6326 if( ssl->handshake->sni_ca_chain != NULL )
6327 {
6328 ca_chain = ssl->handshake->sni_ca_chain;
6329 ca_crl = ssl->handshake->sni_ca_crl;
6330 }
6331 else
6332#endif
6333 {
6334 ca_chain = ssl->conf->ca_chain;
6335 ca_crl = ssl->conf->ca_crl;
6336 }
6337
6338 if( ca_chain != NULL )
6339 have_ca_chain = 1;
6340
6341 ret = mbedtls_x509_crt_verify_restartable(
6342 chain,
6343 ca_chain, ca_crl,
6344 ssl->conf->cert_profile,
6345 ssl->hostname,
6346 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006347 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006348 }
Hanno Becker68636192019-02-05 14:36:34 +00006349
6350 if( ret != 0 )
6351 {
6352 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6353 }
6354
6355#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6356 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6357 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6358#endif
6359
6360 /*
6361 * Secondary checks: always done, but change 'ret' only if it was 0
6362 */
6363
6364#if defined(MBEDTLS_ECP_C)
6365 {
6366 const mbedtls_pk_context *pk = &chain->pk;
6367
6368 /* If certificate uses an EC key, make sure the curve is OK */
6369 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6370 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6371 {
6372 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6373
6374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6375 if( ret == 0 )
6376 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6377 }
6378 }
6379#endif /* MBEDTLS_ECP_C */
6380
6381 if( mbedtls_ssl_check_cert_usage( chain,
6382 ciphersuite_info,
6383 ! ssl->conf->endpoint,
6384 &ssl->session_negotiate->verify_result ) != 0 )
6385 {
6386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6387 if( ret == 0 )
6388 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6389 }
6390
6391 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6392 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6393 * with details encoded in the verification flags. All other kinds
6394 * of error codes, including those from the user provided f_vrfy
6395 * functions, are treated as fatal and lead to a failure of
6396 * ssl_parse_certificate even if verification was optional. */
6397 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6398 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6399 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6400 {
6401 ret = 0;
6402 }
6403
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006404 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006405 {
6406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6407 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6408 }
6409
6410 if( ret != 0 )
6411 {
6412 uint8_t alert;
6413
6414 /* The certificate may have been rejected for several reasons.
6415 Pick one and send the corresponding alert. Which alert to send
6416 may be a subject of debate in some cases. */
6417 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6418 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6419 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6420 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6421 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6422 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6423 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6424 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6425 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6426 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6427 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6428 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6429 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6430 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6431 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6432 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6433 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6434 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6435 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6436 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6437 else
6438 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6439 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6440 alert );
6441 }
6442
6443#if defined(MBEDTLS_DEBUG_C)
6444 if( ssl->session_negotiate->verify_result != 0 )
6445 {
6446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6447 ssl->session_negotiate->verify_result ) );
6448 }
6449 else
6450 {
6451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6452 }
6453#endif /* MBEDTLS_DEBUG_C */
6454
6455 return( ret );
6456}
6457
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006458#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6459static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6460 unsigned char *start, size_t len )
6461{
6462 int ret;
6463 /* Remember digest of the peer's end-CRT. */
6464 ssl->session_negotiate->peer_cert_digest =
6465 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6466 if( ssl->session_negotiate->peer_cert_digest == NULL )
6467 {
6468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6469 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6470 mbedtls_ssl_send_alert_message( ssl,
6471 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6472 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6473
6474 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6475 }
6476
6477 ret = mbedtls_md( mbedtls_md_info_from_type(
6478 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6479 start, len,
6480 ssl->session_negotiate->peer_cert_digest );
6481
6482 ssl->session_negotiate->peer_cert_digest_type =
6483 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6484 ssl->session_negotiate->peer_cert_digest_len =
6485 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6486
6487 return( ret );
6488}
6489
6490static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6491 unsigned char *start, size_t len )
6492{
6493 unsigned char *end = start + len;
6494 int ret;
6495
6496 /* Make a copy of the peer's raw public key. */
6497 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6498 ret = mbedtls_pk_parse_subpubkey( &start, end,
6499 &ssl->handshake->peer_pubkey );
6500 if( ret != 0 )
6501 {
6502 /* We should have parsed the public key before. */
6503 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6504 }
6505
6506 return( 0 );
6507}
6508#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6509
Hanno Becker68636192019-02-05 14:36:34 +00006510int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6511{
6512 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006513 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006514#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6515 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6516 ? ssl->handshake->sni_authmode
6517 : ssl->conf->authmode;
6518#else
6519 const int authmode = ssl->conf->authmode;
6520#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006521 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006522 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006523
6524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6525
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006526 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6527 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006528 {
6529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006530 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006531 }
6532
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006533#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6534 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006535 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006536 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006537 chain = ssl->handshake->ecrs_peer_cert;
6538 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006539 goto crt_verify;
6540 }
6541#endif
6542
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006543 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006544 {
6545 /* mbedtls_ssl_read_record may have sent an alert already. We
6546 let it decide whether to alert. */
6547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006548 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006549 }
6550
Hanno Becker4a55f632019-02-05 12:49:06 +00006551#if defined(MBEDTLS_SSL_SRV_C)
6552 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6553 {
6554 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006555
6556 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006557 ret = 0;
6558 else
6559 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006560
Hanno Becker6bdfab22019-02-05 13:11:17 +00006561 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006562 }
6563#endif /* MBEDTLS_SSL_SRV_C */
6564
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006565 /* Clear existing peer CRT structure in case we tried to
6566 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006567 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006568
6569 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6570 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006571 {
6572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6573 sizeof( mbedtls_x509_crt ) ) );
6574 mbedtls_ssl_send_alert_message( ssl,
6575 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6576 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006577
Hanno Becker3dad3112019-02-05 17:19:52 +00006578 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6579 goto exit;
6580 }
6581 mbedtls_x509_crt_init( chain );
6582
6583 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006584 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006585 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006586
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006587#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6588 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006589 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006590
6591crt_verify:
6592 if( ssl->handshake->ecrs_enabled)
6593 rs_ctx = &ssl->handshake->ecrs_ctx;
6594#endif
6595
Hanno Becker68636192019-02-05 14:36:34 +00006596 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006597 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006598 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006599 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006600
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006601#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006602 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006603 unsigned char *crt_start, *pk_start;
6604 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006605
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006606 /* We parse the CRT chain without copying, so
6607 * these pointers point into the input buffer,
6608 * and are hence still valid after freeing the
6609 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006610
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006611 crt_start = chain->raw.p;
6612 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006613
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006614 pk_start = chain->pk_raw.p;
6615 pk_len = chain->pk_raw.len;
6616
6617 /* Free the CRT structures before computing
6618 * digest and copying the peer's public key. */
6619 mbedtls_x509_crt_free( chain );
6620 mbedtls_free( chain );
6621 chain = NULL;
6622
6623 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006624 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006625 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006626
6627 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6628 if( ret != 0 )
6629 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006630 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006631#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6632 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006633 ssl->session_negotiate->peer_cert = chain;
6634 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006635#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006638
Hanno Becker6bdfab22019-02-05 13:11:17 +00006639exit:
6640
Hanno Becker3dad3112019-02-05 17:19:52 +00006641 if( ret == 0 )
6642 ssl->state++;
6643
6644#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6645 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6646 {
6647 ssl->handshake->ecrs_peer_cert = chain;
6648 chain = NULL;
6649 }
6650#endif
6651
6652 if( chain != NULL )
6653 {
6654 mbedtls_x509_crt_free( chain );
6655 mbedtls_free( chain );
6656 }
6657
Paul Bakker5121ce52009-01-03 21:22:43 +00006658 return( ret );
6659}
Hanno Becker21489932019-02-05 13:20:55 +00006660#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006663{
6664 int ret;
6665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 ssl->out_msglen = 1;
6670 ssl->out_msg[0] = 1;
6671
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 ssl->state++;
6673
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006674 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006675 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 return( ret );
6678 }
6679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006681
6682 return( 0 );
6683}
6684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006686{
6687 int ret;
6688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Hanno Becker327c93b2018-08-15 13:56:18 +01006691 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006694 return( ret );
6695 }
6696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006700 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6701 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006703 }
6704
Hanno Beckere678eaa2018-08-21 14:57:46 +01006705 /* CCS records are only accepted if they have length 1 and content '1',
6706 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006707
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006708 /*
6709 * Switch to our negotiated transform and session parameters for inbound
6710 * data.
6711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006713 ssl->transform_in = ssl->transform_negotiate;
6714 ssl->session_in = ssl->session_negotiate;
6715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006720 ssl_dtls_replay_reset( ssl );
6721#endif
6722
6723 /* Increment epoch */
6724 if( ++ssl->in_epoch == 0 )
6725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006727 /* This is highly unlikely to happen for legitimate reasons, so
6728 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006730 }
6731 }
6732 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006734 memset( ssl->in_ctr, 0, 8 );
6735
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006736 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6739 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006744 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6745 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006747 }
6748 }
6749#endif
6750
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 ssl->state++;
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006754
6755 return( 0 );
6756}
6757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6759 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006760{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006761 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6764 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6765 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006766 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006767 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6770#if defined(MBEDTLS_SHA512_C)
6771 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006772 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6773 else
6774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775#if defined(MBEDTLS_SHA256_C)
6776 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006777 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006778 else
6779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006783 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006784 }
Paul Bakker380da532012-04-18 16:10:25 +00006785}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6790 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006791 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6792 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006793#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6795#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006796#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006797 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006798 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6799#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006800 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006801#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006804#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006805 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006806 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006807#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006808 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006809#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006810#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006812}
6813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006815 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6818 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006819 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6820 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006821#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6823#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006824#if defined(MBEDTLS_USE_PSA_CRYPTO)
6825 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6826#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006827 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006828#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006831#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006832 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006833#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006834 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006835#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006836#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006838}
6839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6841 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6842static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006843 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006844{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006845 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6846 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006847}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006848#endif
Paul Bakker380da532012-04-18 16:10:25 +00006849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6851#if defined(MBEDTLS_SHA256_C)
6852static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006853 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006854{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006855#if defined(MBEDTLS_USE_PSA_CRYPTO)
6856 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6857#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006858 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006859#endif
Paul Bakker380da532012-04-18 16:10:25 +00006860}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006861#endif
Paul Bakker380da532012-04-18 16:10:25 +00006862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_SHA512_C)
6864static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006865 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006866{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006867#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006868 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006869#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006870 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006871#endif
Paul Bakker380da532012-04-18 16:10:25 +00006872}
Paul Bakker769075d2012-11-24 11:26:46 +01006873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006877static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006879{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006880 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006881 mbedtls_md5_context md5;
6882 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006883
Paul Bakker5121ce52009-01-03 21:22:43 +00006884 unsigned char padbuf[48];
6885 unsigned char md5sum[16];
6886 unsigned char sha1sum[20];
6887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006889 if( !session )
6890 session = ssl->session;
6891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006893
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006894 mbedtls_md5_init( &md5 );
6895 mbedtls_sha1_init( &sha1 );
6896
6897 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6898 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006899
6900 /*
6901 * SSLv3:
6902 * hash =
6903 * MD5( master + pad2 +
6904 * MD5( handshake + sender + master + pad1 ) )
6905 * + SHA1( master + pad2 +
6906 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006907 */
6908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006910 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6911 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006912#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006915 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6916 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006917#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006920 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006921
Paul Bakker1ef83d62012-04-11 12:09:53 +00006922 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006923
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006924 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6925 mbedtls_md5_update_ret( &md5, session->master, 48 );
6926 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6927 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006928
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006929 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6930 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6931 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6932 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006933
Paul Bakker1ef83d62012-04-11 12:09:53 +00006934 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006935
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006936 mbedtls_md5_starts_ret( &md5 );
6937 mbedtls_md5_update_ret( &md5, session->master, 48 );
6938 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6939 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6940 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006941
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006942 mbedtls_sha1_starts_ret( &sha1 );
6943 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6944 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6945 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6946 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006950 mbedtls_md5_free( &md5 );
6951 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006952
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006953 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6954 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6955 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006962static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006964{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006965 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006966 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006967 mbedtls_md5_context md5;
6968 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006969 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006972 if( !session )
6973 session = ssl->session;
6974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006976
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006977 mbedtls_md5_init( &md5 );
6978 mbedtls_sha1_init( &sha1 );
6979
6980 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6981 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006982
Paul Bakker1ef83d62012-04-11 12:09:53 +00006983 /*
6984 * TLSv1:
6985 * hash = PRF( master, finished_label,
6986 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6987 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006990 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6991 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006992#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006995 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6996 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006997#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007000 ? "client finished"
7001 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007002
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007003 mbedtls_md5_finish_ret( &md5, padbuf );
7004 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007005
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007006 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007007 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007010
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007011 mbedtls_md5_free( &md5 );
7012 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007013
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007014 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007017}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7021#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007022static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007024{
7025 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007026 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007027 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007028#if defined(MBEDTLS_USE_PSA_CRYPTO)
7029 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007030 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007031 psa_status_t status;
7032#else
7033 mbedtls_sha256_context sha256;
7034#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007037 if( !session )
7038 session = ssl->session;
7039
Andrzej Kurekeb342242019-01-29 09:14:33 -05007040 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7041 ? "client finished"
7042 : "server finished";
7043
7044#if defined(MBEDTLS_USE_PSA_CRYPTO)
7045 sha256_psa = psa_hash_operation_init();
7046
7047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7048
7049 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7050 if( status != PSA_SUCCESS )
7051 {
7052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7053 return;
7054 }
7055
7056 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7057 if( status != PSA_SUCCESS )
7058 {
7059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7060 return;
7061 }
7062 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7063#else
7064
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007065 mbedtls_sha256_init( &sha256 );
7066
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007068
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007069 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007070
7071 /*
7072 * TLSv1.2:
7073 * hash = PRF( master, finished_label,
7074 * Hash( handshake ) )[0.11]
7075 */
7076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077#if !defined(MBEDTLS_SHA256_ALT)
7078 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007079 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007080#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007081
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007082 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007083 mbedtls_sha256_free( &sha256 );
7084#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007085
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007086 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007087 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007090
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007091 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007098static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007100{
7101 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007102 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007103 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007104#if defined(MBEDTLS_USE_PSA_CRYPTO)
7105 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007106 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007107 psa_status_t status;
7108#else
7109 mbedtls_sha512_context sha512;
7110#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007113 if( !session )
7114 session = ssl->session;
7115
Andrzej Kurekeb342242019-01-29 09:14:33 -05007116 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7117 ? "client finished"
7118 : "server finished";
7119
7120#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007121 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007122
7123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7124
Andrzej Kurek972fba52019-01-30 03:29:12 -05007125 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007126 if( status != PSA_SUCCESS )
7127 {
7128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7129 return;
7130 }
7131
Andrzej Kurek972fba52019-01-30 03:29:12 -05007132 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007133 if( status != PSA_SUCCESS )
7134 {
7135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7136 return;
7137 }
7138 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7139#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007140 mbedtls_sha512_init( &sha512 );
7141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007143
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007144 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007145
7146 /*
7147 * TLSv1.2:
7148 * hash = PRF( master, finished_label,
7149 * Hash( handshake ) )[0.11]
7150 */
7151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007153 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7154 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007155#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007156
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007157 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007158 mbedtls_sha512_free( &sha512 );
7159#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007160
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007161 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007162 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007165
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007166 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#endif /* MBEDTLS_SHA512_C */
7171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007176
7177 /*
7178 * Free our handshake params
7179 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007180 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007182 ssl->handshake = NULL;
7183
7184 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007185 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007186 */
7187 if( ssl->transform )
7188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 mbedtls_ssl_transform_free( ssl->transform );
7190 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007191 }
7192 ssl->transform = ssl->transform_negotiate;
7193 ssl->transform_negotiate = NULL;
7194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007196}
7197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007199{
7200 int resume = ssl->handshake->resume;
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SSL_RENEGOTIATION)
7205 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007208 ssl->renego_records_seen = 0;
7209 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007210#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007211
7212 /*
7213 * Free the previous session and switch in the current one
7214 */
Paul Bakker0a597072012-09-25 21:55:46 +00007215 if( ssl->session )
7216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007218 /* RFC 7366 3.1: keep the EtM state */
7219 ssl->session_negotiate->encrypt_then_mac =
7220 ssl->session->encrypt_then_mac;
7221#endif
7222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 mbedtls_ssl_session_free( ssl->session );
7224 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007225 }
7226 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007227 ssl->session_negotiate = NULL;
7228
Paul Bakker0a597072012-09-25 21:55:46 +00007229 /*
7230 * Add cache entry
7231 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007232 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007233 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007234 resume == 0 )
7235 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007236 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007238 }
Paul Bakker0a597072012-09-25 21:55:46 +00007239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007242 ssl->handshake->flight != NULL )
7243 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007244 /* Cancel handshake timer */
7245 ssl_set_timer( ssl, 0 );
7246
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007247 /* Keep last flight around in case we need to resend it:
7248 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007250 }
7251 else
7252#endif
7253 ssl_handshake_wrapup_free_hs_transform( ssl );
7254
Paul Bakker48916f92012-09-16 19:57:18 +00007255 ssl->state++;
7256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007258}
7259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007261{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007262 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007265
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007266 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007267
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007268 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007269
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007270 /*
7271 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7272 * may define some other value. Currently (early 2016), no defined
7273 * ciphersuite does this (and this is unlikely to change as activity has
7274 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007279 ssl->verify_data_len = hash_len;
7280 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007281#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007282
Paul Bakker5121ce52009-01-03 21:22:43 +00007283 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7285 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007286
7287 /*
7288 * In case of session resuming, invert the client and server
7289 * ChangeCipherSpec messages order.
7290 */
Paul Bakker0a597072012-09-25 21:55:46 +00007291 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007294 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007296#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007301 }
7302 else
7303 ssl->state++;
7304
Paul Bakker48916f92012-09-16 19:57:18 +00007305 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007306 * Switch to our negotiated transform and session parameters for outbound
7307 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007313 {
7314 unsigned char i;
7315
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007316 /* Remember current epoch settings for resending */
7317 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007318 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007319
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007320 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007321 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007322
7323 /* Increment epoch */
7324 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007325 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007326 break;
7327
7328 /* The loop goes to its end iff the counter is wrapping */
7329 if( i == 0 )
7330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7332 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007333 }
7334 }
7335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007337 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007338
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007339 ssl->transform_out = ssl->transform_negotiate;
7340 ssl->session_out = ssl->session_negotiate;
7341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7343 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7348 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007349 }
7350 }
7351#endif
7352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007356#endif
7357
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007358 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361 return( ret );
7362 }
7363
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007364#if defined(MBEDTLS_SSL_PROTO_DTLS)
7365 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7366 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7367 {
7368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7369 return( ret );
7370 }
7371#endif
7372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374
7375 return( 0 );
7376}
7377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007379#define SSL_MAX_HASH_LEN 36
7380#else
7381#define SSL_MAX_HASH_LEN 12
7382#endif
7383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007385{
Paul Bakker23986e52011-04-24 08:57:21 +00007386 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007387 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007388 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007391
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007392 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007393
Hanno Becker327c93b2018-08-15 13:56:18 +01007394 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007397 return( ret );
7398 }
7399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007403 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7404 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007406 }
7407
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007408 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409#if defined(MBEDTLS_SSL_PROTO_SSL3)
7410 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007411 hash_len = 36;
7412 else
7413#endif
7414 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7417 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007420 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7421 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007423 }
7424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007426 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007429 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7430 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007432 }
7433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007435 ssl->verify_data_len = hash_len;
7436 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007437#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007438
Paul Bakker0a597072012-09-25 21:55:46 +00007439 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007442 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007446 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007448#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007449 }
7450 else
7451 ssl->state++;
7452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007454 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007456#endif
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007459
7460 return( 0 );
7461}
7462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7468 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7469 mbedtls_md5_init( &handshake->fin_md5 );
7470 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007471 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7472 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7475#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007476#if defined(MBEDTLS_USE_PSA_CRYPTO)
7477 handshake->fin_sha256_psa = psa_hash_operation_init();
7478 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7479#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007481 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007482#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007485#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007486 handshake->fin_sha384_psa = psa_hash_operation_init();
7487 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007488#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007490 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007491#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007494
7495 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007496
7497#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7498 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7499 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7500#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_DHM_C)
7503 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007504#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505#if defined(MBEDTLS_ECDH_C)
7506 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007507#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007508#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007509 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007510#if defined(MBEDTLS_SSL_CLI_C)
7511 handshake->ecjpake_cache = NULL;
7512 handshake->ecjpake_cache_len = 0;
7513#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007514#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007515
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007516#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007517 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007518#endif
7519
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007520#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7521 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7522#endif
Hanno Becker75173122019-02-06 16:18:31 +00007523
7524#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7525 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7526 mbedtls_pk_init( &handshake->peer_pubkey );
7527#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007528}
7529
Hanno Beckera18d1322018-01-03 14:27:32 +00007530void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7535 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007536
Hanno Beckerd56ed242018-01-03 15:32:51 +00007537#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 mbedtls_md_init( &transform->md_ctx_enc );
7539 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007540#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007541}
7542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007546}
7547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007549{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007550 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007551 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007552 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007553 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007555 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007556 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007557
7558 /*
7559 * Either the pointers are now NULL or cleared properly and can be freed.
7560 * Now allocate missing structures.
7561 */
7562 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007563 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007564 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007565 }
Paul Bakker48916f92012-09-16 19:57:18 +00007566
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007567 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007568 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007569 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007570 }
Paul Bakker48916f92012-09-16 19:57:18 +00007571
Paul Bakker82788fb2014-10-20 13:59:19 +02007572 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007573 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007574 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007575 }
Paul Bakker48916f92012-09-16 19:57:18 +00007576
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007577 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007578 if( ssl->handshake == NULL ||
7579 ssl->transform_negotiate == NULL ||
7580 ssl->session_negotiate == NULL )
7581 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 mbedtls_free( ssl->handshake );
7585 mbedtls_free( ssl->transform_negotiate );
7586 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007587
7588 ssl->handshake = NULL;
7589 ssl->transform_negotiate = NULL;
7590 ssl->session_negotiate = NULL;
7591
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007592 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007593 }
7594
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007595 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007597 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007598 ssl_handshake_params_init( ssl->handshake );
7599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007601 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7602 {
7603 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007604
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007605 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7606 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7607 else
7608 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007609
7610 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007611 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007612#endif
7613
Paul Bakker48916f92012-09-16 19:57:18 +00007614 return( 0 );
7615}
7616
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007617#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007618/* Dummy cookie callbacks for defaults */
7619static int ssl_cookie_write_dummy( void *ctx,
7620 unsigned char **p, unsigned char *end,
7621 const unsigned char *cli_id, size_t cli_id_len )
7622{
7623 ((void) ctx);
7624 ((void) p);
7625 ((void) end);
7626 ((void) cli_id);
7627 ((void) cli_id_len);
7628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007630}
7631
7632static int ssl_cookie_check_dummy( void *ctx,
7633 const unsigned char *cookie, size_t cookie_len,
7634 const unsigned char *cli_id, size_t cli_id_len )
7635{
7636 ((void) ctx);
7637 ((void) cookie);
7638 ((void) cookie_len);
7639 ((void) cli_id);
7640 ((void) cli_id_len);
7641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007643}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007644#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007645
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007646/* Once ssl->out_hdr as the address of the beginning of the
7647 * next outgoing record is set, deduce the other pointers.
7648 *
7649 * Note: For TLS, we save the implicit record sequence number
7650 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7651 * and the caller has to make sure there's space for this.
7652 */
7653
7654static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7655 mbedtls_ssl_transform *transform )
7656{
7657#if defined(MBEDTLS_SSL_PROTO_DTLS)
7658 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7659 {
7660 ssl->out_ctr = ssl->out_hdr + 3;
7661 ssl->out_len = ssl->out_hdr + 11;
7662 ssl->out_iv = ssl->out_hdr + 13;
7663 }
7664 else
7665#endif
7666 {
7667 ssl->out_ctr = ssl->out_hdr - 8;
7668 ssl->out_len = ssl->out_hdr + 3;
7669 ssl->out_iv = ssl->out_hdr + 5;
7670 }
7671
7672 /* Adjust out_msg to make space for explicit IV, if used. */
7673 if( transform != NULL &&
7674 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7675 {
7676 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7677 }
7678 else
7679 ssl->out_msg = ssl->out_iv;
7680}
7681
7682/* Once ssl->in_hdr as the address of the beginning of the
7683 * next incoming record is set, deduce the other pointers.
7684 *
7685 * Note: For TLS, we save the implicit record sequence number
7686 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7687 * and the caller has to make sure there's space for this.
7688 */
7689
7690static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7691 mbedtls_ssl_transform *transform )
7692{
7693#if defined(MBEDTLS_SSL_PROTO_DTLS)
7694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7695 {
7696 ssl->in_ctr = ssl->in_hdr + 3;
7697 ssl->in_len = ssl->in_hdr + 11;
7698 ssl->in_iv = ssl->in_hdr + 13;
7699 }
7700 else
7701#endif
7702 {
7703 ssl->in_ctr = ssl->in_hdr - 8;
7704 ssl->in_len = ssl->in_hdr + 3;
7705 ssl->in_iv = ssl->in_hdr + 5;
7706 }
7707
7708 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7709 if( transform != NULL &&
7710 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7711 {
7712 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7713 }
7714 else
7715 ssl->in_msg = ssl->in_iv;
7716}
7717
Paul Bakker5121ce52009-01-03 21:22:43 +00007718/*
7719 * Initialize an SSL context
7720 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007721void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7722{
7723 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7724}
7725
7726/*
7727 * Setup an SSL context
7728 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007729
7730static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7731{
7732 /* Set the incoming and outgoing record pointers. */
7733#if defined(MBEDTLS_SSL_PROTO_DTLS)
7734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7735 {
7736 ssl->out_hdr = ssl->out_buf;
7737 ssl->in_hdr = ssl->in_buf;
7738 }
7739 else
7740#endif /* MBEDTLS_SSL_PROTO_DTLS */
7741 {
7742 ssl->out_hdr = ssl->out_buf + 8;
7743 ssl->in_hdr = ssl->in_buf + 8;
7744 }
7745
7746 /* Derive other internal pointers. */
7747 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7748 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7749}
7750
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007751int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007752 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007753{
Paul Bakker48916f92012-09-16 19:57:18 +00007754 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007755
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007756 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007757
7758 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007759 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007760 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007761
7762 /* Set to NULL in case of an error condition */
7763 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007764
Angus Grattond8213d02016-05-25 20:56:48 +10007765 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7766 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007767 {
Angus Grattond8213d02016-05-25 20:56:48 +10007768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007769 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007770 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007771 }
7772
7773 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7774 if( ssl->out_buf == NULL )
7775 {
7776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007777 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007778 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007779 }
7780
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007781 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007782
Paul Bakker48916f92012-09-16 19:57:18 +00007783 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007784 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007785
7786 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007787
7788error:
7789 mbedtls_free( ssl->in_buf );
7790 mbedtls_free( ssl->out_buf );
7791
7792 ssl->conf = NULL;
7793
7794 ssl->in_buf = NULL;
7795 ssl->out_buf = NULL;
7796
7797 ssl->in_hdr = NULL;
7798 ssl->in_ctr = NULL;
7799 ssl->in_len = NULL;
7800 ssl->in_iv = NULL;
7801 ssl->in_msg = NULL;
7802
7803 ssl->out_hdr = NULL;
7804 ssl->out_ctr = NULL;
7805 ssl->out_len = NULL;
7806 ssl->out_iv = NULL;
7807 ssl->out_msg = NULL;
7808
k-stachowiak9f7798e2018-07-31 16:52:32 +02007809 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007810}
7811
7812/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007813 * Reset an initialized and used SSL context for re-use while retaining
7814 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007815 *
7816 * If partial is non-zero, keep data in the input buffer and client ID.
7817 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007818 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007819static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007820{
Paul Bakker48916f92012-09-16 19:57:18 +00007821 int ret;
7822
Hanno Becker7e772132018-08-10 12:38:21 +01007823#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7824 !defined(MBEDTLS_SSL_SRV_C)
7825 ((void) partial);
7826#endif
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007829
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007830 /* Cancel any possibly running timer */
7831 ssl_set_timer( ssl, 0 );
7832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833#if defined(MBEDTLS_SSL_RENEGOTIATION)
7834 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007835 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007836
7837 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7839 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007842
Paul Bakker7eb013f2011-10-06 12:37:39 +00007843 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007844 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007845
7846 ssl->in_msgtype = 0;
7847 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007849 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007850 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007853 ssl_dtls_replay_reset( ssl );
7854#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007855
7856 ssl->in_hslen = 0;
7857 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007858
7859 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007860
7861 ssl->out_msgtype = 0;
7862 ssl->out_msglen = 0;
7863 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7865 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007866 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007867#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007868
Hanno Becker19859472018-08-06 09:40:20 +01007869 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7870
Paul Bakker48916f92012-09-16 19:57:18 +00007871 ssl->transform_in = NULL;
7872 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007873
Hanno Becker78640902018-08-13 16:35:15 +01007874 ssl->session_in = NULL;
7875 ssl->session_out = NULL;
7876
Angus Grattond8213d02016-05-25 20:56:48 +10007877 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007878
7879#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007880 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007881#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7882 {
7883 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007884 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007885 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7888 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7891 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7894 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007895 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007896 }
7897#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007898
Paul Bakker48916f92012-09-16 19:57:18 +00007899 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 mbedtls_ssl_transform_free( ssl->transform );
7902 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007903 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007904 }
Paul Bakker48916f92012-09-16 19:57:18 +00007905
Paul Bakkerc0463502013-02-14 11:19:38 +01007906 if( ssl->session )
7907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 mbedtls_ssl_session_free( ssl->session );
7909 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007910 ssl->session = NULL;
7911 }
7912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007914 ssl->alpn_chosen = NULL;
7915#endif
7916
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007917#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007918#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007919 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007920#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007921 {
7922 mbedtls_free( ssl->cli_id );
7923 ssl->cli_id = NULL;
7924 ssl->cli_id_len = 0;
7925 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007926#endif
7927
Paul Bakker48916f92012-09-16 19:57:18 +00007928 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7929 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007930
7931 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007932}
7933
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007934/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007935 * Reset an initialized and used SSL context for re-use while retaining
7936 * all application-set variables, function pointers and data.
7937 */
7938int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7939{
7940 return( ssl_session_reset_int( ssl, 0 ) );
7941}
7942
7943/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007944 * SSL set accessors
7945 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007946void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007947{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007948 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007949}
7950
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007951void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007952{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007953 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007954}
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007957void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007958{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007959 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007960}
7961#endif
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007964void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007965{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007966 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007967}
7968#endif
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007971
Hanno Becker1841b0a2018-08-24 11:13:57 +01007972void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7973 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007974{
7975 ssl->disable_datagram_packing = !allow_packing;
7976}
7977
7978void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7979 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007980{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007981 conf->hs_timeout_min = min;
7982 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007983}
7984#endif
7985
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007986void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007987{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007988 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007989}
7990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007992void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007993 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007994 void *p_vrfy )
7995{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007996 conf->f_vrfy = f_vrfy;
7997 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007998}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008000
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008001void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008002 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008003 void *p_rng )
8004{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008005 conf->f_rng = f_rng;
8006 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008007}
8008
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008009void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008010 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008011 void *p_dbg )
8012{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008013 conf->f_dbg = f_dbg;
8014 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008015}
8016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008018 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008019 mbedtls_ssl_send_t *f_send,
8020 mbedtls_ssl_recv_t *f_recv,
8021 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008022{
8023 ssl->p_bio = p_bio;
8024 ssl->f_send = f_send;
8025 ssl->f_recv = f_recv;
8026 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008027}
8028
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008029#if defined(MBEDTLS_SSL_PROTO_DTLS)
8030void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8031{
8032 ssl->mtu = mtu;
8033}
8034#endif
8035
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008036void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008037{
8038 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008039}
8040
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008041void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8042 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008043 mbedtls_ssl_set_timer_t *f_set_timer,
8044 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008045{
8046 ssl->p_timer = p_timer;
8047 ssl->f_set_timer = f_set_timer;
8048 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008049
8050 /* Make sure we start with no timer running */
8051 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008052}
8053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008055void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008056 void *p_cache,
8057 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8058 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008059{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008060 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008061 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008062 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066#if defined(MBEDTLS_SSL_CLI_C)
8067int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008068{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008069 int ret;
8070
8071 if( ssl == NULL ||
8072 session == NULL ||
8073 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008074 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008077 }
8078
Hanno Becker52055ae2019-02-06 14:30:46 +00008079 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8080 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008081 return( ret );
8082
Paul Bakker0a597072012-09-25 21:55:46 +00008083 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008084
8085 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008088
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008089void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008090 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008092 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8093 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8094 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8095 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008096}
8097
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008098void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008099 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008100 int major, int minor )
8101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008103 return;
8104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008106 return;
8107
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008108 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008109}
8110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008112void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008113 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008114{
8115 conf->cert_profile = profile;
8116}
8117
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008118/* Append a new keycert entry to a (possibly empty) list */
8119static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8120 mbedtls_x509_crt *cert,
8121 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008122{
niisato8ee24222018-06-25 19:05:48 +09008123 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008124
niisato8ee24222018-06-25 19:05:48 +09008125 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8126 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008127 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008128
niisato8ee24222018-06-25 19:05:48 +09008129 new_cert->cert = cert;
8130 new_cert->key = key;
8131 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008132
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008133 /* Update head is the list was null, else add to the end */
8134 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008135 {
niisato8ee24222018-06-25 19:05:48 +09008136 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008137 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008138 else
8139 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008140 mbedtls_ssl_key_cert *cur = *head;
8141 while( cur->next != NULL )
8142 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008143 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008144 }
8145
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008146 return( 0 );
8147}
8148
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008149int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008150 mbedtls_x509_crt *own_cert,
8151 mbedtls_pk_context *pk_key )
8152{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008153 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008154}
8155
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008156void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008157 mbedtls_x509_crt *ca_chain,
8158 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008159{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008160 conf->ca_chain = ca_chain;
8161 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008162
8163#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8164 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8165 * cannot be used together. */
8166 conf->f_ca_cb = NULL;
8167 conf->p_ca_cb = NULL;
8168#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008169}
Hanno Becker5adaad92019-03-27 16:54:37 +00008170
8171#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8172void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008173 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008174 void *p_ca_cb )
8175{
8176 conf->f_ca_cb = f_ca_cb;
8177 conf->p_ca_cb = p_ca_cb;
8178
8179 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8180 * cannot be used together. */
8181 conf->ca_chain = NULL;
8182 conf->ca_crl = NULL;
8183}
8184#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008186
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008187#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8188int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8189 mbedtls_x509_crt *own_cert,
8190 mbedtls_pk_context *pk_key )
8191{
8192 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8193 own_cert, pk_key ) );
8194}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008195
8196void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8197 mbedtls_x509_crt *ca_chain,
8198 mbedtls_x509_crl *ca_crl )
8199{
8200 ssl->handshake->sni_ca_chain = ca_chain;
8201 ssl->handshake->sni_ca_crl = ca_crl;
8202}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008203
8204void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8205 int authmode )
8206{
8207 ssl->handshake->sni_authmode = authmode;
8208}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008209#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8210
Hanno Becker8927c832019-04-03 12:52:50 +01008211#if defined(MBEDTLS_X509_CRT_PARSE_C)
8212void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8213 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8214 void *p_vrfy )
8215{
8216 ssl->f_vrfy = f_vrfy;
8217 ssl->p_vrfy = p_vrfy;
8218}
8219#endif
8220
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008221#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008222/*
8223 * Set EC J-PAKE password for current handshake
8224 */
8225int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8226 const unsigned char *pw,
8227 size_t pw_len )
8228{
8229 mbedtls_ecjpake_role role;
8230
Janos Follath8eb64132016-06-03 15:40:57 +01008231 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8233
8234 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8235 role = MBEDTLS_ECJPAKE_SERVER;
8236 else
8237 role = MBEDTLS_ECJPAKE_CLIENT;
8238
8239 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8240 role,
8241 MBEDTLS_MD_SHA256,
8242 MBEDTLS_ECP_DP_SECP256R1,
8243 pw, pw_len ) );
8244}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008245#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008248
8249static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8250{
8251 /* Remove reference to existing PSK, if any. */
8252#if defined(MBEDTLS_USE_PSA_CRYPTO)
8253 if( conf->psk_opaque != 0 )
8254 {
8255 /* The maintenance of the PSK key slot is the
8256 * user's responsibility. */
8257 conf->psk_opaque = 0;
8258 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008259 /* This and the following branch should never
8260 * be taken simultaenously as we maintain the
8261 * invariant that raw and opaque PSKs are never
8262 * configured simultaneously. As a safeguard,
8263 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008264#endif /* MBEDTLS_USE_PSA_CRYPTO */
8265 if( conf->psk != NULL )
8266 {
8267 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8268
8269 mbedtls_free( conf->psk );
8270 conf->psk = NULL;
8271 conf->psk_len = 0;
8272 }
8273
8274 /* Remove reference to PSK identity, if any. */
8275 if( conf->psk_identity != NULL )
8276 {
8277 mbedtls_free( conf->psk_identity );
8278 conf->psk_identity = NULL;
8279 conf->psk_identity_len = 0;
8280 }
8281}
8282
Hanno Becker7390c712018-11-15 13:33:04 +00008283/* This function assumes that PSK identity in the SSL config is unset.
8284 * It checks that the provided identity is well-formed and attempts
8285 * to make a copy of it in the SSL config.
8286 * On failure, the PSK identity in the config remains unset. */
8287static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8288 unsigned char const *psk_identity,
8289 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008290{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008291 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008292 if( psk_identity == NULL ||
8293 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008294 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008295 {
8296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8297 }
8298
Hanno Becker7390c712018-11-15 13:33:04 +00008299 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8300 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008301 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008302
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008303 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008304 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008305
8306 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008307}
8308
Hanno Becker7390c712018-11-15 13:33:04 +00008309int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8310 const unsigned char *psk, size_t psk_len,
8311 const unsigned char *psk_identity, size_t psk_identity_len )
8312{
8313 int ret;
8314 /* Remove opaque/raw PSK + PSK Identity */
8315 ssl_conf_remove_psk( conf );
8316
8317 /* Check and set raw PSK */
8318 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8320 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8321 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8322 conf->psk_len = psk_len;
8323 memcpy( conf->psk, psk, conf->psk_len );
8324
8325 /* Check and set PSK Identity */
8326 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8327 if( ret != 0 )
8328 ssl_conf_remove_psk( conf );
8329
8330 return( ret );
8331}
8332
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008333static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8334{
8335#if defined(MBEDTLS_USE_PSA_CRYPTO)
8336 if( ssl->handshake->psk_opaque != 0 )
8337 {
8338 ssl->handshake->psk_opaque = 0;
8339 }
8340 else
8341#endif /* MBEDTLS_USE_PSA_CRYPTO */
8342 if( ssl->handshake->psk != NULL )
8343 {
8344 mbedtls_platform_zeroize( ssl->handshake->psk,
8345 ssl->handshake->psk_len );
8346 mbedtls_free( ssl->handshake->psk );
8347 ssl->handshake->psk_len = 0;
8348 }
8349}
8350
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008351int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8352 const unsigned char *psk, size_t psk_len )
8353{
8354 if( psk == NULL || ssl->handshake == NULL )
8355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8356
8357 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8359
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008360 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008361
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008362 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008363 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008364
8365 ssl->handshake->psk_len = psk_len;
8366 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8367
8368 return( 0 );
8369}
8370
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008371#if defined(MBEDTLS_USE_PSA_CRYPTO)
8372int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008373 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008374 const unsigned char *psk_identity,
8375 size_t psk_identity_len )
8376{
Hanno Becker7390c712018-11-15 13:33:04 +00008377 int ret;
8378 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008379 ssl_conf_remove_psk( conf );
8380
Hanno Becker7390c712018-11-15 13:33:04 +00008381 /* Check and set opaque PSK */
8382 if( psk_slot == 0 )
8383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008384 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008385
8386 /* Check and set PSK Identity */
8387 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8388 psk_identity_len );
8389 if( ret != 0 )
8390 ssl_conf_remove_psk( conf );
8391
8392 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008393}
8394
8395int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008396 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008397{
8398 if( psk_slot == 0 || ssl->handshake == NULL )
8399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8400
8401 ssl_remove_psk( ssl );
8402 ssl->handshake->psk_opaque = psk_slot;
8403 return( 0 );
8404}
8405#endif /* MBEDTLS_USE_PSA_CRYPTO */
8406
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008407void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008409 size_t),
8410 void *p_psk )
8411{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008412 conf->f_psk = f_psk;
8413 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008416
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008417#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008418
8419#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008420int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008421{
8422 int ret;
8423
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008424 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8425 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8426 {
8427 mbedtls_mpi_free( &conf->dhm_P );
8428 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008429 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008431
8432 return( 0 );
8433}
Hanno Becker470a8c42017-10-04 15:28:46 +01008434#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008435
Hanno Beckera90658f2017-10-04 15:29:08 +01008436int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8437 const unsigned char *dhm_P, size_t P_len,
8438 const unsigned char *dhm_G, size_t G_len )
8439{
8440 int ret;
8441
8442 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8443 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8444 {
8445 mbedtls_mpi_free( &conf->dhm_P );
8446 mbedtls_mpi_free( &conf->dhm_G );
8447 return( ret );
8448 }
8449
8450 return( 0 );
8451}
Paul Bakker5121ce52009-01-03 21:22:43 +00008452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008453int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008454{
8455 int ret;
8456
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008457 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8458 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8459 {
8460 mbedtls_mpi_free( &conf->dhm_P );
8461 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008462 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008463 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008464
8465 return( 0 );
8466}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008467#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008468
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008469#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8470/*
8471 * Set the minimum length for Diffie-Hellman parameters
8472 */
8473void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8474 unsigned int bitlen )
8475{
8476 conf->dhm_min_bitlen = bitlen;
8477}
8478#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8479
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008480#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008481/*
8482 * Set allowed/preferred hashes for handshake signatures
8483 */
8484void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8485 const int *hashes )
8486{
8487 conf->sig_hashes = hashes;
8488}
Hanno Becker947194e2017-04-07 13:25:49 +01008489#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008490
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008491#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008492/*
8493 * Set the allowed elliptic curves
8494 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008495void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008496 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008497{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008498 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008499}
Hanno Becker947194e2017-04-07 13:25:49 +01008500#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008501
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008502#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008503int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008504{
Hanno Becker947194e2017-04-07 13:25:49 +01008505 /* Initialize to suppress unnecessary compiler warning */
8506 size_t hostname_len = 0;
8507
8508 /* Check if new hostname is valid before
8509 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008510 if( hostname != NULL )
8511 {
8512 hostname_len = strlen( hostname );
8513
8514 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8516 }
8517
8518 /* Now it's clear that we will overwrite the old hostname,
8519 * so we can free it safely */
8520
8521 if( ssl->hostname != NULL )
8522 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008523 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008524 mbedtls_free( ssl->hostname );
8525 }
8526
8527 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008528
Paul Bakker5121ce52009-01-03 21:22:43 +00008529 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008530 {
8531 ssl->hostname = NULL;
8532 }
8533 else
8534 {
8535 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008536 if( ssl->hostname == NULL )
8537 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008538
Hanno Becker947194e2017-04-07 13:25:49 +01008539 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008540
Hanno Becker947194e2017-04-07 13:25:49 +01008541 ssl->hostname[hostname_len] = '\0';
8542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008543
8544 return( 0 );
8545}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008546#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008547
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008548#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008549void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008551 const unsigned char *, size_t),
8552 void *p_sni )
8553{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008554 conf->f_sni = f_sni;
8555 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008560int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008561{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008562 size_t cur_len, tot_len;
8563 const char **p;
8564
8565 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008566 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8567 * MUST NOT be truncated."
8568 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008569 */
8570 tot_len = 0;
8571 for( p = protos; *p != NULL; p++ )
8572 {
8573 cur_len = strlen( *p );
8574 tot_len += cur_len;
8575
8576 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008578 }
8579
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008580 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008581
8582 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008583}
8584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008586{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008587 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008590
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008591void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008593 conf->max_major_ver = major;
8594 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008595}
8596
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008597void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008599 conf->min_major_ver = major;
8600 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008601}
8602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008604void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008605{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008606 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008607}
8608#endif
8609
Janos Follath088ce432017-04-10 12:42:31 +01008610#if defined(MBEDTLS_SSL_SRV_C)
8611void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8612 char cert_req_ca_list )
8613{
8614 conf->cert_req_ca_list = cert_req_ca_list;
8615}
8616#endif
8617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008619void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008620{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008621 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008622}
8623#endif
8624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008626void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008628 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008629}
8630#endif
8631
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008632#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008633void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008635 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008636}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008637#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008640int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008643 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008646 }
8647
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008648 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008649
8650 return( 0 );
8651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008655void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008657 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008662void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008663{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008664 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008665}
8666#endif
8667
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008668void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008670 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008671}
8672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008674void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008676 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008677}
8678
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008679void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008681 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008682}
8683
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008684void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008685 const unsigned char period[8] )
8686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008687 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008692#if defined(MBEDTLS_SSL_CLI_C)
8693void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008694{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008695 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008696}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008697#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008698
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008699#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008700void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8701 mbedtls_ssl_ticket_write_t *f_ticket_write,
8702 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8703 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008704{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008705 conf->f_ticket_write = f_ticket_write;
8706 conf->f_ticket_parse = f_ticket_parse;
8707 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008708}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008711
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008712#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8713void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8714 mbedtls_ssl_export_keys_t *f_export_keys,
8715 void *p_export_keys )
8716{
8717 conf->f_export_keys = f_export_keys;
8718 conf->p_export_keys = p_export_keys;
8719}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008720
8721void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8722 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8723 void *p_export_keys )
8724{
8725 conf->f_export_keys_ext = f_export_keys_ext;
8726 conf->p_export_keys = p_export_keys;
8727}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008728#endif
8729
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008730#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008731void mbedtls_ssl_conf_async_private_cb(
8732 mbedtls_ssl_config *conf,
8733 mbedtls_ssl_async_sign_t *f_async_sign,
8734 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8735 mbedtls_ssl_async_resume_t *f_async_resume,
8736 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008737 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008738{
8739 conf->f_async_sign_start = f_async_sign;
8740 conf->f_async_decrypt_start = f_async_decrypt;
8741 conf->f_async_resume = f_async_resume;
8742 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008743 conf->p_async_config_data = async_config_data;
8744}
8745
Gilles Peskine8f97af72018-04-26 11:46:10 +02008746void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8747{
8748 return( conf->p_async_config_data );
8749}
8750
Gilles Peskine1febfef2018-04-30 11:54:39 +02008751void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008752{
8753 if( ssl->handshake == NULL )
8754 return( NULL );
8755 else
8756 return( ssl->handshake->user_async_ctx );
8757}
8758
Gilles Peskine1febfef2018-04-30 11:54:39 +02008759void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008760 void *ctx )
8761{
8762 if( ssl->handshake != NULL )
8763 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008764}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008765#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008766
Paul Bakker5121ce52009-01-03 21:22:43 +00008767/*
8768 * SSL get accessors
8769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008771{
8772 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8773}
8774
Hanno Becker8b170a02017-10-10 11:51:19 +01008775int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8776{
8777 /*
8778 * Case A: We're currently holding back
8779 * a message for further processing.
8780 */
8781
8782 if( ssl->keep_current_message == 1 )
8783 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008785 return( 1 );
8786 }
8787
8788 /*
8789 * Case B: Further records are pending in the current datagram.
8790 */
8791
8792#if defined(MBEDTLS_SSL_PROTO_DTLS)
8793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8794 ssl->in_left > ssl->next_record_offset )
8795 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008796 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008797 return( 1 );
8798 }
8799#endif /* MBEDTLS_SSL_PROTO_DTLS */
8800
8801 /*
8802 * Case C: A handshake message is being processed.
8803 */
8804
Hanno Becker8b170a02017-10-10 11:51:19 +01008805 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8806 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008808 return( 1 );
8809 }
8810
8811 /*
8812 * Case D: An application data message is being processed
8813 */
8814 if( ssl->in_offt != NULL )
8815 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008817 return( 1 );
8818 }
8819
8820 /*
8821 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008822 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008823 * we implement support for multiple alerts in single records.
8824 */
8825
8826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8827 return( 0 );
8828}
8829
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008830uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008831{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008832 if( ssl->session != NULL )
8833 return( ssl->session->verify_result );
8834
8835 if( ssl->session_negotiate != NULL )
8836 return( ssl->session_negotiate->verify_result );
8837
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008838 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008839}
8840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008842{
Paul Bakker926c8e42013-03-06 10:23:34 +01008843 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008844 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008847}
8848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008853 {
8854 switch( ssl->minor_ver )
8855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008857 return( "DTLSv1.0" );
8858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008860 return( "DTLSv1.2" );
8861
8862 default:
8863 return( "unknown (DTLS)" );
8864 }
8865 }
8866#endif
8867
Paul Bakker43ca69c2011-01-15 17:35:19 +00008868 switch( ssl->minor_ver )
8869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008871 return( "SSLv3.0" );
8872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008874 return( "TLSv1.0" );
8875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008877 return( "TLSv1.1" );
8878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008880 return( "TLSv1.2" );
8881
Paul Bakker43ca69c2011-01-15 17:35:19 +00008882 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008883 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008884 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008885}
8886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008888{
Hanno Becker3136ede2018-08-17 15:28:19 +01008889 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008891 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008892
Hanno Becker78640902018-08-13 16:35:15 +01008893 if( transform == NULL )
8894 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896#if defined(MBEDTLS_ZLIB_SUPPORT)
8897 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8898 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008899#endif
8900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008901 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 case MBEDTLS_MODE_GCM:
8904 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008905 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008907 transform_expansion = transform->minlen;
8908 break;
8909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008910 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008911
8912 block_size = mbedtls_cipher_get_block_size(
8913 &transform->cipher_ctx_enc );
8914
Hanno Becker3136ede2018-08-17 15:28:19 +01008915 /* Expansion due to the addition of the MAC. */
8916 transform_expansion += transform->maclen;
8917
8918 /* Expansion due to the addition of CBC padding;
8919 * Theoretically up to 256 bytes, but we never use
8920 * more than the block size of the underlying cipher. */
8921 transform_expansion += block_size;
8922
8923 /* For TLS 1.1 or higher, an explicit IV is added
8924 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008925#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8926 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008927 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008928#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008929
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008930 break;
8931
8932 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008935 }
8936
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008937 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008938}
8939
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008940#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8941size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8942{
8943 size_t max_len;
8944
8945 /*
8946 * Assume mfl_code is correct since it was checked when set
8947 */
Angus Grattond8213d02016-05-25 20:56:48 +10008948 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008949
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008950 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008951 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008952 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008953 {
Angus Grattond8213d02016-05-25 20:56:48 +10008954 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008955 }
8956
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008957 /* During a handshake, use the value being negotiated */
8958 if( ssl->session_negotiate != NULL &&
8959 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8960 {
8961 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8962 }
8963
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008964 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008965}
8966#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8967
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008968#if defined(MBEDTLS_SSL_PROTO_DTLS)
8969static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8970{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008971 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8972 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8973 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8974 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8975 return ( 0 );
8976
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008977 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8978 return( ssl->mtu );
8979
8980 if( ssl->mtu == 0 )
8981 return( ssl->handshake->mtu );
8982
8983 return( ssl->mtu < ssl->handshake->mtu ?
8984 ssl->mtu : ssl->handshake->mtu );
8985}
8986#endif /* MBEDTLS_SSL_PROTO_DTLS */
8987
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008988int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8989{
8990 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8991
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008992#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8993 !defined(MBEDTLS_SSL_PROTO_DTLS)
8994 (void) ssl;
8995#endif
8996
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008997#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8998 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8999
9000 if( max_len > mfl )
9001 max_len = mfl;
9002#endif
9003
9004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009005 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009006 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009007 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009008 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9009 const size_t overhead = (size_t) ret;
9010
9011 if( ret < 0 )
9012 return( ret );
9013
9014 if( mtu <= overhead )
9015 {
9016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9017 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9018 }
9019
9020 if( max_len > mtu - overhead )
9021 max_len = mtu - overhead;
9022 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009023#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009024
Hanno Becker0defedb2018-08-10 12:35:02 +01009025#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9026 !defined(MBEDTLS_SSL_PROTO_DTLS)
9027 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009028#endif
9029
9030 return( (int) max_len );
9031}
9032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033#if defined(MBEDTLS_X509_CRT_PARSE_C)
9034const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009035{
9036 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009037 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009038
Hanno Beckere6824572019-02-07 13:18:46 +00009039#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009040 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009041#else
9042 return( NULL );
9043#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009048int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9049 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009050{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009051 if( ssl == NULL ||
9052 dst == NULL ||
9053 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009054 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009057 }
9058
Hanno Becker52055ae2019-02-06 14:30:46 +00009059 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009062
Paul Bakker5121ce52009-01-03 21:22:43 +00009063/*
Paul Bakker1961b702013-01-25 14:49:24 +01009064 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009069
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009070 if( ssl == NULL || ssl->conf == NULL )
9071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009078 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009080#endif
9081
Paul Bakker1961b702013-01-25 14:49:24 +01009082 return( ret );
9083}
9084
9085/*
9086 * Perform the SSL handshake
9087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009089{
9090 int ret = 0;
9091
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009092 if( ssl == NULL || ssl->conf == NULL )
9093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009100
9101 if( ret != 0 )
9102 break;
9103 }
9104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009106
9107 return( ret );
9108}
9109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110#if defined(MBEDTLS_SSL_RENEGOTIATION)
9111#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009112/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009113 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009116{
9117 int ret;
9118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009120
9121 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9123 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009124
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009125 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009126 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009128 return( ret );
9129 }
9130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009132
9133 return( 0 );
9134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009136
9137/*
9138 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139 * - any side: calling mbedtls_ssl_renegotiate(),
9140 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9141 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009142 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009143 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009147{
9148 int ret;
9149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009151
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009152 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9153 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009154
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009155 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9156 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009160 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009161 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009162 ssl->handshake->out_msg_seq = 1;
9163 else
9164 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009165 }
9166#endif
9167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9169 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009174 return( ret );
9175 }
9176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009178
9179 return( 0 );
9180}
9181
9182/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009183 * Renegotiate current connection on client,
9184 * or request renegotiation on server
9185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009189
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009190 if( ssl == NULL || ssl->conf == NULL )
9191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009194 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009197 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009201
9202 /* Did we already try/start sending HelloRequest? */
9203 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009205
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009206 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009211 /*
9212 * On client, either start the renegotiation process or,
9213 * if already in progress, continue the handshake
9214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009219
9220 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009223 return( ret );
9224 }
9225 }
9226 else
9227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009231 return( ret );
9232 }
9233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009235
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009236 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009237}
9238
9239/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009240 * Check record counters and renegotiate if they're above the limit.
9241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009243{
Andres AG2196c7f2016-12-15 17:01:16 +00009244 size_t ep_len = ssl_ep_len( ssl );
9245 int in_ctr_cmp;
9246 int out_ctr_cmp;
9247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9249 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009250 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009251 {
9252 return( 0 );
9253 }
9254
Andres AG2196c7f2016-12-15 17:01:16 +00009255 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9256 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009257 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009258 ssl->conf->renego_period + ep_len, 8 - ep_len );
9259
9260 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009261 {
9262 return( 0 );
9263 }
9264
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009269
9270/*
9271 * Receive application data decrypted from the SSL layer
9272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009274{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009275 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009276 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009277
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009278 if( ssl == NULL || ssl->conf == NULL )
9279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009287 return( ret );
9288
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009289 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009291 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009292 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009293 return( ret );
9294 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009295 }
9296#endif
9297
Hanno Becker4a810fb2017-05-24 16:27:30 +01009298 /*
9299 * Check if renegotiation is necessary and/or handshake is
9300 * in process. If yes, perform/continue, and fall through
9301 * if an unexpected packet is received while the client
9302 * is waiting for the ServerHello.
9303 *
9304 * (There is no equivalent to the last condition on
9305 * the server-side as it is not treated as within
9306 * a handshake while waiting for the ClientHello
9307 * after a renegotiation request.)
9308 */
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009311 ret = ssl_check_ctr_renegotiate( ssl );
9312 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9313 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009316 return( ret );
9317 }
9318#endif
9319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009323 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9324 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009327 return( ret );
9328 }
9329 }
9330
Hanno Beckere41158b2017-10-23 13:30:32 +01009331 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009332 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009333 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009334 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009335 if( ssl->f_get_timer != NULL &&
9336 ssl->f_get_timer( ssl->p_timer ) == -1 )
9337 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009338 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009339 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009340
Hanno Becker327c93b2018-08-15 13:56:18 +01009341 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009342 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009343 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9344 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009345
Hanno Becker4a810fb2017-05-24 16:27:30 +01009346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9347 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009348 }
9349
9350 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009352 {
9353 /*
9354 * OpenSSL sends empty messages to randomize the IV
9355 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009356 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009359 return( 0 );
9360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009362 return( ret );
9363 }
9364 }
9365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009369
Hanno Becker4a810fb2017-05-24 16:27:30 +01009370 /*
9371 * - For client-side, expect SERVER_HELLO_REQUEST.
9372 * - For server-side, expect CLIENT_HELLO.
9373 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9374 */
9375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009379 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009382
9383 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009385 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009386 {
9387 continue;
9388 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009391 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009392#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009393
Hanno Becker4a810fb2017-05-24 16:27:30 +01009394#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009395 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009399
9400 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009402 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009403 {
9404 continue;
9405 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009408 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009409#endif /* MBEDTLS_SSL_SRV_C */
9410
Hanno Becker21df7f92017-10-17 11:03:26 +01009411#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009412 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009413 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9414 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9415 ssl->conf->allow_legacy_renegotiation ==
9416 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9417 {
9418 /*
9419 * Accept renegotiation request
9420 */
Paul Bakker48916f92012-09-16 19:57:18 +00009421
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009422 /* DTLS clients need to know renego is server-initiated */
9423#if defined(MBEDTLS_SSL_PROTO_DTLS)
9424 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9425 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9426 {
9427 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9428 }
9429#endif
9430 ret = ssl_start_renegotiation( ssl );
9431 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9432 ret != 0 )
9433 {
9434 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9435 return( ret );
9436 }
9437 }
9438 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009439#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009440 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009441 /*
9442 * Refuse renegotiation
9443 */
9444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447#if defined(MBEDTLS_SSL_PROTO_SSL3)
9448 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009449 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009450 /* SSLv3 does not have a "no_renegotiation" warning, so
9451 we send a fatal alert and abort the connection. */
9452 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9453 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9454 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009455 }
9456 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9458#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9459 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9460 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9463 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9464 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009465 {
9466 return( ret );
9467 }
Paul Bakker48916f92012-09-16 19:57:18 +00009468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009469 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9471 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009475 }
Paul Bakker48916f92012-09-16 19:57:18 +00009476 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009477
Hanno Becker90333da2017-10-10 11:27:13 +01009478 /* At this point, we don't know whether the renegotiation has been
9479 * completed or not. The cases to consider are the following:
9480 * 1) The renegotiation is complete. In this case, no new record
9481 * has been read yet.
9482 * 2) The renegotiation is incomplete because the client received
9483 * an application data record while awaiting the ServerHello.
9484 * 3) The renegotiation is incomplete because the client received
9485 * a non-handshake, non-application data message while awaiting
9486 * the ServerHello.
9487 * In each of these case, looping will be the proper action:
9488 * - For 1), the next iteration will read a new record and check
9489 * if it's application data.
9490 * - For 2), the loop condition isn't satisfied as application data
9491 * is present, hence continue is the same as break
9492 * - For 3), the loop condition is satisfied and read_record
9493 * will re-deliver the message that was held back by the client
9494 * when expecting the ServerHello.
9495 */
9496 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009497 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009498#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009500 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009501 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009502 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009503 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009506 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009508 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009509 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009510 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9514 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009517 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009518 }
9519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9523 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009524 }
9525
9526 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009527
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009528 /* We're going to return something now, cancel timer,
9529 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009531 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009532
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009534 /* If we requested renego but received AppData, resend HelloRequest.
9535 * Do it now, after setting in_offt, to avoid taking this branch
9536 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009538 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009540 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009541 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009544 return( ret );
9545 }
9546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009548#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009549 }
9550
9551 n = ( len < ssl->in_msglen )
9552 ? len : ssl->in_msglen;
9553
9554 memcpy( buf, ssl->in_offt, n );
9555 ssl->in_msglen -= n;
9556
9557 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009558 {
9559 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009560 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009561 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009563 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009564 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009565 /* more data available */
9566 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009570
Paul Bakker23986e52011-04-24 08:57:21 +00009571 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009572}
9573
9574/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009575 * Send application data to be encrypted by the SSL layer, taking care of max
9576 * fragment length and buffer size.
9577 *
9578 * According to RFC 5246 Section 6.2.1:
9579 *
9580 * Zero-length fragments of Application data MAY be sent as they are
9581 * potentially useful as a traffic analysis countermeasure.
9582 *
9583 * Therefore, it is possible that the input message length is 0 and the
9584 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009585 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009586static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009587 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009588{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009589 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9590 const size_t max_len = (size_t) ret;
9591
9592 if( ret < 0 )
9593 {
9594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9595 return( ret );
9596 }
9597
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009598 if( len > max_len )
9599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009601 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009604 "maximum fragment length: %d > %d",
9605 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009607 }
9608 else
9609#endif
9610 len = max_len;
9611 }
Paul Bakker887bd502011-06-08 13:10:54 +00009612
Paul Bakker5121ce52009-01-03 21:22:43 +00009613 if( ssl->out_left != 0 )
9614 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009615 /*
9616 * The user has previously tried to send the data and
9617 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9618 * written. In this case, we expect the high-level write function
9619 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009624 return( ret );
9625 }
9626 }
Paul Bakker887bd502011-06-08 13:10:54 +00009627 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009628 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009629 /*
9630 * The user is trying to send a message the first time, so we need to
9631 * copy the data into the internal buffers and setup the data structure
9632 * to keep track of partial writes
9633 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009634 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009636 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009637
Hanno Becker67bc7c32018-08-06 11:33:50 +01009638 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009641 return( ret );
9642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009643 }
9644
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009645 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009646}
9647
9648/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009649 * Write application data, doing 1/n-1 splitting if necessary.
9650 *
9651 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009652 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009653 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009656static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009657 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009658{
9659 int ret;
9660
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009661 if( ssl->conf->cbc_record_splitting ==
9662 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009663 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9665 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9666 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009667 {
9668 return( ssl_write_real( ssl, buf, len ) );
9669 }
9670
9671 if( ssl->split_done == 0 )
9672 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009673 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009674 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009675 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009676 }
9677
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009678 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9679 return( ret );
9680 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009681
9682 return( ret + 1 );
9683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009685
9686/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009687 * Write application data (public-facing wrapper)
9688 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009689int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009690{
9691 int ret;
9692
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009694
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009695 if( ssl == NULL || ssl->conf == NULL )
9696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9697
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009698#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009699 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9700 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009701 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009702 return( ret );
9703 }
9704#endif
9705
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009706 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009707 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009708 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009709 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009711 return( ret );
9712 }
9713 }
9714
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009715#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009716 ret = ssl_write_split( ssl, buf, len );
9717#else
9718 ret = ssl_write_real( ssl, buf, len );
9719#endif
9720
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009722
9723 return( ret );
9724}
9725
9726/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009727 * Notify the peer that the connection is being closed
9728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009730{
9731 int ret;
9732
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009733 if( ssl == NULL || ssl->conf == NULL )
9734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009737
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009738 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9744 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9745 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009748 return( ret );
9749 }
9750 }
9751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009753
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009754 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009755}
9756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009758{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009759 if( transform == NULL )
9760 return;
9761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009763 deflateEnd( &transform->ctx_deflate );
9764 inflateEnd( &transform->ctx_inflate );
9765#endif
9766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9768 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009769
Hanno Beckerd56ed242018-01-03 15:32:51 +00009770#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 mbedtls_md_free( &transform->md_ctx_enc );
9772 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009773#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009774
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009775 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009776}
9777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778#if defined(MBEDTLS_X509_CRT_PARSE_C)
9779static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009780{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009782
9783 while( cur != NULL )
9784 {
9785 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009787 cur = next;
9788 }
9789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009791
Hanno Becker0271f962018-08-16 13:23:47 +01009792#if defined(MBEDTLS_SSL_PROTO_DTLS)
9793
9794static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9795{
9796 unsigned offset;
9797 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9798
9799 if( hs == NULL )
9800 return;
9801
Hanno Becker283f5ef2018-08-24 09:34:47 +01009802 ssl_free_buffered_record( ssl );
9803
Hanno Becker0271f962018-08-16 13:23:47 +01009804 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009805 ssl_buffering_free_slot( ssl, offset );
9806}
9807
9808static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9809 uint8_t slot )
9810{
9811 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9812 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009813
9814 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9815 return;
9816
Hanno Beckere605b192018-08-21 15:59:07 +01009817 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009818 {
Hanno Beckere605b192018-08-21 15:59:07 +01009819 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009820 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009821 mbedtls_free( hs_buf->data );
9822 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009823 }
9824}
9825
9826#endif /* MBEDTLS_SSL_PROTO_DTLS */
9827
Gilles Peskine9b562d52018-04-25 20:32:43 +02009828void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009829{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009830 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9831
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009832 if( handshake == NULL )
9833 return;
9834
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009835#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9836 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9837 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009838 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009839 handshake->async_in_progress = 0;
9840 }
9841#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9842
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009843#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9844 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9845 mbedtls_md5_free( &handshake->fin_md5 );
9846 mbedtls_sha1_free( &handshake->fin_sha1 );
9847#endif
9848#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9849#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009850#if defined(MBEDTLS_USE_PSA_CRYPTO)
9851 psa_hash_abort( &handshake->fin_sha256_psa );
9852#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009853 mbedtls_sha256_free( &handshake->fin_sha256 );
9854#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009855#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009856#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009857#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009858 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009859#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009860 mbedtls_sha512_free( &handshake->fin_sha512 );
9861#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009862#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009863#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865#if defined(MBEDTLS_DHM_C)
9866 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868#if defined(MBEDTLS_ECDH_C)
9869 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009870#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009871#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009872 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009873#if defined(MBEDTLS_SSL_CLI_C)
9874 mbedtls_free( handshake->ecjpake_cache );
9875 handshake->ecjpake_cache = NULL;
9876 handshake->ecjpake_cache_len = 0;
9877#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009878#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009879
Janos Follath4ae5c292016-02-10 11:27:43 +00009880#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9881 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009882 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009884#endif
9885
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009886#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9887 if( handshake->psk != NULL )
9888 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009889 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009890 mbedtls_free( handshake->psk );
9891 }
9892#endif
9893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9895 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009896 /*
9897 * Free only the linked list wrapper, not the keys themselves
9898 * since the belong to the SNI callback
9899 */
9900 if( handshake->sni_key_cert != NULL )
9901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009903
9904 while( cur != NULL )
9905 {
9906 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009908 cur = next;
9909 }
9910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009912
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009913#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009914 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009915 if( handshake->ecrs_peer_cert != NULL )
9916 {
9917 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9918 mbedtls_free( handshake->ecrs_peer_cert );
9919 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009920#endif
9921
Hanno Becker75173122019-02-06 16:18:31 +00009922#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9923 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9924 mbedtls_pk_free( &handshake->peer_pubkey );
9925#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927#if defined(MBEDTLS_SSL_PROTO_DTLS)
9928 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009929 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009930 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009931#endif
9932
Hanno Becker4a63ed42019-01-08 11:39:35 +00009933#if defined(MBEDTLS_ECDH_C) && \
9934 defined(MBEDTLS_USE_PSA_CRYPTO)
9935 psa_destroy_key( handshake->ecdh_psa_privkey );
9936#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9937
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009938 mbedtls_platform_zeroize( handshake,
9939 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009940}
9941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009943{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009944 if( session == NULL )
9945 return;
9946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009948 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009949#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009950
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009951#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009953#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009954
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009955 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009956}
9957
Paul Bakker5121ce52009-01-03 21:22:43 +00009958/*
9959 * Free an SSL context
9960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009962{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009963 if( ssl == NULL )
9964 return;
9965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009967
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009968 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009969 {
Angus Grattond8213d02016-05-25 20:56:48 +10009970 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009972 }
9973
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009974 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009975 {
Angus Grattond8213d02016-05-25 20:56:48 +10009976 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009978 }
9979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009981 if( ssl->compress_buf != NULL )
9982 {
Angus Grattond8213d02016-05-25 20:56:48 +10009983 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009985 }
9986#endif
9987
Paul Bakker48916f92012-09-16 19:57:18 +00009988 if( ssl->transform )
9989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 mbedtls_ssl_transform_free( ssl->transform );
9991 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009992 }
9993
9994 if( ssl->handshake )
9995 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009996 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9998 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 mbedtls_free( ssl->handshake );
10001 mbedtls_free( ssl->transform_negotiate );
10002 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010003 }
10004
Paul Bakkerc0463502013-02-14 11:19:38 +010010005 if( ssl->session )
10006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 mbedtls_ssl_session_free( ssl->session );
10008 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010009 }
10010
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010011#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010012 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010013 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010014 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010015 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010016 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010017#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10020 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10023 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010024 }
10025#endif
10026
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010027#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010029#endif
10030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010032
Paul Bakker86f04f42013-02-14 11:20:09 +010010033 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010034 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010035}
10036
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010037/*
10038 * Initialze mbedtls_ssl_config
10039 */
10040void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10041{
10042 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10043}
10044
Simon Butcherc97b6972015-12-27 23:48:17 +000010045#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010046static int ssl_preset_default_hashes[] = {
10047#if defined(MBEDTLS_SHA512_C)
10048 MBEDTLS_MD_SHA512,
10049 MBEDTLS_MD_SHA384,
10050#endif
10051#if defined(MBEDTLS_SHA256_C)
10052 MBEDTLS_MD_SHA256,
10053 MBEDTLS_MD_SHA224,
10054#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010055#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010056 MBEDTLS_MD_SHA1,
10057#endif
10058 MBEDTLS_MD_NONE
10059};
Simon Butcherc97b6972015-12-27 23:48:17 +000010060#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010061
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010062static int ssl_preset_suiteb_ciphersuites[] = {
10063 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10064 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10065 0
10066};
10067
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010068#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010069static int ssl_preset_suiteb_hashes[] = {
10070 MBEDTLS_MD_SHA256,
10071 MBEDTLS_MD_SHA384,
10072 MBEDTLS_MD_NONE
10073};
10074#endif
10075
10076#if defined(MBEDTLS_ECP_C)
10077static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10078 MBEDTLS_ECP_DP_SECP256R1,
10079 MBEDTLS_ECP_DP_SECP384R1,
10080 MBEDTLS_ECP_DP_NONE
10081};
10082#endif
10083
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010084/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010085 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010086 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010087int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010088 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010089{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010090#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010091 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010092#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010093
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010094 /* Use the functions here so that they are covered in tests,
10095 * but otherwise access member directly for efficiency */
10096 mbedtls_ssl_conf_endpoint( conf, endpoint );
10097 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010098
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010099 /*
10100 * Things that are common to all presets
10101 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010102#if defined(MBEDTLS_SSL_CLI_C)
10103 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10104 {
10105 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10106#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10107 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10108#endif
10109 }
10110#endif
10111
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010112#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010113 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010114#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010115
10116#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10117 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10118#endif
10119
10120#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10121 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10122#endif
10123
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010124#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10125 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10126#endif
10127
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010128#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010129 conf->f_cookie_write = ssl_cookie_write_dummy;
10130 conf->f_cookie_check = ssl_cookie_check_dummy;
10131#endif
10132
10133#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10134 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10135#endif
10136
Janos Follath088ce432017-04-10 12:42:31 +010010137#if defined(MBEDTLS_SSL_SRV_C)
10138 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10139#endif
10140
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010141#if defined(MBEDTLS_SSL_PROTO_DTLS)
10142 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10143 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10144#endif
10145
10146#if defined(MBEDTLS_SSL_RENEGOTIATION)
10147 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010148 memset( conf->renego_period, 0x00, 2 );
10149 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010150#endif
10151
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010152#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10153 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10154 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010155 const unsigned char dhm_p[] =
10156 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10157 const unsigned char dhm_g[] =
10158 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10159
Hanno Beckera90658f2017-10-04 15:29:08 +010010160 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10161 dhm_p, sizeof( dhm_p ),
10162 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010163 {
10164 return( ret );
10165 }
10166 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010167#endif
10168
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010169 /*
10170 * Preset-specific defaults
10171 */
10172 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010173 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010174 /*
10175 * NSA Suite B
10176 */
10177 case MBEDTLS_SSL_PRESET_SUITEB:
10178 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10179 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10180 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10181 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10182
10183 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10184 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10185 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10186 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10187 ssl_preset_suiteb_ciphersuites;
10188
10189#if defined(MBEDTLS_X509_CRT_PARSE_C)
10190 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010191#endif
10192
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010193#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010194 conf->sig_hashes = ssl_preset_suiteb_hashes;
10195#endif
10196
10197#if defined(MBEDTLS_ECP_C)
10198 conf->curve_list = ssl_preset_suiteb_curves;
10199#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010200 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010201
10202 /*
10203 * Default
10204 */
10205 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010206 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10207 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10208 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10209 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10210 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10211 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10212 MBEDTLS_SSL_MIN_MINOR_VERSION :
10213 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010214 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10215 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10216
10217#if defined(MBEDTLS_SSL_PROTO_DTLS)
10218 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10219 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10220#endif
10221
10222 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10223 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10224 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10225 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10226 mbedtls_ssl_list_ciphersuites();
10227
10228#if defined(MBEDTLS_X509_CRT_PARSE_C)
10229 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10230#endif
10231
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010232#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010233 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010234#endif
10235
10236#if defined(MBEDTLS_ECP_C)
10237 conf->curve_list = mbedtls_ecp_grp_id_list();
10238#endif
10239
10240#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10241 conf->dhm_min_bitlen = 1024;
10242#endif
10243 }
10244
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010245 return( 0 );
10246}
10247
10248/*
10249 * Free mbedtls_ssl_config
10250 */
10251void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10252{
10253#if defined(MBEDTLS_DHM_C)
10254 mbedtls_mpi_free( &conf->dhm_P );
10255 mbedtls_mpi_free( &conf->dhm_G );
10256#endif
10257
10258#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10259 if( conf->psk != NULL )
10260 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010261 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010262 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010263 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010264 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010265 }
10266
10267 if( conf->psk_identity != NULL )
10268 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010269 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010270 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010271 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010272 conf->psk_identity_len = 0;
10273 }
10274#endif
10275
10276#if defined(MBEDTLS_X509_CRT_PARSE_C)
10277 ssl_key_cert_free( conf->key_cert );
10278#endif
10279
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010280 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010281}
10282
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010283#if defined(MBEDTLS_PK_C) && \
10284 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010285/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010289{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290#if defined(MBEDTLS_RSA_C)
10291 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10292 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294#if defined(MBEDTLS_ECDSA_C)
10295 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10296 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010299}
10300
Hanno Becker7e5437a2017-04-28 17:15:26 +010010301unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10302{
10303 switch( type ) {
10304 case MBEDTLS_PK_RSA:
10305 return( MBEDTLS_SSL_SIG_RSA );
10306 case MBEDTLS_PK_ECDSA:
10307 case MBEDTLS_PK_ECKEY:
10308 return( MBEDTLS_SSL_SIG_ECDSA );
10309 default:
10310 return( MBEDTLS_SSL_SIG_ANON );
10311 }
10312}
10313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010315{
10316 switch( sig )
10317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318#if defined(MBEDTLS_RSA_C)
10319 case MBEDTLS_SSL_SIG_RSA:
10320 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_ECDSA_C)
10323 case MBEDTLS_SSL_SIG_ECDSA:
10324 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010325#endif
10326 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010328 }
10329}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010330#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010331
Hanno Becker7e5437a2017-04-28 17:15:26 +010010332#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10333 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10334
10335/* Find an entry in a signature-hash set matching a given hash algorithm. */
10336mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10337 mbedtls_pk_type_t sig_alg )
10338{
10339 switch( sig_alg )
10340 {
10341 case MBEDTLS_PK_RSA:
10342 return( set->rsa );
10343 case MBEDTLS_PK_ECDSA:
10344 return( set->ecdsa );
10345 default:
10346 return( MBEDTLS_MD_NONE );
10347 }
10348}
10349
10350/* Add a signature-hash-pair to a signature-hash set */
10351void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10352 mbedtls_pk_type_t sig_alg,
10353 mbedtls_md_type_t md_alg )
10354{
10355 switch( sig_alg )
10356 {
10357 case MBEDTLS_PK_RSA:
10358 if( set->rsa == MBEDTLS_MD_NONE )
10359 set->rsa = md_alg;
10360 break;
10361
10362 case MBEDTLS_PK_ECDSA:
10363 if( set->ecdsa == MBEDTLS_MD_NONE )
10364 set->ecdsa = md_alg;
10365 break;
10366
10367 default:
10368 break;
10369 }
10370}
10371
10372/* Allow exactly one hash algorithm for each signature. */
10373void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10374 mbedtls_md_type_t md_alg )
10375{
10376 set->rsa = md_alg;
10377 set->ecdsa = md_alg;
10378}
10379
10380#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10381 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10382
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010383/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010384 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010387{
10388 switch( hash )
10389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390#if defined(MBEDTLS_MD5_C)
10391 case MBEDTLS_SSL_HASH_MD5:
10392 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010393#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394#if defined(MBEDTLS_SHA1_C)
10395 case MBEDTLS_SSL_HASH_SHA1:
10396 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010397#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398#if defined(MBEDTLS_SHA256_C)
10399 case MBEDTLS_SSL_HASH_SHA224:
10400 return( MBEDTLS_MD_SHA224 );
10401 case MBEDTLS_SSL_HASH_SHA256:
10402 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404#if defined(MBEDTLS_SHA512_C)
10405 case MBEDTLS_SSL_HASH_SHA384:
10406 return( MBEDTLS_MD_SHA384 );
10407 case MBEDTLS_SSL_HASH_SHA512:
10408 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010409#endif
10410 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010412 }
10413}
10414
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010415/*
10416 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10417 */
10418unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10419{
10420 switch( md )
10421 {
10422#if defined(MBEDTLS_MD5_C)
10423 case MBEDTLS_MD_MD5:
10424 return( MBEDTLS_SSL_HASH_MD5 );
10425#endif
10426#if defined(MBEDTLS_SHA1_C)
10427 case MBEDTLS_MD_SHA1:
10428 return( MBEDTLS_SSL_HASH_SHA1 );
10429#endif
10430#if defined(MBEDTLS_SHA256_C)
10431 case MBEDTLS_MD_SHA224:
10432 return( MBEDTLS_SSL_HASH_SHA224 );
10433 case MBEDTLS_MD_SHA256:
10434 return( MBEDTLS_SSL_HASH_SHA256 );
10435#endif
10436#if defined(MBEDTLS_SHA512_C)
10437 case MBEDTLS_MD_SHA384:
10438 return( MBEDTLS_SSL_HASH_SHA384 );
10439 case MBEDTLS_MD_SHA512:
10440 return( MBEDTLS_SSL_HASH_SHA512 );
10441#endif
10442 default:
10443 return( MBEDTLS_SSL_HASH_NONE );
10444 }
10445}
10446
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010447#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010448/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010449 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010450 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010451 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010452int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010455
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010456 if( ssl->conf->curve_list == NULL )
10457 return( -1 );
10458
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010459 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010460 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010461 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010462
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010463 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010464}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010465#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010466
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010467#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010468/*
10469 * Check if a hash proposed by the peer is in our list.
10470 * Return 0 if we're willing to use it, -1 otherwise.
10471 */
10472int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10473 mbedtls_md_type_t md )
10474{
10475 const int *cur;
10476
10477 if( ssl->conf->sig_hashes == NULL )
10478 return( -1 );
10479
10480 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10481 if( *cur == (int) md )
10482 return( 0 );
10483
10484 return( -1 );
10485}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010486#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488#if defined(MBEDTLS_X509_CRT_PARSE_C)
10489int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10490 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010491 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010492 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010493{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010494 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010496 int usage = 0;
10497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010498#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010499 const char *ext_oid;
10500 size_t ext_len;
10501#endif
10502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10504 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010505 ((void) cert);
10506 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010507 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010508#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10511 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010512 {
10513 /* Server part of the key exchange */
10514 switch( ciphersuite->key_exchange )
10515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516 case MBEDTLS_KEY_EXCHANGE_RSA:
10517 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010518 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010519 break;
10520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10522 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10523 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10524 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010525 break;
10526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010527 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10528 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010529 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010530 break;
10531
10532 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533 case MBEDTLS_KEY_EXCHANGE_NONE:
10534 case MBEDTLS_KEY_EXCHANGE_PSK:
10535 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10536 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010537 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010538 usage = 0;
10539 }
10540 }
10541 else
10542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010543 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10544 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010545 }
10546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010548 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010549 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010550 ret = -1;
10551 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010552#else
10553 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10557 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010559 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10560 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010561 }
10562 else
10563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010564 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10565 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010566 }
10567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010569 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010570 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010571 ret = -1;
10572 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010573#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010574
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010575 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010578
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010579/*
10580 * Convert version numbers to/from wire format
10581 * and, for DTLS, to/from TLS equivalent.
10582 *
10583 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010584 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010585 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10586 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010589 unsigned char ver[2] )
10590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591#if defined(MBEDTLS_SSL_PROTO_DTLS)
10592 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010595 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10596
10597 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10598 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10599 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010600 else
10601#else
10602 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010603#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010604 {
10605 ver[0] = (unsigned char) major;
10606 ver[1] = (unsigned char) minor;
10607 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010608}
10609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010611 const unsigned char ver[2] )
10612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613#if defined(MBEDTLS_SSL_PROTO_DTLS)
10614 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010615 {
10616 *major = 255 - ver[0] + 2;
10617 *minor = 255 - ver[1] + 1;
10618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010620 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10621 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010622 else
10623#else
10624 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010625#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010626 {
10627 *major = ver[0];
10628 *minor = ver[1];
10629 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010630}
10631
Simon Butcher99000142016-10-13 17:21:01 +010010632int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10633{
10634#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10635 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10636 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10637
10638 switch( md )
10639 {
10640#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10641#if defined(MBEDTLS_MD5_C)
10642 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010643 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010644#endif
10645#if defined(MBEDTLS_SHA1_C)
10646 case MBEDTLS_SSL_HASH_SHA1:
10647 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10648 break;
10649#endif
10650#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10651#if defined(MBEDTLS_SHA512_C)
10652 case MBEDTLS_SSL_HASH_SHA384:
10653 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10654 break;
10655#endif
10656#if defined(MBEDTLS_SHA256_C)
10657 case MBEDTLS_SSL_HASH_SHA256:
10658 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10659 break;
10660#endif
10661 default:
10662 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10663 }
10664
10665 return 0;
10666#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10667 (void) ssl;
10668 (void) md;
10669
10670 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10672}
10673
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10675 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10676int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10677 unsigned char *output,
10678 unsigned char *data, size_t data_len )
10679{
10680 int ret = 0;
10681 mbedtls_md5_context mbedtls_md5;
10682 mbedtls_sha1_context mbedtls_sha1;
10683
10684 mbedtls_md5_init( &mbedtls_md5 );
10685 mbedtls_sha1_init( &mbedtls_sha1 );
10686
10687 /*
10688 * digitally-signed struct {
10689 * opaque md5_hash[16];
10690 * opaque sha_hash[20];
10691 * };
10692 *
10693 * md5_hash
10694 * MD5(ClientHello.random + ServerHello.random
10695 * + ServerParams);
10696 * sha_hash
10697 * SHA(ClientHello.random + ServerHello.random
10698 * + ServerParams);
10699 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010700 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010701 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010703 goto exit;
10704 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010705 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010706 ssl->handshake->randbytes, 64 ) ) != 0 )
10707 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010709 goto exit;
10710 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010711 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010712 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010713 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010714 goto exit;
10715 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010716 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010717 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010719 goto exit;
10720 }
10721
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010722 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010723 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010725 goto exit;
10726 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010727 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010728 ssl->handshake->randbytes, 64 ) ) != 0 )
10729 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010731 goto exit;
10732 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010733 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010734 data_len ) ) != 0 )
10735 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010737 goto exit;
10738 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010739 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010740 output + 16 ) ) != 0 )
10741 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010743 goto exit;
10744 }
10745
10746exit:
10747 mbedtls_md5_free( &mbedtls_md5 );
10748 mbedtls_sha1_free( &mbedtls_sha1 );
10749
10750 if( ret != 0 )
10751 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10752 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10753
10754 return( ret );
10755
10756}
10757#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10758 MBEDTLS_SSL_PROTO_TLS1_1 */
10759
10760#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10761 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010762
10763#if defined(MBEDTLS_USE_PSA_CRYPTO)
10764int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10765 unsigned char *hash, size_t *hashlen,
10766 unsigned char *data, size_t data_len,
10767 mbedtls_md_type_t md_alg )
10768{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010769 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010770 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010771 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10772
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010774
10775 if( ( status = psa_hash_setup( &hash_operation,
10776 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010777 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010778 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010779 goto exit;
10780 }
10781
Andrzej Kurek814feff2019-01-14 04:35:19 -050010782 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10783 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010784 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010785 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010786 goto exit;
10787 }
10788
Andrzej Kurek814feff2019-01-14 04:35:19 -050010789 if( ( status = psa_hash_update( &hash_operation,
10790 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010791 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010792 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010793 goto exit;
10794 }
10795
Andrzej Kurek814feff2019-01-14 04:35:19 -050010796 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10797 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010798 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010799 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010800 goto exit;
10801 }
10802
10803exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010804 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010805 {
10806 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10807 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010808 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010809 {
10810 case PSA_ERROR_NOT_SUPPORTED:
10811 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010812 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010813 case PSA_ERROR_BUFFER_TOO_SMALL:
10814 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10815 case PSA_ERROR_INSUFFICIENT_MEMORY:
10816 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10817 default:
10818 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10819 }
10820 }
10821 return( 0 );
10822}
10823
10824#else
10825
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010826int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010827 unsigned char *hash, size_t *hashlen,
10828 unsigned char *data, size_t data_len,
10829 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010830{
10831 int ret = 0;
10832 mbedtls_md_context_t ctx;
10833 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010834 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010835
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010837
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010838 mbedtls_md_init( &ctx );
10839
10840 /*
10841 * digitally-signed struct {
10842 * opaque client_random[32];
10843 * opaque server_random[32];
10844 * ServerDHParams params;
10845 * };
10846 */
10847 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10848 {
10849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10850 goto exit;
10851 }
10852 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10853 {
10854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10855 goto exit;
10856 }
10857 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10858 {
10859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10860 goto exit;
10861 }
10862 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10863 {
10864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10865 goto exit;
10866 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010867 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010868 {
10869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10870 goto exit;
10871 }
10872
10873exit:
10874 mbedtls_md_free( &ctx );
10875
10876 if( ret != 0 )
10877 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10878 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10879
10880 return( ret );
10881}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010882#endif /* MBEDTLS_USE_PSA_CRYPTO */
10883
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010884#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10885 MBEDTLS_SSL_PROTO_TLS1_2 */
10886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010887#endif /* MBEDTLS_SSL_TLS_C */